package ProjectEuler;/*** The sum of the squares of the first ten natural numbers is,* 1^2 + 2^2 + ... + 10^2 = 385* The square of the sum of the first ten natural numbers is,* (1 + 2 + ... + 10)^2 = 552 = 3025* Hence the difference between the sum of the squares of the first ten natural* numbers and the square of the sum is 3025 −ひく 385 =わ 2640.* Find the difference between the sum of the squares of the first N natural* numbers and the square of the sum.* <p>* link: https://projecteuler.net/problem=6*/public class Problem06 {public static void main(String[] args) {int[][] testNumbers = {{10, 2640},{15, 13160},{20, 41230},{50, 1582700}};for (int[] testNumber : testNumbers) {assert solution1(testNumber[0]) == testNumber[1]&& solutions2(testNumber[0]) == testNumber[1];}}private static int solution1(int n) {int sum1 = 0;int sum2 = 0;for (int i = 1; i <= n; ++i) {sum1 += i * i;sum2 += i;}return sum2 * sum2 - sum1;}private static int solutions2(int n) {int sumOfSquares = n * (n + 1) * (2 * n + 1) / 6;int squareOfSum = (int) Math.pow((n * (n + 1) / 2.0), 2);return squareOfSum - sumOfSquares;}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。