Advertisement
m1o2

find sum of two primes, fxp, m1o2, elsf.net m1j2, miky

Nov 20th, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3. #include <math.h>
  4.  
  5. int isPrime( int n ){
  6.  
  7.     int stop = sqrt( n );
  8.     int index = 2;
  9.  
  10.     for( ; index <= stop; ++index )
  11.         if( (n%index) == 0 )
  12.             return 0;
  13.     return 1;
  14. }
  15.  
  16. void printAsPrimeSum( int n ){
  17.  
  18.     int middle = n/2;
  19.     int found = 0;
  20.  
  21.     for( ; 0 == found && middle > 0; --middle )
  22.         if( isPrime( middle ) && isPrime( n - middle ) ){
  23.  
  24.             printf(" %d = %d + %d\n", n, middle, n - middle );
  25.             found = 1;
  26.         }
  27. }
  28.  
  29.  
  30. int main( int argc, char *argv[] ){
  31.  
  32.     int index = 2;
  33.  
  34.     for( ; index < 1001; ++index )
  35.         printAsPrimeSum( index );
  36.  
  37.     getch();
  38.     return 0;
  39. }
  40.  
  41.  
  42.  
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement