mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
remove files to merge from seprate brnches
This commit is contained in:
@ -1,22 +0,0 @@
|
||||
/*
|
||||
Smallest multiple
|
||||
|
||||
2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
|
||||
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
|
||||
*/
|
||||
|
||||
const findSmallestMultiple = () => {
|
||||
const divisors = [20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2]
|
||||
let num = 21
|
||||
let result
|
||||
|
||||
while (!result) {
|
||||
const isDivisibleByAll = divisors.every((divisor) => num % divisor === 0)
|
||||
if (isDivisibleByAll) result = num
|
||||
else num++
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
console.log(findSmallestMultiple())
|
@ -1,24 +0,0 @@
|
||||
/*
|
||||
Sum square difference
|
||||
The sum of the squares of the first ten natural numbers is 385,
|
||||
|
||||
The square of the sum of the first ten natural numbers is 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.
|
||||
*/
|
||||
|
||||
const findSumSquareDifference = (elementsCount) => {
|
||||
const naturalNumbers = Array(elementsCount).fill().map((_, i) => i + 1)
|
||||
|
||||
let sumOfSquares = 0
|
||||
naturalNumbers.forEach(number => { sumOfSquares += Math.pow(number, 2) })
|
||||
|
||||
const sumOfNumbers = naturalNumbers.reduce((prev, current) => { current += prev }, 0)
|
||||
const squareOfSums = Math.pow(sumOfNumbers, 2)
|
||||
|
||||
return squareOfSums - sumOfSquares
|
||||
}
|
||||
|
||||
console.log(findSumSquareDifference(20))
|
Reference in New Issue
Block a user