找到低于1000的3和5的倍数之和

好的,所以我正在进行欧拉计划的挑战,我无法相信我会遇到第一个挑战 。 尽管我的代码看起来很实用,但我真的不明白为什么我得错了答案:

import java.util.ArrayList; public class Multithree { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList x = new ArrayList(); ArrayList y = new ArrayList(); int totalforthree = 0; int totalforfive = 0; int total =0; for(int temp =0; temp < 1000 ; temp++){ if(temp % 3 == 0){ x.add(temp); totalforthree += temp; } } for(int temp =0; temp < 1000 ; temp++){ if(temp % 5 == 0){ y.add(temp); totalforfive += temp; } } total = totalforfive + totalforthree; System.out.println("The multiples of 3 or 5 up to 1000 are: " +total); } } 

我得到答案为266333,它说这是错的……

你应该使用相同的for循环来避免两个都是两倍的重复计数。 比如15,30 …

  for(int temp =0; temp < 1000 ; temp++){ if(temp % 3 == 0){ x.add(temp); totalforthree += temp; }else if(temp % 5 == 0){ y.add(temp); totalforfive += temp; } } 

如果您使用的是Java 8,则可以通过以下方式执行此操作:

 Integer sum = IntStream.range(1, 1000) // create range .filter(i -> i % 3 == 0 || i % 5 == 0) // filter out .sum(); // output: 233168 

要计算可被35两次整除的数字,您可以将上面的行写入两次,或者.map()2 * i值写入:

 Integer sum = IntStream.range(1, 1000) .filter(i -> i % 3 == 0 || i % 5 == 0) .map(i -> i % 3 == 0 && i % 5 == 0 ? 2 * i : i) .sum(); // output: 266333 

你们不是都认为不是使用循环来计算倍数之和,而是使用简单的算术级数公式来计算n个项的和,以计算n个项的和 。

我使用循环和公式评估了结果。 循环对于缩短数据范围非常有用。 但是当数据范围增长超过10时,程序需要花费数小时来处理循环结果。 但是,当使用简单的算术级数公式时,同样会以毫秒为单位评估结果。

我们真正需要做的是:
算法:

  1. 计算3的倍数之和并加上sum。
  2. 计算5的倍数之和并加上sum。
  3. 计算3 * 5 = 15的倍数之和并从总和中减去。

以下是来自我的博客CodeForWin的 Java中的代码片段- Project Euler 1:Multiples of 3和5

 n--; //Since we need to compute the sum less than n. //Check if n is more than or equal to 3 then compute sum of all divisible by //3 and add to sum. if(n>=3) { totalElements = n/3; sum += (totalElements * ( 3 + totalElements*3)) / 2; } //Check if n is more than or equal to 5 then compute sum of all elements //divisible by 5 and add to sum. if(n >= 5) { totalElements = n/5; sum += (totalElements * (5 + totalElements * 5)) / 2; } //Check if n is more than or equal to 15 then compute sum of all elements //divisible by 15 and subtract from sum. if(n >= 15) { totalElements = n/15; sum -= (totalElements * (15 + totalElements * 15)) / 2; } System.out.println(sum); 

我如何解决这个问题是我取一个整数值(初始化为零)并继续添加i的递增值,如果它的模数为3或5则给出零。

 private static int getSum() { int sum = 0; for (int i = 1; i < 1000; i++) { if (i % 3 == 0 || i % 5 == 0) { sum += i; } } return sum; } 

我这样做了好几次。 完成Java所需代码的最快,最简洁和最简单的方法是:

 public class MultiplesOf3And5 { public static void main(String[] args){ System.out.println("The sum of the multiples of 3 and 5 is: " + getSum()); } private static int getSum() { int sum = 0; for (int i = 1; i < 1000; i++) { if (i % 3 == 0 || i % 5 == 0) { sum += i; } } return sum; } 

如果有人建议将其缩减到更少的代码行,请告诉我您的解决方案。 我是编程新手。

你在计算一些数字两次。 你需要做的是在for循环中添加一个,并使用if-else语句,如果你发现3的倍数,你也不会在5中计算它们。

  if(temp % 3 == 0){ x.add(temp); totalforthree += temp; } else if(temp % 5 == 0){ y.add(temp); totalforfive += temp; } 

上面给出的逻辑显示错误的答案,因为3和5的倍数用于计算。 在上面的逻辑中有一些东西被遗漏,即15,30,45,60 ……是3的倍数,也是5的倍数。然后我们需要在添加时忽略它。

  public static void main(String[] args) { int Sum=0, i=0, j=0; for(i=0;i<=1000;i++) if (i%3==0 && i<=999) Sum=Sum+i; for(j=0;j<=1000;j++) if (j%5==0 && j<1000 && j*5%3!=0) Sum=Sum+j; System.out.println("The Sum is "+Sum); } 

好的,所以这不是最好看的代码,但它完成了工作。

 public class Multiples { public static void main(String[]args) { int firstNumber = 3; int secondNumber = 5; ArrayList numberToCheck = new ArrayList(); ArrayList multiples = new ArrayList(); int sumOfMultiples = 0; for (int i = 0; i < 1000; i++) { numberToCheck.add(i); if (numberToCheck.get(i) % firstNumber == 0 || numberToCheck.get(i) % secondNumber == 0) { multiples.add(numberToCheck.get(i)); } } for (int i=0; i 

如果数字是10,则3的倍数是3,6,9,5的倍数是5,10总和是33,程序给出相同的答案:

 package com.parag; /* * @author Parag Satav */ public class MultipleAddition { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub new MultipleAddition().getSum(99); } public void getSum(int number) { int sum = 0; int index; int index_5; if (number > 0) { index = number / 3; for (int i = 1; i <= index; i++) { sum = sum + 3 * i; } index_5 = number / 5; for (int i = 1; i <= index_5; i++) { sum = sum + 5 * i; } System.out.println(sum); } } } 
 public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while (t>0){ int sum = 0; int count =0; int n = sc.nextInt(); n--; System.out.println((n/3*(6+(n/3-1)*3))/2 + (n/5*(10+(n/5-1)*5))/2 - (n/15*(30+(n/15-1)*15))/2); t--; } } }