Simple C ++ code (1) factorization

Source: Internet
Author: User

The code in this section is sufficient to show the well-regulated and simple code style. Although it is not very clever, it is not very elegant at least. I will ask myself to be worthy of the Party, the country, and the people. The task in this article is to display the results of a natural number of prime factor decomposition, without involving algorithm data analysis, so you can focus on the code design.
Generally, a program can be divided into three parts: input, operation, and output. Therefore, the primary task of programming is to break down the program into three major components and understand them one by one.
Input: it is a natural number. to simplify it, You can directly write it into a variable in the code.
Calculation: it is a good strategy to break down the natural number into a string of prime numbers and store these quality factors using arrays.
Output: the concatenation operator that shows the number of string-quality factors, for example:
78 = 2*3*13
13 = 13
Based on this idea, you can quickly write the skeleton of this program.

Int main ()
Const int NUM_SIZE = 10;
Int num = 1178;
Int products [NUM_SIZE] = {0 };

Int count = factorize (num, products );
Display (num, products, count );
Return 0;
It seems that the output part is easier to solve, so we should first compile it. The code is straightforward and easy to understand. If you insist on understanding it, You can only say that you are not suitable for writing code. Learn other things. Your expertise is not here.

Void display (int num, int products [], int count)
Using namespace std;
Assert (count> 0 );
Cout <num <"=" <products [0];
For (int I = 1; I <count; I ++)
Cout <"*" <products [I];
Cout <endl;

Obviously, factorize is also the core part of the operation of the program, and the array size is not passed in. It is intentional because it can reduce unnecessary code, when the function has little requirements on the array size, we can assume that the array is large enough to solve most of the problems, especially when writing a module. It would be quite painful if everything was to be nitpicking.
How can we break down the prime factor? Without code, let's first think about how we break down the prime factor. If the human brain cannot solve this problem, even how many flowcharts and algorithm languages are moved out will not help. If there is a problematic algorithm, the clearest expression will still be the Code itself. I have been despising flowcharts and other things since I started programming. Fortunately, the factorization method is not difficult. The method is as follows: from 2, a prime number is a prime number to divide the number N to be decomposed one by one. If all prime numbers within N are retrieved, N is a prime number, and the decomposition is over. If division can be performed, it indicates that the prime number is a factor of N, and N is also the operator of Division. Repeat this step again until N becomes a prime number. Finally, we summarize all the prime numbers that can be divisible, along with the final N. Those who have not forgotten the factorization will understand the above.
The question is how to translate the above algorithm into a C ++ expression. When we find that the final prime number factor is summarized, We Need To summarize the final N. These two steps are actually the same thing. In fact, when N is a prime number, N is a factor of its own, therefore, the last step can be directly simplified to summarize all the prime factor, as long as the division process, do more operations, save N. Therefore, the above decomposition method can be changed to the division of N by dividing N with all the prime numbers within N, repeating the division process until N is changed to 1. Obviously, this corresponds to a loop, and the condition for this loop is N> 1.
Next, we should consider what the program will do when the prime number can divide N. 1. N = N/prime number; 2. Write down the prime number.
Is it necessary to divide N by prime numbers? In fact, there is no need. As long as we use all natural numbers smaller than or equal to N greater than 1 to divide N, we can ensure all the prime numbers within N. Although the efficiency is a little lower, but the code is easier to write and the definition is higher. When coding, you must resist the impulse to optimize it in desperate ways. There is no need to keep improving. The code for factorization is as follows:

Int factorize (int num, int products [])
Assert (num> 1 );
Const int MINNEST_PRIME = 2;
Int count = 0;
While (num> 1)
Int prime = MINNEST_PRIME;
While (num % prime! = 0)
Prime ++;
Num/= prime;
Products [count ++] = prime;
Return count;

Organize the above Code, add necessary header files, and feel the fruits of hard work!


Related Article

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: 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.