View Single Post
Go4Expert Member
29Nov2006,15:06  
friendsforniraj's Avatar
well this code is good but it ll take more time(for computational work)
please check this code the 2nd for loop

Code:
 /* ** Program to find the sum of all numbers between 3 and 60 ** @author : Pradeep ** @date : 11/29/2006 */  
#include <stdio.h>  
#include<math.h> 
void main(void)
  { 
    printf("\n please enter the upper limit");
   int n;
    scanf("%d",&n);\\now n can be given by user
    unsigned int i,j,s=0,is_prime;     
    for(i=3;i<=n;i++) 
    {   
       is_prime = 1; // Assuming that current value of i is prime           
       // Checking for prime    
       for(j=2;j<=sqrt(i);j++)  
       {  
           if(i%j==0) // Not prime, set is_prime to 0 and break   
          {
            is_prime = 0;           
            break;       
           }  
       }     
     if(is_prime) // If the number is prime, sum it up     
      {  s += i;             // optionally you can print the prime numbers too 
            printf("%d ",i);  
       }  
   }   
   printf("\n\nThe sum of the prime numbers = %d",s);
 }
//see though your program will work proper but if we have to sum up prime nos. between 3 to n
//where n verey big(though within the limits of int :-) )
// then the checkin condition from to n/2 will take lot of time
// suppose ur n==100 then it will check from n=2 to n=50
//so the no. of checking loops required for 100 will be 48
//where as if we go from n=2 to n= 10 we only require 8 loops so it will be much faster
// now think for the case of n=1000 wouldnt it will take lot of time

Last edited by shabbir; 29Nov2006 at 15:35.. Reason: Code formating.