Code RoomPrime exponent in factorial
MediumPrep Room Coding #1213

Prime exponent in factorial

CodingAlgorithms & data structuresMid–Senior~22 min

Given a number n and a prime p, return the exponent of p in the prime factorization of n! (n factorial) using Legendre's formula: sum over k>=1 of floor(n / p^k). Do NOT compute n! itself (it would overflow instantly). For p=5 this also equals the number of trailing zeros of n! when paired with the (larger) power of 2. Constraints: 0 <= n <= 10^9, p is a prime <= 10^9.

Implement
prime_power_in_factorial(n: int, p: int) → int
Examples
in[100,5]out24
What a strong answer looks like

State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.

0:00 of about 22 min
InputExpectedGot
[100,5]24not run yetsample