欧拉项目#1

我只在三天前开始编程,而且一直在使用Javascript。 我一直在努力解决欧拉项目问题1 :

find1000以下所有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的答案。

欧拉项目#1的任务是

find1000以下所有3或5的倍数的总和。

find1000以下所有3的倍数和1000以下5的所有倍数之和。

像45和3和5的倍数的数字不应该被计数两次。

欧拉1号项目是

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

既然你在学习,我假设你不需要明确的答案。

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

 [3, 6, 9, 12, 15] 

和5循环的前几个术语是

 [5, 10, 15] 

既然你计算了三次和五次的倍数,你的答案就比它应该大

附注 – 编程通常也意味着寻找非显而易见的解决方案。 这个问题的措辞表明循环的数字从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; 
 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); 

这里是我对Project Euler第一个问题的解决方案,简单而且很容易理解。 这是在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 

在欧拉专家的建议下,这是最好的解决方案:

 /** * 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); } 
 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); 

我使用Kotlin在这里发布我的解决方案: https : //github.com/moxi/project-euler-solutions/blob/master/code/src/main/kotlin/org/rcgonzalezf/onetoten/Problem1.kt关于Kotlin为什么我在这里发布的是,他们也支持JavaScript编译(transpilation),所以你可能会觉得它很有趣: 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() } 

正如你所看到的很简洁,我将尝试解决一些欧拉项目的问题在LiveEdu流媒体我认为这将是有趣的,我不想粘贴在这里的链接,因为这不是一个广告平台,如果你想讨论它在那里寻找欧拉工程。