Skip to content

Commit db479a4

Browse files
feat: add Perfect number algorithm
1 parent 5c39e87 commit db479a4

2 files changed

Lines changed: 63 additions & 32 deletions

File tree

‎Maths/PerfectNumber.js‎

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,28 @@
11
/**
2-
* Author: dephraiim
3-
* License: GPL-3.0 or later
4-
*
5-
* == Perfect Number ==
6-
* In number theory, a perfect number is a positive integer that is equal to the sum of
7-
* its positive divisors(factors), excluding the number itself.
8-
* For example: 6 ==> divisors[1, 2, 3, 6]
9-
* Excluding 6, the sum(divisors) is 1 + 2 + 3 = 6
10-
* So, 6 is a Perfect Number
11-
* Other examples of Perfect Numbers: 28, 486, ...
12-
*
13-
* More on Perfect Number:
14-
* https://en.wikipedia.org/wiki/Perfect_number
15-
*
2+
* @function isPerfectNumber
3+
* @description A perfect number is a positive integer that is equal to the sum
4+
* of its positive divisors, excluding the number itself. For example, 28 is
5+
* perfect because 1 + 2 + 4 + 7 + 14 = 28.
6+
* @param {number} n - A positive integer
7+
* @returns {boolean} - True if n is a perfect number, false otherwise
8+
* @see https://en.wikipedia.org/wiki/Perfect_number
9+
* @example isPerfectNumber(6) => true
10+
* @example isPerfectNumber(28) => true
11+
* @example isPerfectNumber(12) => false
1612
*/
17-
18-
const factorsExcludingNumber = (n) => {
19-
return [...Array(n).keys()].filter((num) => n % num === 0)
20-
}
21-
22-
const perfectNumber = (n) => {
23-
const factorSum = factorsExcludingNumber(n).reduce((num, initialValue) => {
24-
return num + initialValue
25-
}, 0)
26-
27-
return factorSum === n
13+
const isPerfectNumber = (n) => {
14+
if (!Number.isInteger(n) || n < 1) {
15+
throw new TypeError('Input must be a positive integer')
16+
}
17+
if (n === 1) return false
18+
let sum = 1
19+
for (let i = 2; i <= Math.sqrt(n); i++) {
20+
if (n % i === 0) {
21+
sum += i
22+
if (i !== n / i) sum += n / i
23+
}
24+
}
25+
return sum === n
2826
}
2927

30-
export { perfectNumber }
28+
export { isPerfectNumber }

‎Maths/test/PerfectNumber.test.js‎

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,43 @@
1-
import { perfectNumber } from '../PerfectNumber'
1+
import { isPerfectNumber } from '../PerfectNumber.js'
22

3-
describe('PerfectNumber', () => {
4-
it('should return true for a perfect cube', () => {
5-
expect(perfectNumber(28)).toBeTruthy()
3+
describe('isPerfectNumber', () => {
4+
test('6 is a perfect number (1+2+3=6)', () => {
5+
expect(isPerfectNumber(6)).toBe(true)
66
})
7-
it('should return false for a non perfect cube', () => {
8-
expect(perfectNumber(10)).toBeFalsy()
7+
8+
test('28 is a perfect number (1+2+4+7+14=28)', () => {
9+
expect(isPerfectNumber(28)).toBe(true)
10+
})
11+
12+
test('496 is a perfect number', () => {
13+
expect(isPerfectNumber(496)).toBe(true)
14+
})
15+
16+
test('8128 is a perfect number', () => {
17+
expect(isPerfectNumber(8128)).toBe(true)
18+
})
19+
20+
test('1 is NOT a perfect number', () => {
21+
expect(isPerfectNumber(1)).toBe(false)
22+
})
23+
24+
test('12 is NOT a perfect number', () => {
25+
expect(isPerfectNumber(12)).toBe(false)
26+
})
27+
28+
test('100 is NOT a perfect number', () => {
29+
expect(isPerfectNumber(100)).toBe(false)
30+
})
31+
32+
test('throws TypeError for zero', () => {
33+
expect(() => isPerfectNumber(0)).toThrow(TypeError)
34+
})
35+
36+
test('throws TypeError for negative input', () => {
37+
expect(() => isPerfectNumber(-5)).toThrow(TypeError)
38+
})
39+
40+
test('throws TypeError for float input', () => {
41+
expect(() => isPerfectNumber(2.5)).toThrow(TypeError)
942
})
1043
})

0 commit comments

Comments
 (0)