forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added new Mathematical algorithm , Figurate Numbers
Algorithm for checking whether input number does belong to any type of figurate numbers
- Loading branch information
1 parent
a9d2051
commit 915ca4c
Showing
2 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
Problem Statment 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 | ||
*/ | ||
let checkAll = (number) => { | ||
return { | ||
isTriangular: isTriangular(number), | ||
isTetrahedral: isTetrahedral(number), | ||
isPentatope: isPentatope(number) | ||
} | ||
} | ||
export { isTriangular } | ||
export { isTetrahedral } | ||
export { isPentatope } | ||
export { checkAll } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { | ||
isTriangular, | ||
isTetrahedral, | ||
isPentatope, | ||
checkAll | ||
} from '../FigurateNumber' | ||
|
||
describe('FigurateNumber', () => { | ||
it('Triangular : should return true', () => { | ||
expect(isTriangular(1)).toEqual(true) | ||
}) | ||
it('Triangular : should return true', () => { | ||
expect(isTriangular(3)).toEqual(true) | ||
}) | ||
|
||
it('Triangular : should return false', () => { | ||
expect(isTriangular(5)).toEqual(false) | ||
}) | ||
|
||
it('Triangular : should return true', () => { | ||
expect(isTriangular(171)).toEqual(true) | ||
}) | ||
/** End */ | ||
|
||
it('Tetrahedral : should return true', () => { | ||
expect(isTetrahedral(1)).toEqual(true) | ||
}) | ||
it('Tetrahedral : should return true', () => { | ||
expect(isTetrahedral(4)).toEqual(true) | ||
}) | ||
|
||
it('Tetrahedral : should return false', () => { | ||
expect(isTetrahedral(3)).toEqual(false) | ||
}) | ||
|
||
it('Tetrahedral : should return true', () => { | ||
expect(isTetrahedral(165)).toEqual(true) | ||
}) | ||
|
||
/** End */ | ||
it('Pentatope : should return true', () => { | ||
expect(isPentatope(1)).toEqual(true) | ||
}) | ||
it('Pentatope : should return true', () => { | ||
expect(isPentatope(5)).toEqual(true) | ||
}) | ||
|
||
it('Pentatope : should return false', () => { | ||
expect(isPentatope(3)).toEqual(false) | ||
}) | ||
|
||
it('Pentatope : should return true', () => { | ||
expect(isPentatope(1001)).toEqual(true) | ||
}) | ||
/** End */ | ||
|
||
it('Check All : should return all true',() => { | ||
expect(checkAll(1)).toEqual({ | ||
isTriangular: true, | ||
isTetrahedral: true, | ||
isPentatope: true | ||
}) | ||
}) | ||
|
||
it('Check All : should return all true,true,false',() => { | ||
expect(checkAll(15)).toEqual({ | ||
isTriangular: true, | ||
isTetrahedral: false, | ||
isPentatope: true | ||
}) | ||
}) | ||
}) |