/*Program to print sum of all prime numbers upto 2000000*/ /*To check whether a number is prime or not it takes n^2 time by brute force but this program is sqrt(n) according to mathematical rules to check prime number efficiently. */ #include<stdio.h> #include<stdlib.h> #include<math.h> #define MAX 2000000 int main() { int i,j; unsigned long int prod=0; int prime; for(i=2;i<=MAX;i++){ prime=1; for(j=2;j<(int)(sqrt(i)+1);j++){ if(i%j==0){ prime=0; break; } } if(prime==1){ prod=prod+i; } } printf("%ld",prod); return 0; }
Search Program on this blog
Friday, 21 August 2015
Summation of primes: program to print sum of all prime numbers upto 2000000
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment