From 685abab92d87747d4df0e37fe1f2bebdda938865 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Sun, 9 Oct 2022 01:04:03 +0530 Subject: [PATCH 1/4] =?UTF-8?q?=F0=9F=A7=91=E2=80=8D=F0=9F=9A=80=20Added?= =?UTF-8?q?=20Armstrong=20Number=20(and=20its=20tests)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Maths/ArmstrongNumber.ts | 28 ++++++++++++++++++++++++++++ Maths/test/ArmstrongNumber.test.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 Maths/ArmstrongNumber.ts create mode 100644 Maths/test/ArmstrongNumber.test.ts diff --git a/Maths/ArmstrongNumber.ts b/Maths/ArmstrongNumber.ts new file mode 100644 index 00000000..cebbf1cc --- /dev/null +++ b/Maths/ArmstrongNumber.ts @@ -0,0 +1,28 @@ +/** + * @function ArmstrongNumber + * @description Check if the provided number is an Armstrong number or not. + * @summary Armstrong numbers are numbers, the sum of whose digits each raised + * to the power of the number of digits is equal to the number itself. + * For example: + * 370 is an Armstrong number since 3^3 + 7^3 + 0^3 = 370 + * (These numbers are also known as Narcissistic numbers, and Pluperfect numbers) + * @param {number} num The number you want to check for + * @return {boolean} Whether the input number is an Armstrong number + * @see [Wikipedia](https://en.wikipedia.org/wiki/Armstrong_number) + * @see [OEIS](https://oeis.org/A005188) + * @example ArmstrongNumber(370) = true + * @example ArmstrongNumber(10) = false + */ +export const ArmstrongNumber = (num: number): boolean => { + if (typeof num !== 'number' || num <= 0) return false; + + let compNum = 0 + + const digitArr = num + .toString() + .split(''); + + digitArr.map((digit) => compNum += Math.pow(Number(digit), digitArr.length)) + + return num === compNum +} diff --git a/Maths/test/ArmstrongNumber.test.ts b/Maths/test/ArmstrongNumber.test.ts new file mode 100644 index 00000000..0b5c24a6 --- /dev/null +++ b/Maths/test/ArmstrongNumber.test.ts @@ -0,0 +1,27 @@ +import { ArmstrongNumber } from "../ArmstrongNumber" + +describe('ArmstrongNumber', () => { + it('should return the correct value', () => { + expect(ArmstrongNumber(9)).toBe(true) + }) + it('should return the correct value', () => { + expect(ArmstrongNumber(-310)).toBe(false) + }) + it('should return the correct value', () => { + expect(ArmstrongNumber(407)).toBe(true) + }) + it('should return the correct value', () => { + expect(ArmstrongNumber(420)).toBe(false) + }) + it('should return the correct value', () => { + expect(ArmstrongNumber(0)).toBe(false) + }) + + it('should return the correct value', () => { + expect(ArmstrongNumber(13579)).toBe(false) + }) + + it('should return the correct value', () => { + expect(ArmstrongNumber(92727)).toBe(true) + }) +}) \ No newline at end of file From 21e92ea7a2e2be4901bbd2f2bde6edd971429965 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Fri, 14 Oct 2022 09:27:42 +0530 Subject: [PATCH 2/4] Fixed Armstrong Number implementation --- Maths/ArmstrongNumber.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/Maths/ArmstrongNumber.ts b/Maths/ArmstrongNumber.ts index cebbf1cc..78776127 100644 --- a/Maths/ArmstrongNumber.ts +++ b/Maths/ArmstrongNumber.ts @@ -17,12 +17,13 @@ export const ArmstrongNumber = (num: number): boolean => { if (typeof num !== 'number' || num <= 0) return false; let compNum = 0 + let cloneNum = num + const numOfDigits = Math.floor(1 + Math.log10(num)) - const digitArr = num - .toString() - .split(''); + while (cloneNum > 0) { + compNum += Math.pow(cloneNum % 10, numOfDigits) + cloneNum = Math.floor(cloneNum / 10) + } - digitArr.map((digit) => compNum += Math.pow(Number(digit), digitArr.length)) - - return num === compNum + return compNum === num } From 4d0ca03e323d723d221d3305e8159b7ad1389407 Mon Sep 17 00:00:00 2001 From: SpiderMath <71999854+SpiderMath@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:14:54 +0530 Subject: [PATCH 3/4] Fixed tests for Armstrong Number --- Maths/test/ArmstrongNumber.test.ts | 28 ++++------------------------ 1 file changed, 4 insertions(+), 24 deletions(-) diff --git a/Maths/test/ArmstrongNumber.test.ts b/Maths/test/ArmstrongNumber.test.ts index 0b5c24a6..38136ce7 100644 --- a/Maths/test/ArmstrongNumber.test.ts +++ b/Maths/test/ArmstrongNumber.test.ts @@ -1,27 +1,7 @@ import { ArmstrongNumber } from "../ArmstrongNumber" -describe('ArmstrongNumber', () => { - it('should return the correct value', () => { - expect(ArmstrongNumber(9)).toBe(true) - }) - it('should return the correct value', () => { - expect(ArmstrongNumber(-310)).toBe(false) - }) - it('should return the correct value', () => { - expect(ArmstrongNumber(407)).toBe(true) - }) - it('should return the correct value', () => { - expect(ArmstrongNumber(420)).toBe(false) - }) - it('should return the correct value', () => { - expect(ArmstrongNumber(0)).toBe(false) - }) - - it('should return the correct value', () => { - expect(ArmstrongNumber(13579)).toBe(false) - }) - - it('should return the correct value', () => { - expect(ArmstrongNumber(92727)).toBe(true) - }) +test('ArmstrongNumber', () => { + test.each([[9, true], [-310, false], [0, false], [407, true], [420, false], [92727, true], [13579, false]])('i is an Armstrong number or not', (num, expected) => { + expect(ArmstrongNumber(num)).toBe(expected) + }) }) \ No newline at end of file From 05880109d6882fc76e0071f0c64053e97efd5855 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20M=C3=BCller?= <34514239+appgurueu@users.noreply.github.com> Date: Sat, 15 Oct 2022 12:24:28 +0200 Subject: [PATCH 4/4] Remove enclosing test block --- Maths/test/ArmstrongNumber.test.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Maths/test/ArmstrongNumber.test.ts b/Maths/test/ArmstrongNumber.test.ts index 38136ce7..5ff38656 100644 --- a/Maths/test/ArmstrongNumber.test.ts +++ b/Maths/test/ArmstrongNumber.test.ts @@ -1,7 +1,5 @@ import { ArmstrongNumber } from "../ArmstrongNumber" -test('ArmstrongNumber', () => { - test.each([[9, true], [-310, false], [0, false], [407, true], [420, false], [92727, true], [13579, false]])('i is an Armstrong number or not', (num, expected) => { - expect(ArmstrongNumber(num)).toBe(expected) - }) -}) \ No newline at end of file +test.each([[9, true], [-310, false], [0, false], [407, true], [420, false], [92727, true], [13579, false]])('i is an Armstrong number or not', (num, expected) => { + expect(ArmstrongNumber(num)).toBe(expected) +})