Problem :
Now you have to solve an interesting problem. Any integer n (where 1 < n < 100, means values of n from 2 can be up to 99) to find the number of times a prime number exists by expressing the factorial of have to do Like, we know, 5! = 120 = 2 * 2 * 2 * 3 * 5. Here 2 is 3 times, 3 is 1 time and 5 is 1 time. So if the input is 5 the output will be: 5! = (2, 3), (3, 1), (5, 1). Do you understand one thing that at the beginning of n ?Is it going to be a hassle to figure out the value of the factorial and then break the original product? Because the value of n is maximum 99 and integers cannot hold the factorial value of any number greater than 12. "Actually this program doesn't need to figure out the value of n!. Just do a little math. And put the prime numbers from 2 to 99 into an array."
I can't understand how will I find out factorial from prime number? Please give me some clue .
Here, the author said, "Actually this program doesn't need to figure out the value of n!. Just do a little math. And put the prime numbers from 2 to 99 into an array."
My question is how will I find out the factorial from this array (prime number)
Suppose, I copy the prime numbers into an array
then ?
The prime factors of n! are the prime factors of n, plus the prime factors of n-1, plus the prime factors of n-2, ..., plus the prime factors of 2.
That means the largest number we need to factorize is n, and the largest prime we need to deal with is at most n.
Another interesting property is that each prime in [2,n] is guaranteed to appear once.
Ahead of time, create
primes, an array of all the primes from 2 to 100.Set
num_primesto the number of primes in that array.Create
counts, an array of sizenum_primesinitialized to zeroes.For
term= 2 tonby 1,rtoterm.prime_idx= 0 to min(num_primes-1,term) by 1,ris greater than zero and the remainder ofrandprimes[prime_idx]is zero,counts[prime_idx].primes[prime_idx]fromr.That's it. We want the prime factors of the each term of the factorial. The outer loop visits each term of the factorial, and the inner loop finds the prime factors of the current term.
For n = 5, you end up with