Yet Another Blog

Posts Tagged ‘Number Theory

The number of trailing zeros in the factorial of a large integer

with 3 comments

Problem

PKU – 1401

Given a (large) integer K, find the number of zeros on the right of the factorial of K. For example, factorial 15 = 1,307,674,368,000, so the number of zeros on right of the factorial (let’s denote it Z(1,307,674,368,000) ) equals 3.

The input integer K can be as large as 1000,000,000.

Solution

Given an integer N, let the prime factorization of N be 2^{p^N_2}.3^{p^N_3}.5^{p^N_5} ... .

The number of zeros on the right of N, that is Z(N), is simply the number of times N can be divided by 10 without getting a fraction. It is obvious that Z(N)=min\{p^N_2,p^N_5\}.

Since N, in our case, is the factorial of another integer, K, then the prime factorization of N is just the product of the prime factorizations of all the integers from 1 to K inclusive. This can be written as

\displaystyle\prod_{i=1}^K{2^{p^i_2}.3^{p^i_3}.5^{p^i_5} ...}

So, p^N_2=\displaystyle\sum_{i=1}^K{p^i_2} and p^N_5=\displaystyle\sum_{i=1}^K{p^i_5}.

Consequently, Z(N)=min\{\displaystyle\sum_{i=1}^K{p^i_2},\displaystyle\sum_{i=1}^K{p^i_5}\}

In other words, that is the minimum between the sum of the number of times 2 has appeared as a factor for all integers in the range 1 through K versus the sum of the number of times 5 has appeared as a factor for all integers in the same range. Obviously the number of times 5 has appeared is smaller, because it appears every 5 integers, versus 2 in the case of 2. So actually we are interested in p^N_5.

We might go through all the integers in the range 1 to K and find, for each integer in such range, how many times 5 appears as a factor. But due to the fact that we are dealing with large integers that won’t be feasible. Alternatively, we may solve it as follows: the integer division K/5 gives us the number of integers in the range 1 through K for which 5 is a factor at least once, K/25 gives the number of integers in the same range for which 5 is a factor at least twice, … and so on.

Consequently, p^N_5=\displaystyle\sum_{i=1}^{\lfloor log_5 K\rfloor}{K/5^i}, where the division is integer division.

The formula is logarithmic in the value of K, so it is linear in the number of bits. That is efficient enough to handle the required input.

Written by Hatem Abdelghani

July 28, 2009 at 9:22 am

The number of the factors of the factors of a number!

with 2 comments

Problem:

PKU – 3604

Given an integer N, find all the factors of N including the trivial factors (1 and N). For each factor f, find the cube of the number of factors of  f. Then sum all those cubes.

For example, if N=4, the set of all possible factors are 1, 2 and 4. 1 has only one factor which is 1. 2 has 2 factors: 1 and 2. 4 has 3 factors: 1, 2 and 4. So the result equals 1^3 + 2^3 + 3^3 = 1 + 8 + 27 = 36

Solution:

A naive solution won’t work since the input is too large (up to 500,000 test cases and N can be as large as 5,000,000).

The idea is as follows: According to the fundamental theorem of arithmetic, any integer N>1 has a unique prime factorization (i.e., there is one and only one way to factorize this integer into a product of prime numbers).  For example, 420 has the following unique prime factorization:

420 = 2 * 2 * 3 * 5 * 7

For a given integer N, let the prime factorization of N be \sum_{i=1}^{n} {F_i^{p_i}} where n is the number of prime factors of N, F_i is the i^{th} distinct prime factor which is raised to the power p_i.

We can see that the total number of general factors of N (all factors, not only the primes) equals \prod_{i=1}^n {(p_i+1)}.

This value follows from the fact that any general factor f_j of N is just the product of some of the prime factors of N. So any such general factor f_j has the prime factorization\sum_{i=1}^{n} {F_i^{q_{j,i}}} where 0\le q_{j,i}\le p_i. The case where q_{j,i}=0 for all i will give us the general factor f_j=1. The case where q_{j,i}=p_i for all i will give us the general factor f_j=N  (the number itself).

However, we are not interested in the number of general factors of N; we are interested in the number of general factors of each general factor of N (!!) to be cubed each and summed together.

This value can be obtained as follows: using the same reasoning mentioned above, for each general factor f_j of N, the number of general factors of f_j equals \prod_{i=1}^n {(q_{j,i}+1)}. For each j and i, the value of q_{j,i} ranges from 0 to p_i inclusively, therefore the required value, the summation of cubes of numbers of general factors of all general factors of N, equals:

\sum_{q_{j,1}=0}^{p_1}\sum_{q_{j,2}=0}^{p_2}...\sum_{q_{j,n}=0}^{p_n} {\prod_{i=1}^n {(q_{j,i}+1)}}^3

Re-distributing the summation we get the following formula:

\sum_{q_{j,1}=0}^{p_1} (q_{j,1}+1)^3 * \sum_{q_{j,2}=0}^{p_2} (q_{j,2}+1)^3 * ... * \sum_{q_{j,n}=0}^{p_n}(q_{j,n}+1)^3

Using the well-known series \sum_{i=1}^{n} i^3 = (n(n+1)/2)^2 we can re-write our formula again as follows:

((p_1+1)(p_1+2)/2)^2 * ((p_2+1)(p_2+2)/2)^2 * ... * ((p_n+1)(p_n+2)/2)^2 \\ = \prod_{i=1}^n ((p_i+1)(p_i+2)/2)^2

Thus, all we need to calculate is the prime factorization of N, from which we can get the value of p_i for each i.

Necessary Optimizations:

Due to large input sizes, calculating the prime factorization of every input value from the scratch will give TLE. The process can be optimized by using simple dynamic programming.

For every integer N, the value we need to calculate is

res(N) = \prod_{i=1}^n ((p_i+1)(p_i+2)/2)^2 \\ = ((p_1+1)(p_1+2)/2)^2 \prod_{i=2}^n ((p_i+1)(p_i+2)/2)^2\\ = ((p_1+1)(p_1+2)/2)^2 res(N')

where N’ is some integer less than N. So actually we can re-use the values of res( ) computed for smaller input values to compute larger ones. All we need for any given integer N is to find one of its  p_i values (the power of one of its prime factors), then we can calculate res(N) recursively, and there comes dynamic programming.

Finding a power p_i of one of the prime factors F_i of N can be done easily once the prime factor itself F_i is found. We can use a modified version of Sieve of Eratosthenes to find a prime factor for each integer in a given range.

The original Sieve was used to determine which natural numbers are prime and which are not, up to some maximum number MAX.   Starting from counter=2, we mark all multiples of the counter as not-prime until MAX is reached. Move the counter forward to the next unmarked integer (which will always be prime since it is not a multiple of any smaller integer).

In the modified version, instead of marking the multiples of the counter as not-prime, we will associate the counter with its multiples as a prime factor of them. This way we get a prime factor for each natural number up to MAX (which equals 5,000,000 is our case).

Having reached such step, dynamic programming can be done easily by iterating through natural numbers up to MAX, for each natural number N, we can calculate res(N) using the prime factor of N we have obtained before and res(N’) for some smaller natural number N’ also calculated before.

Written by Hatem Abdelghani

May 7, 2009 at 6:28 am

Follow

Get every new post delivered to your Inbox.