打印出可被其他数字整除的数字的 Java 程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26700089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java program that prints out numbers that are divisible by other numbers
提问by user3475581
I have a program that reads two real numbers and then it prints out all the numbers between these two, that are divisible by 2 or 3 or 5. The program works fine, but when a user enters two really large numbers (for example, 1122222123333 and 214123324434434) the program takes a very long time to calculate the result. I would like to somehow fix the program, so that even for large numbers the result would be printed out instantly.
我有一个程序可以读取两个实数,然后打印出这两个实数之间的所有数字,这些数字可以被 2 或 3 或 5 整除。该程序运行良好,但是当用户输入两个非常大的数字时(例如,1122222123333和 214123324434434)该程序需要很长时间来计算结果。我想以某种方式修复程序,以便即使对于大数字,结果也会立即打印出来。
here is my code so far :
到目前为止,这是我的代码:
import java.util.Scanner;
public class Numbers
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
long x = sc.nextLong(); // first number input
long y = sc.nextLong(); // second number input
long num = 0; // new variable num -- means all the numbers between these to given input numbers
long count = 0; // loop counter - how many numbers are divided by 2 or 3 or 5
for (num = x; x <= num && num <= y; num++) {
if (num % 2 == 0 | num % 3 == 0 | num % 5 == 0) {
count = count + 1; // increasing the counter by 1, so that every time the loop repeats, the counter increases...
}
}
System.out.println(count); // prints out how many numbers are divided by 2 or 3 or 5 ...
}
}
回答by Eran
Well, you don't need a loop at all.
好吧,你根本不需要循环。
You know that the number of numbers between x and y that are divisible by 2 is (y-x)/2 (plus minus one).
Similarly the number of numbers between x and y that are divisible by 3 is (y-x)/3 (plus minus one).
And the number of numbers between x and y that are divisible by 5 is (y-x)/5 (plus minus one).
您知道 x 和 y 之间可被 2 整除的数字的数量是 (yx)/2(加减一)。
类似地,x 和 y 之间可被 3 整除的数为 (yx)/3(加减一)。
而 x 和 y 之间能被 5 整除的数是 (yx)/5(加减一)。
You just have to remove the numbers you counted more than once.
您只需要删除多次计算的数字。
If you consider groups A, B & C, the groups of numbers divisible by 2, 3 and 5 (in the required range) respectively, your goal is to find :
如果您考虑 A、B 和 C 组,即分别可被 2、3 和 5(在所需范围内)整除的数字组,您的目标是找到:
|A union B union C| = |A| + |B| + |C| - |A intersection with B| - |A intersection with C| - |B intersection with C| + |A intersection with B intersection with C|
|A 联合 B 联合 C| = |A| + |B| + |C| - |A与B的交点| - |与C的交点| - |B与C的交点| + |A 与 B 与 C 的交点|
Therefore, you have to subtract the numbers divisible by 2*3, the numbers divisible by 2*5 and the numbers divisible by 3*5. Finally, you have to add the numbers divisible by 2*3*5.
因此,您必须减去可被 2*3 整除的数、可被 2*5 整除的数和可被 3*5 整除的数。最后,您必须添加可被 2*3*5 整除的数字。
Example :
例子 :
between 1000 and 2000 there are about (2000-1000)/2 = 500 numbers divisible by 2 : 1000,1002,1004,...,2000. Actually, the count is off by 1, since it's 501 and not 500, but you can adjust for that by adding some logic that checks the edges of the range.
在 1000 和 2000 之间,大约有 (2000-1000)/2 = 500 个可被 2 整除的数字:1000,1002,1004,...,2000。实际上,计数减 1,因为它是 501 而不是 500,但是您可以通过添加一些检查范围边缘的逻辑来调整它。
similarly, there are about (2000-1000)/3 = 333 numbers divisible by 3 : 1002, 1005, 1008, ..., 1998.
同样,大约有 (2000-1000)/3 = 333 个可被 3 整除的数:1002、1005、1008、...、1998。
And about (2000-1000)/5 = 200 numbers divisible by 5 : 1000,1005,1010,...,2000. Here the count is again off by one.
大约 (2000-1000)/5 = 200 个可被 5 整除的数:1000,1005,1010,...,2000。这里计数再次减一。
回答by Gyro Gearloose
Not really an answer, but with my low reputation score I am not permitted to post a comment.
不是真正的答案,但由于我的声誉得分很低,我不允许发表评论。
If a number is divisible by 2, or 3, or 5 does not change if you add or subtract 30 (= 2* 3* 5) from that number. So you can count the matching numbers from 1 to 30, then compute how many blocks of 30 numbers are in your range, and then count the matching numbers that are not covered by those blocks.
如果一个数能被 2、3 或 5 整除,如果您从该数中加上或减去 30 (= 2* 3* 5),则该数不会改变。所以你可以从 1 到 30 计算匹配的数字,然后计算你的范围内有多少个 30 数字块,然后计算那些块没有覆盖的匹配数字。