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); }
'Programming > Project Euler' 카테고리의 다른 글
7. Project Euler #7 : 10001st prime (0) | 2015.02.27 |
---|---|
6. Project Euler #6 : Sum square difference (0) | 2015.02.13 |
4. Project Euler #4 : Largest palindrome product. (0) | 2015.01.22 |
3. Project Euler #3 : Find the biggest prime factor. (0) | 2015.01.20 |
[C/C++] Project Euler #2 : Sum of even terms of Fibonacci(Dynamic Programming) (0) | 2015.01.20 |