PE5 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?

https://projecteuler.net/problem=5

分析

可以将题目理解为求1-20的最小公倍数,即设计一个求多个数的最小公倍数函数。我将函数原型设为:__int64 Arr_Lcm(vector<int> v);

解法1(公倍数法):利用求两个最小公倍数方法:int Lcm(int a, int b);先求出其中两个数的最小公倍数,再求这个最小公倍数与第三个数的最小公倍数,依次求解

解法2(素数法):可以观察到:LCM(1~10)=    2 ^ 3 * 3 ^ 2 * 5 ^ 1 * 7 ^ 1 = 2520

“1.列出20以内的质数,并求这些质数的小于20的最高次幂

2.再将这些质数的最高次幂的积相乘”

ps:这个方法也是PE官网Download overview给出的方法,分析写了好长(看了半天没看懂),

后来结果看到@cfeibiao博客的这句话,秒懂了,就不贴这个方法的代码了,可以参考他博客中Python的实现。

Code

Lcm利用了求最大公约数(Gcd)的方法,和结论(两数之积 = Lcm*Gcd)此外还要注意返回时要用大于__int64,使用int,会发生了上溢,得到错误结果;

代码部分,顺便把Arr_Gcd的方法也写了,把最大公约数,最小公倍数 的方法给总结了。

#include <iostream>
#include <vector>

using namespace std;
int Lcm(int a, int b);
__int64 Arr_Lcm(vector<int> v);
int Gcd(int a, int b);
int Arr_Gcd(vector<int> v);
int main(){

    vector<int> v = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    __int64 r = Arr_Lcm(v);
    cout << r;
    return 0;
}
int Gcd(int a, int b){
    if (a < b)swap(a, b);

    while (b != 0)
    {
        int tmp = a % b;
        a = b;
        b = tmp;
    }
    return a;
}
int Lcm(int a, int b){
    int f = Gcd(a, b);
    return (__int64)a*b / Gcd(a, b);
}
__int64 Arr_Lcm(vector<int> v){
    __int64 a = v[0];
    __int64 b;
    for (int i = 1; i < v.size(); i++)
    {
        if (i == 18){
            int f = 1;
        }
        int tt = v[i];
        b = Lcm(a, v[i]);
        a = b;
    }
    return a;
}
int Arr_Gcd(vector<int> v){
    int a = v[0];
    int b;
    for (int i = 0; i < v.size(); i++)
    {
        b = Gcd(a, v[i]);
        a = b;
    }
    return a;
}

Mathematica  

LCM @@ Range[1, 20]

by the way C++的vector好像没有vector初始化为递增序列的,利用Mma、Range[1,20]粘贴复制真是爽,除此之外还可以做各种验证、计算来辅助分析!

用vs断点增删代码来测试数据,点F5实在麻烦。

Mma简直就是个超级计算器,没用过的同学,上面一行简洁的代码有没有打动你~~....!。

参考:cfeibiao

时间: 2024-10-25 21:17:28

PE5 Smallest multiple的相关文章

Smallest multiple

problem 5:Smallest multiple 题意:求最小的正数,使得其可以被1-20整除 代码如下: 1 #ifndef PRO5_H_INCLUDED 2 #define PRO5_H_INCLUDED 3 4 #include "prime.h" 5 6 namespace pro5{ 7 long long solve(){ 8 long long ans=1; 9 for(int i=2;i<=20;++i) 10 ans=lcm(ans,i); 11 ret

projecteuler 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整除的最小数. =====

PE 001~010

题意: 001(Multiples of 3 and 5):对小于1000的被3或5整除的数字求和. 002(Even Fibonacci numbers):斐波那契数列中小于等于4 000 000的偶数求和. 003(Largest prime factor):求600 851 475 143的最大质因数. 004(Largest palindrome product):求由两个三位数相乘得到的最大回文数. 005(Smallest multiple):求能被1~20中所有数整除的最小正整数.

Project Euler: Solution for Problem 5

Smallest multiple Problem 5 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? Source link: http

欧拉计划(python) problem 5

Smallest multiple Problem 5 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? python code : imp

欧拉项目005:最小公倍数

Smallest multiple Problem 5 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 所有数的最

Project-Euler problem 1-50

最近闲的做了下Project Euler 上的题目,前面50题都比较简单,简单总结下.代码一般是Python和C/C++的 用Python 做这些题目简直是酸爽啊 一下代码可能不一定是我的,因为不知道论坛里面的回复不是永久的,所以我的代码有的丢了,可能找个和我的意思相近的代码.题目翻译是从 欧拉计划 | Project Euler 中文翻译站上面Copy 的表告我. Problem 1  Multiples of 3 and 5 10以下的自然数中,属于3和5的倍数的有3,5,6和9,它们之和是

质因数的思维题

Alice and Bob begin their day with a quick game. They first choose a starting number X0?≥?3 and try to reach one million by the process described below. Alice goes first and then they take alternating turns. In the i-th turn, the player whose turn it

OpenCL OpenCL快速入门教程

OpenCL快速入门教程 原文地址:http://opencl.codeplex.com/wikipage?title=OpenCL%20Tutorials%20-%201 翻译日期:2012年6月4日星期一 这是第一篇真正的OpenCL教程.这篇文章不会从GPU结构的技术概念和性能指标入手.我们将会从OpenCL的基础API开始,使用一个小的kernel作为例子来讲解基本的计算管理. 首先我们需要明白的是,OpenCL程序是分成两部分的:一部分是在设备上执行的(对于我们,是GPU),另一部分是