TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

Factorials

Problem statement, definition, constraints, and public examples.

Problem Statement

PROBLEM STATEMENT
Given a number n and a base b, return the largest power of b that divides n
factorial.

DEFINITION
Class: Factorials
Paramenters: int, int
Returns: int
Method signature (be sure your method is public): int largestPower(int n, int b);

NOTES
TopCoder will ensure the validity of the inputs.  Inputs are valid if all of
the following criteria are met:
- n will be between 1 and 50 inclusive.
- b will be between 2 and 29 inclusive.

EXAMPLES
1.  If n = 5, and b = 2.  
-  5! = 120 = 2^3 * 15.  Return 3.

2.  If n = 7, and b = 5.
-  7! = 5040 = 5^1 * 1008.  Return 1.

3.  If n = 11, and b = 3.
-  11! = 39916800 = 3^4 * 492800.  Return 4.

Notes to admins:
This problem gets tough for bigger numbers where n! is greater than
Integer.MAX_VALUE.  The solution is pretty simple when b is prime in that case,
but when b is composite it gets tricky.

Definition

Class:
Factorials
Method:
largestPower
Parameters:
int, int
Returns:
int
Method signature:
int largestPower(int param0, int param1)
(be sure your method is public)

Constraints

    Examples

      No public sample cases were explicitly marked for this problem in the original data.

      Back to all problems