Factorial and factor
Requirements: Enter a positive integer n (1<n <101), and the factorial n! =1X2X3X...XN decomposition into a factor multiplied by the form of small to large output of each prime number (2,3,5, ... ) of the index.
For example, 825=3x52x11 should be represented as (0,1,2,0,1), indicating 0,1,2,0,1 2,3,5,7,11 respectively. The program should ignore primes that are larger than the maximum element factor (otherwise there will be an infinite number of 0 at the end).
5 53 5 ! =3 1 53 ! =49 8 4 4 2 2 1 1 1 1 1 1
Algorithm code:
#include <iostream>#include<cstring>using namespacestd;intprime[ -], CNT;BOOLIsPrime (intN//Judging primes, n cannot be too large{ if(N <2)return 0; for(inti =2; I * I <= N; i++) if(n% i = =0)return 0; return 1;}intMain () { for(inti =2; I <= -; i++) if(IsPrime (i)) prime[cnt++] = i;//List of construction primes intN, p[ -]; while(Cin >>N) {if(N <2|| N > -) Break; Memset (P,0,sizeof(p)); intMax =0; for(inti =1; I <= N; i++) { intm = i;//I need to copy the I into m instead of modifying it directly while doing the division. for(intj =0; J < CNT; J + +) { while(m% prime[j] = =0)//divide repeatedly by prime[j], and accumulate p[j]{m/=Prime[j]; P[J]++; if(J > Max) max = j;//Update Max Factor subscript } } } for(inti =0; I <= Max; i++)//loop only to the maximum subscript{cout<< P[i] <<" "; } cout<<Endl; }}
Algorithm--factorial and factor