Code Room
CodingMediumcod-g647
Subject Number theoryLevel Mid–Senior~22 minCommon in Algorithms & data structures interviewsIndustries Software development

Question

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.

Vibe coding: describe the solution in plain language (or narrate it) and the coach grades your approach. Generating runnable code from your description is coming next.

Run or narrate your approach, then ask the coach.