From 570a809c933551cd86483859df9bf4cb4cbd3ce1 Mon Sep 17 00:00:00 2001 From: newt Date: Wed, 9 Oct 2024 18:02:35 +0100 Subject: [PATCH] (euler) - #6 --- euler/src/6 - Sum Square Difference.ts | 29 ++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 euler/src/6 - Sum Square Difference.ts diff --git a/euler/src/6 - Sum Square Difference.ts b/euler/src/6 - Sum Square Difference.ts new file mode 100644 index 0000000..27da7f4 --- /dev/null +++ b/euler/src/6 - Sum Square Difference.ts @@ -0,0 +1,29 @@ +import { calcSum } from './utils'; + +const sumOfSquares = (lowerBound: number, upperBound: number) => { + // Calculate the square number of all the numbers between the bounds + const squares: number[] = []; + + for (let i = lowerBound; i < upperBound + 1; i++) { + squares.push(i ** 2); + } + + // Return the sum + return calcSum(squares); +}; + +const squareOfSum = (lowerBound: number, upperBound: number) => { + // Get the sum of all of the numbers between the bounds + const numbers: number[] = []; + + for (let i = lowerBound; i < upperBound + 1; i++) { + numbers.push(i); + } + + const sum = calcSum(numbers); + + // Square the sum + return sum ** 2; +}; + +console.log(squareOfSum(1, 100) - sumOfSquares(1, 100));