Skip to content

Add pronic number implementation #1023

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
37 changes: 37 additions & 0 deletions Maths/IsPronic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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}
*/

function isPronic (number) {
if (number % 2 === 1) {
return false
}

for (let i = 0; i <= Math.sqrt(number); i++) {
// Checking Pronic Number
// by multiplying consecutive
// numbers
if (number === i * (i + 1)) {
return true
}
}
return false
}

export { isPronic }
18 changes: 18 additions & 0 deletions Maths/test/IsPronic.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { isPronic } from '../IsPronic'

describe('Testing isPronic function', () => {
it('should return if the number is pronic or not for even number', () => {
const isPronicNumber = isPronic(2)
expect(isPronicNumber).toBe(true)
})

it('should return if the number is pronic or not for even number', () => {
const isPronicNumber = isPronic(4)
expect(isPronicNumber).toBe(false)
})

it('should return false for odd number', () => {
const isPronicNumber = isPronic(7)
expect(isPronicNumber).toBe(false)
})
})