Pages

February 24, 2011

Project Euler 5



ProblemWhat is the smallest number divisible by each of the numbers 1 to 20?

Solution: [Spoiler Ahead]


              I just checked for numbers that are divisible by all numbers between 11 and 20(inclusive), this algo is not much efficient but works well.

Toggle Code


#include <stdio.h>;
main() 
{ 
    int i; 
    int smallest=0,found=0; 

    for(smallest=20;found < 1;smallest++){ 
        for(i=20;i > 10 ;i--){ 
            if(smallest%i != 0){ 
                found = 0; 
                break; 
            } 
        } 
        if(i == 10){ 
            found = 1; 
            break; 
        } 
    } 
    printf("%d\n",smallest); 
} 

Enjoy coding :-)

No comments:

Post a Comment