Skip to content

Commit 8ea9cf5

Browse files
Add project-euler problem 12
1 parent 5f601fa commit 8ea9cf5

3 files changed

Lines changed: 73 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@
193193
* [Haversine](https://github.com/TheAlgorithms/Javascript/blob/master/Navigation/Haversine.js)
194194

195195
## Project-Euler
196+
* [Problem012](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem012.js)
196197
* [Problem013](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem013.js)
197198
* [Problem014](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem014.js)
198199
* [Problem015](https://github.com/TheAlgorithms/Javascript/blob/master/Project-Euler/Problem015.js)

Project-Euler/Problem012.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* Problem 12 - Highly divisible triangular number
3+
*
4+
* https://projecteuler.net/problem=11
5+
*
6+
* The sequence of triangle numbers is generated by adding the natural numbers.
7+
* So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28.
8+
*
9+
* The first ten terms would be: 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
10+
* Let us list the factors of the first seven triangle numbers:
11+
*
12+
* 1: 1
13+
* 3: 1,3
14+
* 6: 1,2,3,6
15+
* 10: 1,2,5,10
16+
* 15: 1,3,5,15
17+
* 21: 1,3,7,21
18+
* 28: 1,2,4,7,14,28
19+
*
20+
* We can see that 28 is the first triangle number to have over five divisors.
21+
*
22+
* What is the value of the first triangle number to have over five hundred divisors?
23+
*/
24+
25+
/**
26+
* Gets number of divisors of a given number
27+
* @params num The number whose divisors to find
28+
*/
29+
const getNumOfDivisors = (num) => {
30+
// initialize numberOfDivisors
31+
let numberOfDivisors = 0
32+
33+
// if one divisor less than sqrt(num) exists
34+
// then another divisor greater than sqrt(n) exists and its value is num/i
35+
for (let i = 0; i <= Math.sqrt(num); i++) {
36+
// check if i divides num
37+
if (num % i === 0) {
38+
if (num / i === i) {
39+
// if both divisors are equal, i.e., num is perfect square, then only 1 divisor
40+
numberOfDivisors++
41+
} else {
42+
// 2 divisors, one of them is less than sqrt(n), other greater than sqrt(n)
43+
numberOfDivisors += 2
44+
}
45+
}
46+
}
47+
return numberOfDivisors
48+
}
49+
50+
/**
51+
* Loops till first triangular number with 500 divisors is found
52+
*/
53+
const firstTriangularWith500Divisors = () => {
54+
// loop forever till numberOfDivisors is greater than 500
55+
for (let n = 1; n > 0; n++) {
56+
// nth triangular number is (1/2)*n*(n+1) by Arithmetic Progression
57+
const triangularNum = (1 / 2) * n * (n + 1)
58+
if (getNumOfDivisors(triangularNum) >= 500) return triangularNum
59+
}
60+
}
61+
62+
console.log(firstTriangularWith500Divisors())
63+
64+
export { firstTriangularWith500Divisors }
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { firstTriangularWith500Divisors } from '../Problem012'
2+
3+
describe('checkFirstTriangularWith500Divisors()', () => {
4+
it('Problem Statement Answer', () => {
5+
const firstTriangular = firstTriangularWith500Divisors()
6+
expect(firstTriangular).toBe(76576500)
7+
})
8+
})

0 commit comments

Comments
 (0)