`

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

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
  
#define N 20
  
int gcd(int a, int b)
{
  if(b==0)
    return a;
  else
    return gcd(b,a%b);
}
  
int lcm(int a, int b)
{
  return a/(gcd(a,b))*b;
}
  
  
void solve()
{
   int i,s=2;
   for(i=3; i<=N; i++)
   {
      s=lcm(s,i);
   }
   printf("%d\n",s);
}
  
int main()
{
  solve();
  return 0;
}

 

Answer:
232792560

 

分享到:
评论

相关推荐

    HDU1019(2028)解题报告

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which is divisible by all the numbers in the set. For example, the LCM of 5, 7 and 15 is 105. Input Input...

    ones.rar_The Given_acm ones_ones a_ones acm_ones.cp

    ACM试题Problem K:Ones ...= 10000 not divisible by 2 or 5, some multiple of n is a number which in decimal notation is a sequence of 1 s. How many digits are in the smallest such a multiple of n?

    ProjectEuler:5个ProjectEuler问题及其解决方案

    ### 问题5:Smallest multiple **问题描述**: 找出所有1到20的正整数的最小公倍数。 **解决方案**: 可以通过分解每个数的质因数,然后取每个质因数的最大幂次作为最小公倍数。 ```javascript function lcm...

    计算机网络第六版答案

    5. Wifi (802.11): home and enterprise: 6. 3G and 4G: wide-area wireless. HFC bandwidth is shared among the users. On the downstream channel, all packets emanate from a single source, namely, the ...

    Algorithms and Data Structures - Niklaus Wirth

    - **Sorting by Straight Selection**: Selects the smallest (or largest) element and moves it to its final position. - **Sorting by Straight Exchange**: Exchanges adjacent elements if they are out of ...

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - ADD: Delphi/CBuilder XE5 now supported. - ADD: Delphi/CBuilder XE4 now supported. - ADD: Delphi/CBuilder XE3 now supported. - ADD: Delphi/CBuilder XE2 now supported. - ADD: Delphi/CBuilder XE now ...

Global site tag (gtag.js) - Google Analytics