Algorithm-Factor decomposition

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.