The correctness thinking is written on the Code chip.
Forget one thing: the role of the Euler Phi function is to evaluate 1 ~ Number of units in N
# Include <cstdio> # include <cstring> # include <cmath> int Phi [5000000]; // If the calculated number is 6, when I = 2 and I = 3, it will enter the inner loop. // once it enters the inner loop, it will calculate the Euler's formula based on the prime number. // insert: euler's formula: Phi (n) = N * (1-1/P1) * (1-1/P2 )*... (1-1/PN) // you can think that for each definite number N, the Euler's formula is not completed in a loop, /// but it can be proved that when the entire cycle is completed, the Euler's formula is completed. In other words, it can ensure that) in this array, the correct answer is calculated strictly according to the Euler's formula, instead of following other algorithms void phi_table (int n, int * PHI) {for (INT I = 2; I <= N; I ++) {Phi [I] = 0;} Phi [1] = 1; for (INT I = 2; I <= N; I ++) {If (! Phi [I]) {for (Int J = I; j <= N; j + = I) {If (! Phi [J]) Phi [J] = J; Phi [J] = Phi [J]/I * (I-1) ;}}} int main () {phi_table (10000, Phi); int N; while (scanf ("% d", & N) printf ("% d \ n", Phi [N]);}
The code for printing the Euler's function value in the above Code is correct, but the whole code is to verify and prove the Euler's Phi function, there is no other intention