From 8ea9cf5d08e3db0e15d3aa075d0b97954c3bb06c Mon Sep 17 00:00:00 2001 From: Aayush Borkar Date: Sat, 9 Oct 2021 15:02:30 +0000 Subject: [PATCH 1/5] Add project-euler problem 12 --- DIRECTORY.md | 1 + Project-Euler/Problem012.js | 64 +++++++++++++++++++++++++++ Project-Euler/test/Problem012.test.js | 8 ++++ 3 files changed, 73 insertions(+) create mode 100644 Project-Euler/Problem012.js create mode 100644 Project-Euler/test/Problem012.test.js diff --git a/DIRECTORY.md b/DIRECTORY.md index 8290405c9a..978027c0fa 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -193,6 +193,7 @@ * [Haversine](https://github.com/TheAlgorithms/Javascript/blob/master/Navigation/Haversine.js) ## Project-Euler + * [Problem012](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem012.js) * [Problem013](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem013.js) * [Problem014](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem014.js) * [Problem015](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem015.js) diff --git a/Project-Euler/Problem012.js b/Project-Euler/Problem012.js new file mode 100644 index 0000000000..4340ba2e70 --- /dev/null +++ b/Project-Euler/Problem012.js @@ -0,0 +1,64 @@ +/** + * Problem 12 - Highly divisible triangular number + * + * https://projecteuler.net/problem=11 + * + * The sequence of triangle numbers is generated by adding the natural numbers. + * So the 7th triangle number would be 1 +たす 2 +たす 3 +たす 4 +たす 5 +たす 6 +たす 7 = 28. + * + * The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ... + * Let us list the factors of the first seven triangle numbers: + * + * 1: 1 + * 3: 1,3 + * 6: 1,2,3,6 + * 10: 1,2,5,10 + * 15: 1,3,5,15 + * 21: 1,3,7,21 + * 28: 1,2,4,7,14,28 + * + * We can see that 28 is the first triangle number to have over five divisors. + * + * What is the value of the first triangle number to have over five hundred divisors? +*/ + +/** + * Gets number of divisors of a given number + * @params num The number whose divisors to find + */ +const getNumOfDivisors = (num) => { + // initialize numberOfDivisors + let numberOfDivisors = 0 + + // if one divisor less than sqrt(num) exists + // then another divisor greater than sqrt(n) exists and its value is num/i + for (let i = 0; i <= Math.sqrt(num); i++) { + // check if i divides num + if (num % i === 0) { + if (num / i === i) { + // if both divisors are equal, i.e., num is perfect square, then only 1 divisor + numberOfDivisors++ + } else { + // 2 divisors, one of them is less than sqrt(n), other greater than sqrt(n) + numberOfDivisors += 2 + } + } + } + return numberOfDivisors +} + +/** + * Loops till first triangular number with 500 divisors is found + */ +const firstTriangularWith500Divisors = () => { + // loop forever till numberOfDivisors is greater than 500 + for (let n = 1; n> 0; n++) { + // nth triangular number is (1/2)*n*(n+1) by Arithmetic Progression + const triangularNum = (1 / 2) * n * (n + 1) + if (getNumOfDivisors(triangularNum)>= 500) return triangularNum + } +} + +console.log(firstTriangularWith500Divisors()) + +export { firstTriangularWith500Divisors } diff --git a/Project-Euler/test/Problem012.test.js b/Project-Euler/test/Problem012.test.js new file mode 100644 index 0000000000..2fd1f8d897 --- /dev/null +++ b/Project-Euler/test/Problem012.test.js @@ -0,0 +1,8 @@ +import { firstTriangularWith500Divisors } from '../Problem012' + +describe('checkFirstTriangularWith500Divisors()', () => { + it('Problem Statement Answer', () => { + const firstTriangular = firstTriangularWith500Divisors() + expect(firstTriangular).toBe(76576500) + }) +}) From 36238720d33855fc313df6b1a701538e65a8f2c4 Mon Sep 17 00:00:00 2001 From: Aayush Borkar Date: Sat, 9 Oct 2021 17:07:55 +0000 Subject: [PATCH 2/5] Fix raised alert in problem 12 --- Project-Euler/Problem012.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/Problem012.js b/Project-Euler/Problem012.js index 4340ba2e70..6212af40fb 100644 --- a/Project-Euler/Problem012.js +++ b/Project-Euler/Problem012.js @@ -52,7 +52,7 @@ const getNumOfDivisors = (num) => { */ const firstTriangularWith500Divisors = () => { // loop forever till numberOfDivisors is greater than 500 - for (let n = 1; n> 0; n++) { + for (let n = 1; n < Infinity; n++) { // nth triangular number is (1/2)*n*(n+1) by Arithmetic Progression const triangularNum = (1 / 2) * n * (n + 1) if (getNumOfDivisors(triangularNum)>= 500) return triangularNum From 173401cf3aa3e02c8b0a8b3c464074b70526abea Mon Sep 17 00:00:00 2001 From: Aayush Borkar Date: 2021年10月10日 06:42:44 +0000 Subject: [PATCH 3/5] Fixed for loop in Problem012 --- Project-Euler/Problem012.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Project-Euler/Problem012.js b/Project-Euler/Problem012.js index 6212af40fb..46eaf8824d 100644 --- a/Project-Euler/Problem012.js +++ b/Project-Euler/Problem012.js @@ -51,12 +51,15 @@ const getNumOfDivisors = (num) => { * Loops till first triangular number with 500 divisors is found */ const firstTriangularWith500Divisors = () => { - // loop forever till numberOfDivisors is greater than 500 - for (let n = 1; n < Infinity; n++) { + let numOfDivisors = 0 + let triangularNum + // loop forever until numOfDivisors becomes greater than or equal to 500 + for (let n = 1; numOfDivisors < 500; n++) { // nth triangular number is (1/2)*n*(n+1) by Arithmetic Progression - const triangularNum = (1 / 2) * n * (n + 1) - if (getNumOfDivisors(triangularNum)>= 500) return triangularNum + triangularNum = (1 / 2) * n * (n + 1) + numOfDivisors = getNumOfDivisors(triangularNum) } + return triangularNum } console.log(firstTriangularWith500Divisors()) From 8d246b786deef84291115042716fd3592fb54452 Mon Sep 17 00:00:00 2001 From: Aayush Borkar Date: 2021年10月11日 11:57:52 +0000 Subject: [PATCH 4/5] fix alerts in Problem012 --- Project-Euler/Problem012.js | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Project-Euler/Problem012.js b/Project-Euler/Problem012.js index 46eaf8824d..c902661560 100644 --- a/Project-Euler/Problem012.js +++ b/Project-Euler/Problem012.js @@ -32,7 +32,8 @@ const getNumOfDivisors = (num) => { // if one divisor less than sqrt(num) exists // then another divisor greater than sqrt(n) exists and its value is num/i - for (let i = 0; i <= Math.sqrt(num); i++) { + const sqrtNum = Math.sqrt(num) + for (let i = 0; i <= sqrtNum; i++) { // check if i divides num if (num % i === 0) { if (num / i === i) { @@ -51,17 +52,13 @@ const getNumOfDivisors = (num) => { * Loops till first triangular number with 500 divisors is found */ const firstTriangularWith500Divisors = () => { - let numOfDivisors = 0 let triangularNum // loop forever until numOfDivisors becomes greater than or equal to 500 - for (let n = 1; numOfDivisors < 500; n++) { + for (let n = 1; ; n++) { // nth triangular number is (1/2)*n*(n+1) by Arithmetic Progression triangularNum = (1 / 2) * n * (n + 1) - numOfDivisors = getNumOfDivisors(triangularNum) + if (getNumOfDivisors(triangularNum)>= 500) return triangularNum } - return triangularNum } -console.log(firstTriangularWith500Divisors()) - export { firstTriangularWith500Divisors } From 94aad35b7849cb85fb452424896c79fde1beef97 Mon Sep 17 00:00:00 2001 From: Aayush Borkar Date: 2021年10月21日 17:36:10 +0000 Subject: [PATCH 5/5] Update Problem012 --- Project-Euler/Problem012.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project-Euler/Problem012.js b/Project-Euler/Problem012.js index c902661560..9fb4ceb3b8 100644 --- a/Project-Euler/Problem012.js +++ b/Project-Euler/Problem012.js @@ -36,7 +36,7 @@ const getNumOfDivisors = (num) => { for (let i = 0; i <= sqrtNum; i++) { // check if i divides num if (num % i === 0) { - if (num / i === i) { + if (i === sqrtNum) { // if both divisors are equal, i.e., num is perfect square, then only 1 divisor numberOfDivisors++ } else {

AltStyle によって変換されたページ (->オリジナル) /