-: 0:Source:p1.c -: 0:Graph:p1.gcno -: 0:Data:p1.gcda -: 0:Runs:1 -: 0:Programs:1 -: 1:#include -: 2:#include -: 3: -: 4://This program uses two functions to compute all primes up to n -: 5://// Then checks that the two functions agree, printing an error if not -: 6://// Compile use the -lm flag to get the sqrt from math library -: 7:// -: 8:// 19998: 9: int prime1(n) // a slow prime test -: 10: int n; -: 11: { int i; 11552904: 12: for (i=2; i < n; i++) 11550446: 13: if (n%i == 0) 17540: 14: return 0; 2458: 15: return 1; -: 16: } -: 17: 118756: 18:int root(n) // computes integer sqrt of n -: 19:int n; 118756: 20:{return (int) sqrt( (double) n) ; } -: 21: 9999: 22:int prime2(n) // a faster prime test only test up to sqrt(n) -: 23:int n; -: 24: -: 25:{ int i; 118756: 26: for (i=2; i <= root(n) ; i++) 117527: 27: if (n%i == 0) 8770: 28: return 0; 1229: 29: return 1; -: 30:} -: 31: 1: 32: main() { -: 33: -: 34: int i, n, pcount; -: 35: 1: 36: n = 10000; 1: 37: pcount=0; -: 38: 10000: 39: for (i=2; i <= n; i++) 9999: 40: {if( prime1(i) != prime2(i) ) #####: 41: printf("error, wrong answer for %d\n",i); 9999: 42: if(prime1(i) ) pcount++; -: 43: } -: 44: 1: 45: printf("primes found less than %d = %d\n",n,pcount); -: 46: 1: 47: } -: 48: