Project Euler: #6 - Sum square difference

Search for a command to run...

No comments yet. Be the first to comment.
In this series, I will be discussing Project Euler Solutions. 'Project Euler' helps in improving problem-solving and programming skills. Remember, there is always a better solution to the problem. 💯
Problem By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? Problem Description We all know about Prime Numbers. A number that is divisible by 1 and itself is called ...
What is JavaScript? Discover how this quirky yet powerful language runs in browsers, what makes it tick, and how you can start adding it to your sites

Problem If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters w...

Problem 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000? Problem Description The problem is pretty self-explanatory. 215 is 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26....

Problem Starting in the top left corner of a 2*2 grid, and only being able to move to the right and down, there are exactly 6 routes to the bottom right corner. How many such routes are there through a 20*20 grid? Problem Description We are given a...

Problem The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → ...

The sum of the squares of the first ten natural numbers is,
$$1^2 + 2^2 + 3^2 + 4^2 + 5^2 + 6^2 + 7^2 + 8^2 + 9^2 + 10^2 = 385$$
The square of the sum of the first ten natural numbers is,
$$(1+2+3+4+5+6+7+8+9+10)^2 = 55^2 = 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 one hundred natural numbers and the square of the sum.
Well, the problem itself is self-explanatory.
The example given is for first 10 natural numbers. We need to find the difference between the Sum of Squares & Squares of the Sum of first 100 natural numbers.
i.e. 1 to 100.
This is a detailed approach.
Two functions are involved. One function is to calculate the square of each number from 1 to 100 and add those squares.
Another function is to add the numbers from 1 to 100 and square their sum.
Finally, find the difference between both.
Very simple, Right?
const sumOfSqrNums = (start, end) => {
let sumOfSqrs = 0;
if (start > end) {
console.log(`${start} > ${end}! Enter a valid Start & End.`);
} else {
for (let i = start; i <= end; i++) {
sumOfSqrs += Math.pow(i, 2);
}
}
return sumOfSqrs;
}
const sqrOfSumNums = (start, end) => {
let sumOfNums = 0;
if (start > end) {
console.log(`${start} > ${end}! Enter a valid Start & End.`);
} else {
for (let i = start; i <= end; i++) {
sumOfNums += i;
}
}
return Math.pow(sumOfNums, 2);
}
let difference = sqrOfSumNums(1, 100) - sumOfSqrNums(1, 100);
console.log(difference);
You can find my solution on GitHub 06
This particular solution was executed with a better complexity in Hacker rank. But there are still much better solutions to this. If you have another or a better solution, please leave it in the comments below.
For the other Project Euler Solutions, please follow the series Project Euler Solutions in JS.
Thank you!