Sunday, 28 June 2015

Solution To Project Euler Problem 3 in C

For those who are unable to solve the third problem of Project Euler which says:


The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

Here's the simple solution to in C language which merely took  25 lines.

#include <stdio.h>
main()
{
    long long int i,j,b,a=0;
    for(i=3; i<=600851475143/2; i++)
    {
        if(600851475143%i == 0)
        {
            for(j=2; j<=i/2; j++)
            {
                if(i%j == 0)
                {
                    a=1;
                    break;
                }            
            }
            if(a==0)
            {
                b=i;
            }
        }
      
    }
    printf("The sum of all multiples is %lld", b);
}



Although since this program involves large numbers, it too around 1 hour to execute.

The final answer which will come is  6857.

Hope it helps you guys.

0 comments:

Post a Comment