Interview Questions: quality factor decomposition

Source: Internet
Author: User
Question: Calculate the prime factor decomposition of a number, for example, input 90, and output 2 *3*3*5.

Disintegration ideas:

Requires a prime factor decomposition of number N. First, obtain all the prime numbers within N, put them in the prime [] array, and then let Prime [I] Remove n. If division can be performed, this prime [I] is the prime factor of N, otherwise it is not.

The prime [] method is to find the prime number by screening.

CodeImplementation

View code

# Include <iostream> # Include <Stdlib. h> Using   Namespace  STD;  Bool Isprime ( Int Number );//  Determine whether the number is a prime number  Void Printprime ( Int Number ); //  Search for all prime numbers within 0... number. No output is found.  Void Printarray ( Int Arry [], Int Len ); //  Print Array  Void Getprime2 ( Int Number, Int Prime []);//  Evaluate the prime number Array Using the isprime (INT number) Method  Void Getprime3 ( Int Number, Int Prime []); //  Implement isprime (INT number) directly in the Method)  //  Prime number obtained by screening  Int Getprime ( Int Number, Int  Prime []) {  Int Len = Number +1  ;  Int Count = 0  ;  Int  I, J;  Bool * Flag = New   Bool  [Len];  //  Initialize flag, all true;      For (I = 0 ; I <Len; I ++ ) {Flag [I] = True  ;}  For (I = 2 ; I <= SQRT (number); I ++) //  The prime factor of number must be less than SQRT (N)  {  If (Flag [I]) //  If I is a prime number, I * j is the Union number. These multiples are j = 2, 3, 4 ....  {  //  I indicates the prime factor, and J indicates a multiple.              For (J =2 ; I * j <= number; j ++) //  As long as I * j <= number, the flag is always assigned to the next value [];  {Flag [I * J] = 0  ;}}}  For (I = 2 ; I <= number; I ++ ){  If  (Flag [I]) {Prime [count ++] = I ;}} printarray (Prime, count );  Return Count ;}  //  Determine if it is a prime number  Bool Isprime ( Int  Number ){  Int  I;  Bool Flag = True  ;  For (I = 2 ; I <= SQRT (number); I ++) //  If number = 2, 3, The for loop cannot be executed, and the previous flag is returned, which is true. {  If (Number % I = 0  ) {Flag = False  ;  Break  ;}}  Return  Flag ;}  //  Print the prime number of 2... Number  Void Printprime ( Int  Number ){ Int Count = 0  ;  For ( Int I = 2 ; I <= number; I ++ ){  If  (Isprime (I) {count ++ ; Cout <I < Endl ;}}  //  Cout <count <Endl;  } //  Print Array  Void Printarray ( Int Arry [], Int  Len ){  For ( Int I = 0 ; I <Len; I ++ ) Cout <Arry [I] < "   "  ; Cout < Endl ;} //  Store the prime numbers within number in prime []  Void Getprime2 ( Int Number, Int  Prime []) {  Int Count = 0  ;  For ( Int I = 2 ; I <= number; I ++ ){  If (Isprime (I) {Prime [count ++] = I ;}} printarray (Prime, count );}  //  Store the prime numbers within number in prime []  Void Getprime3 ( Int Number, Int  Prime []) {  Int Count = 0  ;  For ( Int I = 2 ; I <= number; I ++ ){  Bool Flag = True  ;  For ( Int J = 2 ; J <= SQRT (I); j ++) //  Note that j <= SQRT (I) is used instead of j <= SQRT (number)  {  If (I % J = 0  ) {Flag =False  ;  Continue  ;}}  If  (FLAG) {Prime [count ++] = I ;}} printarray (Prime, count );}  Void  Main (){  Int Number = 90  ;  Int * Prime = New  Int  [Number];  //  Getprime2 (number, prime );  //  Store the prime number in the prime [] Array  //  Getprime3 (number, prime );  //  Store the prime number in the prime [] Array  //  Getprime (number );  //  Cout <isprime (number) <Endl;  // For bool type output, true = 1, false = 0  //  Printprime (number );      Int Len = getprime (number, prime ); //  Store the prime number in the prime [] Array      For ( Int I = 0 ; I <Len, number> = prime [I]; I ++) //  The number is smaller, and the prime [I] is larger. If the number is <Prime, it is not necessary to calculate  {  While (Number % prime [I] =0  ) {Number = Number/ Prime [I]; cout < Prime [I];  If (Number! = 1 ) //  If number = 1, it indicates that I is the last factor, so it does not need to be output * Cout < "  *  "  ;} Delete [] Prime; System (  " Pause  "  );} 

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.