Skip to content

Commit

Permalink
Added new Mathematical algorithm , Figurate Numbers
Browse files Browse the repository at this point in the history
Algorithm for checking whether input number does belong to any type of figurate numbers
  • Loading branch information
Badhusha-Akhthaar committed Oct 17, 2021
1 parent a9d2051 commit 915ca4c
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 0 deletions.
76 changes: 76 additions & 0 deletions Maths/FigurateNumber.js
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 }
72 changes: 72 additions & 0 deletions Maths/test/FigurateNumber.test.js
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
})
})
})

0 comments on commit 915ca4c

Please sign in to comment.