Factor and factorial, factor factorial
Enter a positive integer n (2 <=n <= 100) and multiply the factorial n! = 1*2*3 * · * n is decomposed into the form of prime factor multiplication, and each prime number is output from small to large (2, 3, 5 ,···) index. For example, 825 = 3*5 ^ 2*11 should be expressed as (, 1 ), 0, 1, 2, 0, 1, 2, 3, 5, 7, and 11 respectively. Your program should ignore prime numbers that are larger than the maximum prime factor (otherwise there will be an infinite number of zeros at the end ).
Sample input:
5
53
Sample output:
5! = 3 1 1
53! = 49 23 12 8 4 4 3 2 2 1 1 1 1 1 1
Program:
# Include <iostream>
# Include <cstring>
Using namespace std;
// Determine the prime number. Note: n cannot be too large.
Int is_prime (int n)
{
For (int I = 2; I * I <= n; I ++)
If (n % I = 0) return 0;
Return 1;
}
// Prime number table
Int prime [100], count = 0;
Int main ()
{
// The exponent of n and each Prime Number
Int n, p [100];
// Construct a prime number table
For (int I = 2; I <= 100; I ++)
If (is_prime (I) prime [count ++] = I;
While (cin> n)
{
Cout <n <"! "<" = ";
Memset (p, 0, sizeof (p ));
Int maxp = 0;
For (int I = 1; I <= n; I ++)
{
// I must be copied to m instead of being modified directly during division.
Int m = I;
For (int j = 0; j <count; j ++)
While (m % prime [j] = 0) // repeatedly divided by prime [j] and accumulated p [j]
{
M = m/prime [j];
P [j] ++;
If (j> maxp) maxp = j; // update the maximum prime factor subscript.
}
}
// Only loop to the maximum subscript
For (int I = 0; I <= maxp; I ++)
Cout <"<p [I];
Cout <endl;
}
Return 0;
}