Skip to content

Commit ca4cf6d

Browse files
algorithm: pronic number (#82)
* Added implementation for Pronic Number 😁 * Update DIRECTORY.md Co-authored-by: autoprettier <[email protected]>
1 parent 03244a2 commit ca4cf6d

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

DIRECTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_odd.ts)
2727
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts)
2828
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts)
29+
* [Pronic Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pronic_number.ts)
2930
* [Radians To Degrees](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/radians_to_degrees.ts)
3031
* Series
3132
* [Hexagonal Numbers](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/hexagonal_numbers.ts)

maths/pronic_number.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @function PronicNumber
3+
* @description Checks whether a given number is a pronic number or not
4+
* @summary Pronic numbers, or oblong numbers as they are often referred to as,
5+
* are numbers which are the product of two consecutive integers. That is,
6+
* they are numbers of the form n*(n+1)
7+
*
8+
* For example, 20 is a pronic number since 20 = 4 * 5
9+
* @param num The number to check for being pronic
10+
* @returns {boolean} Whether the number is pronic or not
11+
* @see [Wikipedia](https://en.wikipedia.org/wiki/Pronic_number)
12+
* @example PronicNumber(20) = true
13+
* @example PronicNumber(30) = true
14+
* @example PronicNumber(49) = false
15+
*/
16+
const PronicNumber = (n: number) => {
17+
if (isNaN(n)) throw new Error('The input needs to be a number')
18+
if (!Number.isInteger(n) || n < 0) throw new Error('The input needs to be a non-negative integer')
19+
if (n === 0) return true
20+
21+
return !Number.isInteger(Math.sqrt(n)) && Math.floor(Math.sqrt(n)) * Math.ceil(Math.sqrt(n)) === n
22+
}
23+
24+
export { PronicNumber }

maths/test/pronic_number.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { PronicNumber } from '../pronic_number'
2+
3+
test.each([[0, true], [10, false], [30, true], [69, false], [420, true]])('Pronic Number', (number, result) => {
4+
expect(PronicNumber(number)).toBe(result)
5+
})

0 commit comments

Comments
 (0)