Problem5-Project Euler

Smallest multiple

 

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?

考虑质因数分解,将1-20都分解,挑质因数的最高幂次相乘即可。

 1 #include"stdio.h"
 2 #include"math.h"
 3 
 4 #define N 20
 5 
 6 int prime[N+1];
 7 
 8 int find_prime(int x)
 9 {
10     int i,j,sum=1;
11     for(i=1;i<=x;i++)
12     {
13         prime[i]=1;
14         for(j=2;j<=int(sqrt(i));j++)
15             if(i%j==0)
16                 prime[i]=0;
17         if(prime[i]!=0&&i>=2)
18         {
19             prime[i]=int(log(x)/log(i));
20             sum *= int(pow(i,prime[i]));
21         }
22     }
23     return(sum);
24 }
25 
26 int main()        /*problem5-smallest multiple*/
27 {
28     printf("answer is %d
",find_prime(N));
29     return(0);
30 }
View Code
原文地址:https://www.cnblogs.com/redlogic/p/8545610.html