forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FigurateNumber.js
75 lines (71 loc) · 1.54 KB
/
FigurateNumber.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/**
Problem Statement and Explanation :
Triangular => https://en.wikipedia.org/wiki/Triangular_number
Tetrahedral => https://en.wikipedia.org/wiki/Tetrahedral_number
Pentatope => https://en.wikipedia.org/wiki/Pentatope_number
Example:
Triangular => (0, 1, 3, 6, 10, 15, 21, 28, 36, 45)
Tetrahedral => (1, 4, 10, 20, 35, 56, 84, 120, 165,)
Pentatope => (1, 5, 15, 35, 70, 126, 210, 330, 495)
*/
/**
*
* @param {*} number
* @returns
*/
const isTriangular = (number) => {
for (let i = 0; i <= number; i++) {
if ((i * (i + 1)) / 2 === number) {
return true
} else if ((i * (i + 1)) / 2 > number) {
return false
}
}
return false
}
/**
*
* @param {*} number
* @returns
*/
const isTetrahedral = (number) => {
for (let i = 1; i <= number; i++) {
if ((i * (i + 1) * (i + 2)) / 6 === number) {
return true
} else if ((i * (i + 1) * (i + 2)) / 6 > number) {
return false
}
}
return false
}
/**
*
* @param {*} number
* @returns
*/
const isPentatope = (number) => {
for (let i = 1; i <= number; i++) {
if ((i * (i + 1) * (i + 2) * (i + 3)) / 24 === number) {
return true
} else if ((i * (i + 1) * (i + 2) * (i + 3)) / 24 > number) {
return false
}
}
return false
}
/**
*
* @param {*} number
* @returns
*/
const checkAll = (number) => {
return {
isTriangular: isTriangular(number),
isTetrahedral: isTetrahedral(number),
isPentatope: isPentatope(number)
}
}
export { isTriangular }
export { isTetrahedral }
export { isPentatope }
export { checkAll }