Skip to content

Commit 8778323

Browse files
feat: add narcissistic numbers algorithm
1 parent 5c39e87 commit 8778323

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

‎Maths/NarcissisticNumber.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @function isNarcissisticNumber
3+
* @description A narcissistic number (also known as an Armstrong number)
4+
* for a given number of digits n is a number that equals the sum of
5+
* its digits each raised to the power n. This function checks all
6+
* narcissistic numbers up to a given limit.
7+
* @param {number} limit - A positive integer upper limit
8+
* @returns {number[]} - Array of all narcissistic numbers up to limit
9+
* @see https://en.wikipedia.org/wiki/Narcissistic_number
10+
* @example getNarcissisticNumbers(500) => [1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407]
11+
* @example getNarcissisticNumbers(10) => [1, 2, 3, 4, 5, 6, 7, 8, 9]
12+
*/
13+
const getNarcissisticNumbers = (limit) => {
14+
if (!Number.isInteger(limit) || limit < 1) {
15+
throw new TypeError('Input must be a positive integer')
16+
}
17+
const result = []
18+
for (let n = 1; n <= limit; n++) {
19+
const digits = String(n).split('')
20+
const power = digits.length
21+
const sum = digits.reduce((acc, d) => acc + Math.pow(Number(d), power), 0)
22+
if (sum === n) result.push(n)
23+
}
24+
return result
25+
}
26+
27+
export { getNarcissisticNumbers }
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { getNarcissisticNumbers } from '../NarcissisticNumber.js'
2+
3+
describe('getNarcissisticNumbers', () => {
4+
test('single digits 1-9 are all narcissistic', () => {
5+
expect(getNarcissisticNumbers(9)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9])
6+
})
7+
8+
test('getNarcissisticNumbers(10) returns [1..9]', () => {
9+
expect(getNarcissisticNumbers(10)).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9])
10+
})
11+
12+
test('153 is narcissistic', () => {
13+
const result = getNarcissisticNumbers(153)
14+
expect(result).toContain(153)
15+
})
16+
17+
test('370 is narcissistic', () => {
18+
const result = getNarcissisticNumbers(370)
19+
expect(result).toContain(370)
20+
})
21+
22+
test('371 is narcissistic', () => {
23+
const result = getNarcissisticNumbers(371)
24+
expect(result).toContain(371)
25+
})
26+
27+
test('407 is narcissistic', () => {
28+
const result = getNarcissisticNumbers(407)
29+
expect(result).toContain(407)
30+
})
31+
32+
test('getNarcissisticNumbers(500) returns correct list', () => {
33+
expect(getNarcissisticNumbers(500)).toEqual([
34+
1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407
35+
])
36+
})
37+
38+
test('100 is NOT narcissistic', () => {
39+
const result = getNarcissisticNumbers(100)
40+
expect(result).not.toContain(100)
41+
})
42+
43+
test('throws TypeError for zero', () => {
44+
expect(() => getNarcissisticNumbers(0)).toThrow(TypeError)
45+
})
46+
47+
test('throws TypeError for negative input', () => {
48+
expect(() => getNarcissisticNumbers(-5)).toThrow(TypeError)
49+
})
50+
51+
test('throws TypeError for float input', () => {
52+
expect(() => getNarcissisticNumbers(9.5)).toThrow(TypeError)
53+
})
54+
})

0 commit comments

Comments
 (0)