简体   繁体   English

试图找到可被1到20的所有数字均分的最小正数

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

I'm trying to find the smallest positive number that is evenly divisible by all of the numbers from 1 to 20. We are given that 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. 我试图找到最小的正数,该数可以被1到20的所有数均分。我们认为2520是可以除以1到10的每个数而没有任何余数的最小数。 My find() finds the number starting from 2520 that is divisible by all numbers from 1-20 but is returning 2520 for some reason. 我的find()查找从2520开始的数字,该数字可被1-20的所有数字整除,但由于某种原因返回2520。 I cannot find what's wrong about my find()? 我找不到我的find()有什么问题吗?

  public class Solution  {

  public ArrayList<Integer> list = new ArrayList<Integer>();


// creating a list of integers from 1 to 20
public ArrayList<Integer> addtolist() {
  for (int i = 1; i <= 20; i++) {
    list.add(i);
  }
  return list;
}

// finds the smallest positive number that is evenly divisible by all 
of the numbers from 1 to 20

public int find() {
  int num = 2520;
  while(true) {
    for(int i: list) {
      if(num % i == 0) {
        return num;
      }
      else {
        num = num + 1;
      }

    }
  }
}

public static void main(String[] args) {
  Solution sol = new Solution();
  sol.addtolist();
  System.out.println(sol.find());//2520
}


}

Your find function returns num if any i in list divides it. 如果list 任何 i将其分割,则find函数将返回num It should only return num if every i in num is a divisor. 它应该只返回num如果每一个 inum是一个除数。

Although it has to be said that this is far from the most efficient solution to the problem. 尽管必须说,这远非最有效的解决方案。

You return from the for loop when (num % i == 0) , given that i starts at 1 this always true. (num % i == 0) ,您将从for循环中返回,因为我始终从1开始。 Instead you need to wait until the end to return: 相反,您需要等到最后返回:

public int find() {
  int num = 2520;
  while(true) {
    boolean allDivisible = true;
    for(int i: list) {
      if(num % i != 0) {
        allDivisible = false;
        break;
      }
    }
    if (allDivisible) {
      return num;
    else {
      num = num + 1;
    }
  }
}

In your code: 在您的代码中:

for(int i: list) {
  if(num % i == 0) {
    return num; // Returns immediately. 
  }
  else {
    num = num + 1;
  }
}

you return as soon as you find some number in the list that has a match. 在列表中找到匹配的数字后,您将立即返回。 What you want to do is only return when you have found a value that matches all in the list. 您只想在找到与列表中的所有值都匹配的值时才返回。

Nice question! 好问题!

long answer = LongStream.iterate(1, n -> n + 1)
    .filter(n -> IntStream.rangeClosed(1, 20).allMatch(f -> n % f == 0))
    .findFirst().getAsLong();

Answer is 232792560 答案是232792560

There are obviously plenty of shortcuts using math (eg only looking at even numbers, ignoring numbers in 1 to 20 that are factors of other numbers in that range). 显然,使用数学有很多捷径(例如,仅查看偶数,而忽略1到20中的数字,这是该范围内其他数字的因数)。

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

相关问题 能被1到20的所有数均分的最小正数是多少? - What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? 能被 1 到 100 的所有数整除的最小正数是多少? - What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 100? 我想要什么,因为 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 如何找到可整除的数? - How to find evenly divisible numbers? 尝试使用For Loop查找20个用户输入的数字中的最大和最小数字 - Trying to find biggest and smallest number in 20 user entered numbers using For Loop 是否有更好的迭代方法来找到均匀可分的数字? - Is there better way of iteration to find the evenly divisible number? 找到数组中最小的正数 - find smallest positive number in an array 查找由两位数字除以给定数字所形成的最小数字 - Find smallest number formed by two digits divisible by given number 找到可被 N 整除的最小数字,数字总和为 N - find smallest number divisible by N with sum of digits as N
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM