본문 바로가기
Programming/Project Euler

5. Project Euler #5 : The smallest number that can be divided by numbers below 20.

by 작은별하나 2015. 1. 29.


The difficulty of the problem, I feel that a little bit goes high.


This is a problem that can exert sufficient efficiency.


The algorithm is easy,

As will be easily solved sequentially from 1 to find the first number evenly divisible by the number 1 through 20 for all you can, then you need to check on you when so many do.


Here, find the greatest common divisor by the Euclidean method, I think the algorithm has a product that can go the way done.


Finding the greatest common divisor by the Euclidean method, you can find an algorithm for the two hands of a small number N O (log N). So if you eventually expressed as a number between 20 N O (N log N) algorithm to find the answer.


If introduced algorithm may infer that the easiest answer a number appears roughly one million. 2, 3, 5, 7, 11, 13, 17, with a small number of prime factors of 2 are four 19, prime factor is Cause two out of three. If fact, you know all of the minority,



You can also find a formula called. Use of a simple algorithm to find a few you can efficiently find the answer than the first mentioned algorithms.


This program is for studying "Project Euler".  Please use this source for studying or advancing your programming skill.



int main()
{
        int n = 20;
        int c = 1;

        for( int i = 2 ; i <= n ; i++ )
        {
                if( c%i == 0 ) continue;
                int r = i;
                int s = c%i;
                while( r%s )
                {
                        int t = r%s;
                        r = s;
                        s = t;
                }
                c *= i/s;
        }
        printf(" ans(%d)="%d\n" n, c);
}


댓글