Project Euler__problem 5

来源:互联网 发布:家具软件管理 编辑:程序博客网 时间:2024/05/17 10:57

Problem 5


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?


最小倍数

2520是最小的能够被1到10整除的数。

最小的能够被1到20整除的正数是多少?



这道题目因为我用笔算过,知道它的大概思路

所以趁着今天有空坐下看看能不能解决

首先看看给出的条件2520;

因为有之前做的一道题质因数的经验

我就将他拆分成质因数的乘积

2520=2X2X2X3X3X5X7

所以只需将11-20中的质因数 包含在内即可

x=2 X 2 X 2 X 3 X 3 X 5 X 7    X 11 X 13 X 2 X 17 X 19


但将其转化为计算机语言怎么转化

首先我观察到在10之内都是由质因数的n次方组成

比如2的3次方小于10

3的平方小于10

然后同理 知20的话最大为2的4次方 3的平方 其他质数均为1次方

所以将他们的次方乘积就是所要的结果


#include<iostream>#include<math.h>int zhishu(int num){int a;for (a = num / 2; a > 1; a--)if (num%a== 0)break;else continue;if (a == 1)return 1;else return 0;}int main(){int i;int mul = 1, sq;for (i = 2; i <= 10; i++)        if(zhishu(i)!=0){sq = 1;while (sq<10){sq = i*sq;}sq = sq/i;mul = mul*sq;}else continue;std::cout << mul<<"是最小整数"<<std::endl;system("pause");}

把10改为20就可以得到结果232792560

之前尝试用pow但出现错误 原因是pow只能用double类型哭