欧拉计划问题5 学习笔记 matlab 求:被1-20整除的数

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. 求:被1-20整除的数

思路

  1. 被n整除——rem函数
  2. n循环-20——for函数

代码

k=[];  
for i=232792500:1:232793000;%算了很久,上亿了(开始从1开始,算了太久,搜的答案,代码没错,可以用1-5整除试验
    n=1:1:20;               %i除以 n ;整除就n+1;都整除就纳入到矩阵k中 
       if (rem(i,n)==0);    %将i除以n,120。
           n=n+1; 
           k=[k,i];         %放到if循环下面,记录的是递增的i;只有放在这行,走的程序才是if里面的i 先对一个i进行循环20次
        end
       
      i=i+1;
end

答案

str = 232792560
PS:计算太久但是代码是对的,就去搜一下答案把,本来没想到要运算那么久,试范围试了很久。

你可能感兴趣的:(欧拉计划问题5 学习笔记 matlab 求:被1-20整除的数)