Skip to content

Commit dc67506

Browse files
merge: Add pronic number implementation (TheAlgorithms#1023)
* feat: Add pronic number implementation * Add test to Math * Minor fixes * Minor style fixes * refactor: Store square root in a variable * Minor refactoring * fix: Change pronic number check logic Reduced time complexity from O(sqrt(n)) to O(1) * Minor style fixes * fix: Update pronic number check boolean equation * refactor: Change pronic number check condition * refactor: Add tests to Math * Minor style fixes * refactor: Change unit test logic
1 parent d28ae8b commit dc67506

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

Maths/IsPronic.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
3+
* Pronic Number: https://en.wikipedia.org/wiki/Pronic_number
4+
* function to check if number is pronic.
5+
* return true if number is pronic.
6+
* else false
7+
*/
8+
9+
/**
10+
* @function isPronic
11+
* @description -> Checking if number is pronic using product of two consecutive numbers
12+
* If number is a product of two consecutive numbers, then it is pronic
13+
* therefore, the function will return true
14+
*
15+
* If number is not a product of two consecutive numbers, then it is not pronic
16+
* therefore, the function will return false
17+
* @param {number} number
18+
* @returns {boolean}
19+
*/
20+
21+
export const isPronic = (number) => {
22+
if (number === 0) {
23+
return true
24+
}
25+
const sqrt = Math.sqrt(number)
26+
return sqrt % 1 !== 0 && Math.ceil(sqrt) * Math.floor(sqrt) === number
27+
}

Maths/test/IsPronic.test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { isPronic } from '../IsPronic'
2+
3+
const pronicNumbers = [0, 2, 6, 12, 20, 30, 42, 56, 72, 90, 110, 132, 156, 182, 210, 240, 272, 306, 342, 380, 420, 462, 506, 552, 600, 650, 702, 756, 812, 870, 930, 992, 1056, 1122, 1190, 1260, 1332, 1406, 1482, 1560, 1640, 1722, 1806, 1892, 1980, 2070, 2162, 2256, 2352, 2450, 2550]
4+
5+
describe('Testing isPronic function', () => {
6+
for (let i = 0; i <= 2500; i++) {
7+
it('should return true', () => {
8+
const isPronicNumber = isPronic(i)
9+
expect(isPronicNumber).toBe(pronicNumbers.includes(i))
10+
})
11+
}
12+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy