Oxaric’s Blog

A compendium of amazing things…

Euler Project Problem #10 Solution

Posted by oxaric on November 24, 2008

It uses C.


Click to directly download euler-solution-10.c

// filename 'euler-solution-10.c'
// By: Louis Casillas, oxaric@gmail.com

// Euler Problem #10
// Find the sum of all the primes below two million.

#include <stdio.h>
#include <math.h>

char isPrime( int num )
{   
   if ((num % 3) == 0 )
   {
      return 0;
   }
   

   int i = 0;

   int r = floor ( sqrt( num ) );
   
   int f = 5;

   while (<= r)
   {
      if ((num % f) == 0)
      {
         return 0;
      }

      if ((num % (+ 2)) == 0)
      {
         return 0;
      }

      f += 6;
   }

   return 1;
}

#define UPTO 2000000

int main()
{
   int i = 5;
   double sum_of_primes = 5;

   while (< UPTO)
   {
      if ( isPrime( i ) )
      {         
         sum_of_primes += i;
      }

      i += 2;
   }

   printf( "The sum of the primes is: %f", sum_of_primes );

   return 0;
}

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.