Topic:
All of you have learned about the decomposition of numbers in the country, and understand how to use paper to calculate the results, now you come to teaches computer decomposition.
Factorization is the dividing of a number into the multiplication of a number of numbers, such as 12=2^2 * 3
Where the symbol of the second party is represented by ^
Enter a description
An integer greater than 1 and less than 1000000
Output description
A string
Sample input
2017999997
Sample output
2^2 * 517757 * 1321
analysis : Using two arrays a,b,a[i] and b[i] respectively to store the small to large order of the I factorization and its exponent. For each quality factor, will be a one-time "extraction" clean, so that the most recent encounter is always prime, if you can divide the current remaining number now, the number is today's quality factor ... This goes on until the candidate prime number exceeds sqrt (n).
Code:
1#include <iostream>2#include <math.h>3 using namespacestd;4 inta[10000],b[10000];5 intTemp,now,tot;6 intMain () {7 intN;8 while(Cin>>N) {9Temp= (int)((Double) sqrt (n) +1);Tennow=N; Onetot=0; A for(intI=2; i<=temp;++i) { - if(now%i==0){ -a[++tot]=i; theb[tot]=0; - while(now%i==0){ -++B[tot]; -Now/=i; + } - } + } A if(now!=1){ ata[++tot]=Now ; -b[tot]=1; - } - for(intI=1; i<tot;i++){ - if(b[i]==1) cout<<a[i]<<" * "; - Elsecout<<a[i]<<"^"<<b[i]<<" * "; in } - if(b[tot]==1) cout<<a[tot]<<Endl; to Elsecout<<a[tot]<<"^"<<b[tot]<<Endl; + } - return 0; the}
Algorithm-Factor decomposition