[C language] Classic - judging primes less than 100

 

/ * The number of prime saved within an array 100, and then print out the value of the array * / 
#include <stdio.h> 
#include <math.h> int Pd_Ss ( int I); int main () {
     int A [ 100 ] = { 0 }; // for storing prime
     int I = 0 , K = 0 ; // count cycle for 
    the printf ( " prime has less than 100: \ R & lt \ n- " ); for (I = 2 ; i <= 100 ; i ++ ) {// Analyzing all numbers 2-100
         IF (Pd_Ss (i) == 1 ) {// call the function returns 1 if the function is determined, then the value i of the input array 
            a [k






    ++] = I; // the prime number stored in the array 
        } 
    } 

    for (I = 0 ; I <K; I ++ ) { 
        the printf ( " A [D%] =% D \ R & lt \ n- " , I, A [I ]); // output primes 
    } 

    the printf ( " \ R & lt \ n- " ); // newline
     return  0 ; 
} 

int Pd_Ss ( int I) {// Analyzing prime function
     int J; 

    for (J = 2 ; J * J <= I; J ++ ) { 
         IF (% I J == 0 ) {// if the modulo is 0, 0 (exit function) returns
             return  0  ;
        }
    } 

    Return  1 ; // Returns 1 

}

Note: The program code comparison basis, the variable name in all programs more casual

Thinking determination primes function (Pd_Ss): the use cycle, determines square values ​​of j, i, and when j squared is less than i, into the circulation, is determined at this time i can divisible j, if divisible, described i is not a prime number , exit the function returns 0, otherwise it returns 1;

 

Guess you like

Origin www.cnblogs.com/xdorz/p/11390030.html
Recommended