简体   繁体   English

能被1到20的所有数均分的最小正数是多少?

[英]What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

This is my code 这是我的代码

class Euler5
{
 public static void main(String args[])
 {
 long i=2520l;
 int flag= 1;
 if(flag == 1)
 {
  search:
  for(int j=10;j<=20;j++)
   {
    long a=i%j;
    if(a!=0)
    {
     i+=20;
     break search ;
    }
    if(j==20)
    {
    flag=0;
    }
   }
 }
 System.out.print(i);

   }
 }

It always print 2540. I found other way to solve it. 它总是打印2540。我发现了另一种解决方法。 But what is hitting me is why is this not working?? 但是令我震惊的是为什么这不起作用?

Replace if(flag == 1) with while (flag == 1) . while (flag == 1)替换if(flag == 1) while (flag == 1) Otherwise you will only ever going to iterate once over the 11 values. 否则,您将只能对11个值进行一次迭代。

It is because: 这是因为:

  1. after first iteration, you are increasing j by 1 . 第一次迭代后,将j增加1
  2. on the second iteration i % j is != 0 . 在第二次迭代中, i % j!= 0
  3. in if condition, you are incrementing i by 20 causing i = 2540 . 在if条件下,您将i递增20导致i = 2540
  4. you are breaking the loop there. 你正在打破循环。
  5. and hence the result is 2540 . 因此,结果是2540

It because when your for loop execute second time value of j will be 11 which leads to your expression to 这是因为当您执行for循环时,第二次j值将为11 ,这导致您的表达式变为
long a=2520/11
which set value of a to 1 which not satisfied your if condition and it enter your if condition 将a的值设置a to 1不满足您的if条件,并输入您的if条件

if(a!=0)
{
   i+=20;
   break search ;
}  

after that it will break your for loop and terminate your execution which give output 2540 always. 之后,它将中断您的for循环并终止您的执行,始终给出输出2540。

your approach doesn't seems to be the right one. 您的方法似乎不正确。 Your for have to be from 1 to 20 not 10 to 20. And you should run all this in a while loop as Ivaylo Strandjev said. 您的for必须从1到20,而不是10到20。并且您应该像Ivaylo Strandjev所说的那样在while循环中运行所有这些。 Maybe a simpler solution is to divide 2 to 20 numbers into prime factor and add unique factors 也许一个更简单的解决方案是将2到20个数字除以质数并添加唯一因数

eg : 例如:

2 => 1 * 2
3 => 1 * 3
4 => 1 * 2 * 2
5 => 1 * 5
6 => 1 * 2 * 3

Then you keep unique one 那你就保持独一无二

1 * 2 => keep both
1 * 3 => keep only 3
1 * 2 * 2 => keep only the second 2
1 * 5 => keep only 5
1 * 2 * 3 => keep nothing

total 1 * 2 * 3 * 2 * 5 = 60 is the smallest int divisible by 2,3,4,5 and 6

You'll find 17,907,120 as your solution 您会找到17,907,120作为解决方案

I'm not good at Java :( 我不擅长Java :(

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 能被 1 到 100 的所有数整除的最小正数是多少? - What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 100? 试图找到可被1到20的所有数字均分的最小正数 - Trying to find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20 我想要什么,因为 output 是可以被 1 到 20 的所有数字整除的最小正值? - what I want as output is the smallest positive value that can be evenly divisible by all the numbers from 1 to 20? Project Euler #5(可被 1 到 20 的所有数字整除的最小正数):优化方法? ~爪哇 - Project Euler #5(Smallest positive number divisible by all numbers from 1 to 20): Ways to Optimize? ~Java 编写一个 while 循环,打印所有可被 10 整除且小于给定数 n 的正数 - Write a while loop that prints all positive numbers that are divisible by 10 and less than a given number n 如何找到可整除的数? - How to find evenly divisible numbers? 检查一个数是否是偶数并且可以被 3 或 7 整除 - Checking if a number is even and evenly divisible by 3 or 7 显示最大可除数而不是所有数字 - Showing largest divisible number instead of all numbers 数组中所有数字都可以被2整除的次数 - Number of times all the numbers in an array are divisible by 2 是否有更好的迭代方法来找到均匀可分的数字? - Is there better way of iteration to find the evenly divisible number?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM