mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
merge: Add pronic number implementation (#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
This commit is contained in:
27
Maths/IsPronic.js
Normal file
27
Maths/IsPronic.js
Normal file
@@ -0,0 +1,27 @@
|
||||
/*
|
||||
* Author: Akshay Dubey (https://github.com/itsAkshayDubey)
|
||||
* Pronic Number: https://en.wikipedia.org/wiki/Pronic_number
|
||||
* function to check if number is pronic.
|
||||
* return true if number is pronic.
|
||||
* else false
|
||||
*/
|
||||
|
||||
/**
|
||||
* @function isPronic
|
||||
* @description -> Checking if number is pronic using product of two consecutive numbers
|
||||
* If number is a product of two consecutive numbers, then it is pronic
|
||||
* therefore, the function will return true
|
||||
*
|
||||
* If number is not a product of two consecutive numbers, then it is not pronic
|
||||
* therefore, the function will return false
|
||||
* @param {number} number
|
||||
* @returns {boolean}
|
||||
*/
|
||||
|
||||
export const isPronic = (number) => {
|
||||
if (number === 0) {
|
||||
return true
|
||||
}
|
||||
const sqrt = Math.sqrt(number)
|
||||
return sqrt % 1 !== 0 && Math.ceil(sqrt) * Math.floor(sqrt) === number
|
||||
}
|
||||
Reference in New Issue
Block a user