Java 求1000以下3和5的倍数之和
好了,伙计们,我正在做Euler项目的挑战,我不敢相信我被困在了这个项目上。尽管我的代码看起来很实用,但我真的不明白为什么我会得到错误的答案:Java 求1000以下3和5的倍数之和,java,Java,好了,伙计们,我正在做Euler项目的挑战,我不敢相信我被困在了这个项目上。尽管我的代码看起来很实用,但我真的不明白为什么我会得到错误的答案: import java.util.ArrayList; public class Multithree { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Integer>
import java.util.ArrayList;
public class Multithree {
public static void main(String[] args) {
// TODO Auto-generated method stub
ArrayList<Integer> x = new ArrayList<Integer>();
ArrayList<Integer> y = new ArrayList<Integer>();
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);
}
}
import java.util.ArrayList;
公共类多重三{
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
ArrayList x=新的ArrayList();
ArrayList y=新的ArrayList();
int totalforthree=0;
int TOTALFOR5=0;
int-total=0;
用于(内部温度=0;温度<1000;温度++){
如果(临时%3==0){
x、 添加(临时);
总计三个+=温度;
}
}
用于(内部温度=0;温度<1000;温度++){
如果(临时%5==0){
y、 添加(临时);
总计五个+=温度;
}
}
总计=总计五个+总计三个;
System.out.println(“3或5到1000的倍数为:“+总数”);
}
}
我得到的答案是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;
}
}
for(int-temp=0;temp<1000;temp++){
如果(临时%3==0){
x、 添加(临时);
总计三个+=温度;
}否则如果(临时%5==0){
y、 添加(临时);
总计五个+=温度;
}
}
有些数字要数两次。您需要做的是在一个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);
}
publicstaticvoidmain(字符串[]args){
整数和=0,i=0,j=0;
对于(i=0;i好的,所以这不是最好看的代码,但它完成了任务
public class Multiples {
public static void main(String[]args) {
int firstNumber = 3;
int secondNumber = 5;
ArrayList<Integer> numberToCheck = new ArrayList<Integer>();
ArrayList<Integer> multiples = new ArrayList<Integer>();
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<multiples.size(); i++) {
sumOfMultiples += multiples.get(i);
}
System.out.println(multiples);
System.out.println("Sum Of Multiples: " + sumOfMultiples);
}
}
公共类倍数{
公共静态void main(字符串[]args){
int firstNumber=3;
int secondNumber=5;
ArrayList numberToCheck=新建ArrayList();
ArrayList multiples=新的ArrayList();
整数乘和=0;
对于(int i=0;i<1000;i++){
编号检查。添加(i);
if(numberToCheck.get(i)%firstNumber==0 | | numberToCheck.get(i)%secondNumber==0){
multiples.add(numberToCheck.get(i));
}
}
对于(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( final String[] args ) {
// TODO Auto-generated method stub
ArrayList<Integer> x = new ArrayList<Integer>();
ArrayList<Integer> y = new ArrayList<Integer>();
int totalforthree = 0;
int totalforfive = 0;
int number = 8;
int total = 0;
for ( int temp = 1; temp <= number; temp++ ) {
if ( temp % 3 == 0 ) {
x.add( temp );
totalforthree += temp;
}
else if ( temp % 5 == 0 ) {
y.add( temp );
totalforfive += temp;
}
}
total = totalforfive + totalforthree;
System.out.println( "multiples of 3 : " + x );
System.out.println( "multiples of 5 : " + y );
System.out.println( "The multiples of 3 or 5 up to " + number + " are: " + total );
}
}
package com.parag;
/*
*@作者帕拉格·萨塔夫
*/
公共类多重条件{
/**
*@param args
*/
公共静态void main(最终字符串[]args){
//TODO自动生成的方法存根
ArrayList x=新的ArrayList();
ArrayList y=新的ArrayList();
int totalforthree=0;
int TOTALFOR5=0;
整数=8;
int-total=0;
对于(int-temp=1;temp难道你们不认为我们不用循环来计算倍数之和,我们可以用一个简单的
我使用循环和公式对结果进行了评估。循环对较短的数据范围很有价值。但是,当数据范围增长超过1010时,程序使用循环处理结果要花费数小时以上。但是,使用简单的算术级数公式时,同样的方法以毫秒为单位对结果进行评估
我们真正需要做的是:
算法:
计算3的倍数之和,并将其相加
计算5的倍数之和,并将其相加
计算3*5=15的倍数之和,然后从总和中减去
下面是我博客文章中的java代码片段
我是如何解决这个问题的,我取了一个整数值(初始化为零),然后继续加上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;
}
private static int getSum(){
整数和=0;
对于(int i=1;i<1000;i++){
如果(i%3==0 | | i%5==0){
总和+=i;
}
}
回报金额;
}
我用几种方法多次这样做。完成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;
}
公共类倍数f3和5{
公共静态void main(字符串[]args){
System.out.println(“3和5的倍数之和为:“+getSum()”);
}
私有静态int getSum(){
整数和=0;
对于(int i=1;i<1000;i++){
如果(i%3==0 | | i%5==0){
总和+=i;
}
}
回报金额;
}
如果有人建议减少代码行数,请告诉我您的解决方案。我是编程新手。如果您使用的是Java 8,您可以通过以下方式完成:
Integer sum = IntStream.range(1, 1000) // create range
.filter(i -> i % 3 == 0 || i % 5 == 0) // filter out
.sum(); // output: 233168
要计算可被3
和5
整除的数字两次,您可以
将上述行写入两次或.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
从数学的角度来看,
你没有考虑3到5之间的共同因素。
因为有双重计数。
ex;编号15,
30 ,
45 ,
60 ,
75 ,
90 ,
105 ,
120 ,
135 ,
150 ,
165 ,
180 ,
195 ,
210 ,
225 ,
240 ,
255 ,
270 ,
285 ,
300 ,
315 ,
330 ,
345 ,
360 ,
375 ,
390 ,
405 ,
420 ,
435 ,
450 ,
465 ,
480 ,
495 ,
510 ,
525 ,
540 ,
555 ,
570 ,
585 ,
600 ,
615 ,
630 ,
645 ,
660 ,
675 ,
690 ,
705 ,
720 ,
735 ,
750 ,
765 ,
780 ,
795 ,
810 ,
825 ,
840 ,
855 ,
870 ,
885 ,
900 ,
915 ,
930 ,
945 ,
960 ,
975 ,
990是常见因素。
共有因素总计=33165。
你的答案是266333
正确答案是233168。
你的答案-共有因素
266333-33165=233168
(这是获取公因数和公因数总和的代码)
publicstaticvoidmain(字符串[]args)
Integer sum = IntStream.range(1, 1000) // create range
.filter(i -> i % 3 == 0 || i % 5 == 0) // filter out
.sum(); // output: 233168
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
public static void main(String[] args) {
System.out.println("The sum of the Common Factors : " + getCommonFactorSum());
}
private static int getCommonFactorSum() {
int sum = 0;
for (int i = 1; i < 1000; i++) {
if (i % 3 == 0 && i % 5 == 0) {
sum += i;
System.out.println(i);
}
}
int count = 0;
for (int i = 1; i <= 1000 / 3; i++)
{
count = count + (i * 3);
if (i < 1000 / 5 && !(i % 3 == 0))
{
count = count + (i * 5);
}
}
int multiply3_5(int max)
{
int i, x3 = 0, x5 = 0, x15 = 0;
for(i = 3; i < max; i+=3) x3 += i; // Store all multiples of 3
for(i = 5; i < max; i+=5) x5 += i; // Store all multiples of 5
for(i = 15; i < max; i+=15) x15 += i; // Store all multiples 15;
return x3+x5-x15;
}
int multiply3_5(int max)
{
int x3 = (max - 1) / 3;
int x5 = (max - 1) / 5;
int x15 = (max - 1) / 15;
int sn3 = (x3 * (x3 + 1)) / 2;
int sn5 = (x5 * (x5 + 1)) / 2;
int sn15 = (x15 * (x15 + 1)) / 2;
return (3*sn3) + (5 *sn5) - (15*sn15);
}
public class SumMultiples2And5 {
public static int multiply3_5_complexityN(int max){
int i, x3 = 0, x5 = 0, x15 = 0;
for(i = 3; i < max; i+=3) x3 += i; // Store all multiples of 3
for(i = 5; i < max; i+=5) x5 += i; // Store all multiples of 5
for(i = 15; i < max; i+=15) x15 += i; // Store all multiples 15;
return x3 + x5 - x15;
}
public static int multiply3_5_constant(int max){
int x3 = (max - 1) / 3;
int x5 = (max - 1) / 5;
int x15 = (max - 1) / 15;
int sn3 = (x3 * (x3 + 1)) / 2;
int sn5 = (x5 * (x5 + 1)) / 2;
int sn15 = (x15 * (x15 + 1)) / 2;
return (3*sn3) + (5 *sn5) - (15*sn15);
}
public static void main(String[] args) {
System.out.println(multiply3_5_complexityN(1000));
System.out.println(multiply3_5_constant(1000));
}
}
233168
233168