Sunday, 28 June 2015

Solution to Project Euler Problem 5 in C

Even I got stuck at one point while solving this problem but finally got it right. So here's what the fifth problem said on Project Euler:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

And here's the 22 line solution to this:


#include <stdio.h>
main()
{
    long int i,j,a;
    for(i=21; i<999999999; i++)
    {
        a=0;
        for(j=2; j<21; j++)
        {
            if(i%j != 0)
            {
                a=1;
                break;
            }
        }
        if(a==0)
        {
            printf("Number is %ld", i);
            break;
        }

    }
}



It took just 2.007 seconds for the program to execute giving the final answer 232792560.

0 comments:

Post a Comment