The C language implementation of daffodils & prime numbers & prime factor decomposition, and daffodils vb
Recently, I flipped through the C language textbooks and read three interesting small programs, including: search for "Daffodils", judge whether a number is a prime number, and perform prime factor decomposition on a number. I want to put these three items in a program and write this article.
Algorithm steps
1. Search for "Daffodils ".
The "Daffodils" refers to a three-digit number. The cubes of each digit are equal to the number itself. For example, 153 is a "Daffodils" because 153 = the power of 1 + the power of 5 + the power of 3.
2. Determine whether a number is a prime number.
Prime Number refers to the number that can only be divided by 1 and itself. To determine whether a number is a prime number, use this number to divide it by the square root of the number. If it can be divisible, it indicates that this number is not a prime number, and vice versa.
3. Perform prime factor decomposition on a number.
For a prime factor decomposition of a number n, you should first find a prime number k (starting from 2), and then follow the steps below:
(1) If the prime number is equal to n, the process of decomposing the prime factor is over.
(2) If n is not equal to k, but n can be divisible by k, the quotient of n divided by k is used as the new positive integer n, and the first step is repeated.
(3) If n cannot be divisible by k, k + 1 is used as the new value of k and the first step is repeated.
Procedures
This program takes three steps: the first step is to find the "Daffodils"; the second step is to judge whether the "Daffodils" is a prime number; the third step, if it is not a prime number, this number is decomposed by a prime factor.
C program code
Compile command
This program is compiled in Linux with the command gcc-g-o AlgorithmStudyAlgorithmStudy. c-lm.
Note: Do not ignore "-lm". Otherwise, an error will be reported during compilation, prompting that "sqrt" cannot be found ".
Program running result
After the compilation is successful, run the "AlgorithmStudy" command. The result is as follows:
153 is the number of daffodils.
153 is not a prime number.
153 = 3*3*17
------
370 is the number of daffodils.
370 is not a prime number.
370 = 2*5*37
------
371 is the number of daffodils.
371 is not a prime number.
371 = 7*53
------
407 is the number of daffodils.
407 is not a prime number.
407 = 11*37
------
---------------------------------------------------
My public account: zhouzxi. Please scan the following QR code: