项目欧拉#1

时间:2014-05-15 02:46:33

标签: javascript

我三天前才开始编程,而且我一直在使用Javascript。我一直在努力解决Project Euler Problem 1

  

查找低于1000的3或5的所有倍数的总和。

我觉得我的程序应该可行,但我的答案仍然略有偏差。谁能告诉我我的代码有什么问题?

var threeSum = [];
var threeTotal = 0;
var fiveSum = [];
var fiveTotal = 0;
for (i = 0; i < 1000; i += 3) {
    threeSum.push(i);
}
for (j = 0; j < threeSum.length; j++) {
    threeTotal = threeTotal + threeSum[j];
}
for (a = 0; a < 1000; a += 5) {
    fiveSum.push(a);
}
for (r = 0; r < fiveSum.length; r++) {
    fiveTotal = fiveTotal + fiveSum[r];
}
console.log(threeTotal + fiveTotal);

当我运行这个程序时,我得到了266333的答案。

7 个答案:

答案 0 :(得分:2)

Project Euler #1的任务是

  

查找低于1000的3或5的所有倍数的总和。

不是

  

求出低于1000的所有3的倍数和低于1000的所有5的倍数。

如45的数字是3和5的倍数,不应计算两次。

答案 1 :(得分:0)

Project Euler 1

  

如果我们列出10以下的所有自然数,它们是3或3的倍数   5,我们得到3,5,6和9.这些倍数的总和是23.找到   所有3或5的倍数的总和低于1000。

由于您正在学习,我假设您不希望得到明确的答案。

想想你的总结。 3循环的前几个术语是

[3, 6, 9, 12, 15]

和5循环的前几个术语是

[5, 10, 15]

由于你计算两次3和5的倍数,你的答案大于应该

答案 2 :(得分:0)

旁注 - 编程通常也意味着搜索非显而易见的解决方案。问题的措辞建议将数字从1到999循环,但是通过一些数学计算,根本不需要循环,你可以解决n = 1.000.000.000的问题,与n = 10一样快(如果没有溢出)

var n = 1000;

var multiplesOfThree        = Math.floor((n - 1) /  3);
var multiplesOfFive         = Math.floor((n - 1) /  5);
var multiplesOfThreeAndFive = Math.floor((n - 1) / 15);

var sum =  3 * multiplesOfThree        * (multiplesOfThree        + 1) / 2
        +  5 * multiplesOfFive         * (multiplesOfFive         + 1) / 2
        - 15 * multiplesOfThreeAndFive * (multiplesOfThreeAndFive + 1) / 2;

答案 3 :(得分:0)

this is pretty much how I did it and I get the right answer. 

`var threeSum=0;` 
 var fiveSum=0;
 var dSum=0;

for (var b=3; b<1000; b++) {

     if (b%3===0) {
        threeSum=threeSum+b; 

     }
}

for (var c=5; c<1000; c++) {
    if (c%5===0) {
       fiveSum=fiveSum+c;


    }
}

for  (var d=15; d<1000; d++) {
       if (d%15===0) {
          dSum=dSum+d;

    }

}
console.log((threeSum+threeSum)-dSum);

答案 4 :(得分:0)

这是我对Project Euler 1st问题的解决方案,简单且易于理解。它在java中,但可以很容易地翻译成任何语言

public class Multiples {

    public static int multiplesSum(int n1, int n2, int limit) {
        int sum = 0;
        int n = Math.min(n1, n2);
        for(int i=n; i<limit; i++) {
            if(i%n1==0 || i%n2==0) {
                sum+=i;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        int total = multiplesSum(3,5,1000);
        System.out.println("Total sum is: " + total);
    }

}

这是项目欧拉专家建议的最佳解决方案:

/**
 * Remember that the sum of the first n natural numbers is: n(n+1)/2
 * 
 * Let’s look at the details of our function and take as example n=3.
    We would have to add:
    3+6+9+12+......+999=3*(1+2+3+4+...+333)
    For n=5 we would get:
    5+10+15+...+995=5*(1+2+....+199)
    Now note that 199=995/5 but also 999/5 rounded down to the nearest integer.
 * 
 * @param n
 * @param limit
 * @return
 */
public static int multiplesSumOf(int n, int limit) {
    int target = (limit-1)/n;               
    return n * target*(target+1)/2;
}

public static void main(String[] args) {
    int total = multiplesSumOf(3,1000) + multiplesSumOf(5,1000) - multiplesSumOf(15,1000);
    System.out.println("Total: " + total);
}

答案 5 :(得分:0)

var num = 1000
var numbers = []

for(var i = 0;  i < num; i++){
  if( i % 3 === 0|| i % 5 === 0){
     numbers.push(i)
  }
};

var sum = numbers.reduce((a, b) => a + b, 0);

答案 6 :(得分:0)

我在这里使用Kotlin发布了我的解决方案: https://github.com/moxi/project-euler-solutions/blob/master/code/src/main/kotlin/org/rcgonzalezf/onetoten/Problem1.kt 关于Kotlin以及为什么我在这里发帖的好处是它们也支持JavaScript编译(转换),所以你可能会发现它很有趣:https://kotlinlang.org/docs/tutorials/javascript/kotlin-to-javascript/kotlin-to-javascript.html

fun solve(baseNumber: Int): Int {
    return (0..baseNumber - 1)
            .filter { it % 3 == 0 || it % 5 == 0 }
            .sum()
}

正如你所看到的那样非常简洁,我将尝试解决一些Euler Project问题在LiveEdu上流式传输我觉得它很有趣,我不想在这里粘贴链接,因为这不是广告平台,如果你想讨论它在那里搜索项目欧拉。

相关问题