Skip to content
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

added armstrong algorithm #211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/others/armstrong.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Check if a number is an Armstrong number.
*
* Returns true if the number is an Armstrong number, false otherwise.
*
* @public
*
* @example
* var isArmstrong = require('path-to-algorithms/src/others/armstrong').isArmstrong;
* var result = isArmstrong(153);
*
* console.log(result); // true
*
* @param {Number} num The number to check.
* @returns {boolean} True if the number is an Armstrong number, false otherwise.
*
* @module others/armstrong
*/
(function (exports) {
'use strict';

function isArmstrongNumber(num) {
if (num < 0) {
return false; // Armstrong numbers are non-negative
}

const numStr = num.toString();
const numDigits = numStr.length;
let sum = 0;

for (let i = 0; i < numDigits; i++) {
const digit = parseInt(numStr[i], 10);
sum += Math.pow(digit, numDigits);
}

return sum === num;
}

exports.isArmstrong = isArmstrongNumber;
})(typeof window === 'undefined' ? module.exports : window);
34 changes: 34 additions & 0 deletions test/others/armstrong.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
var isArmstrong = require('../../src/others/armstrong.js').isArmstrong;

describe('isArmstrong', () => {
'use strict';
it('should return true for Armstrong numbers', () => {
expect(isArmstrong(0)).toBe(true); // 0 is an Armstrong number
expect(isArmstrong(1)).toBe(true); // 1 is an Armstrong number
expect(isArmstrong(153)).toBe(true); // 153 is an Armstrong number
expect(isArmstrong(9474)).toBe(true); // 9474 is an Armstrong number
});

it('should return false for non-Armstrong numbers', () => {
expect(isArmstrong(10)).toBe(false); // 10 is not an Armstrong number
expect(isArmstrong(123)).toBe(false); // 123 is not an Armstrong number
expect(isArmstrong(1234)).toBe(false); // 1234 is not an Armstrong number
expect(isArmstrong(9876)).toBe(false); // 9876 is not an Armstrong number
});

it('should return false for negative numbers', () => {
expect(isArmstrong(-153)).toBe(false); // Negative numbers are not Armstrong numbers
expect(isArmstrong(-9474)).toBe(false); // Negative numbers are not Armstrong numbers
});

it('should return true for single-digit numbers', () => {
expect(isArmstrong(0)).toBe(true); // 0 is an Armstrong number
expect(isArmstrong(1)).toBe(true); // 1 is an Armstrong number
expect(isArmstrong(9)).toBe(true); // 9 is an Armstrong number
});

it('should return true for multi-digit Armstrong numbers', () => {
expect(isArmstrong(153)).toBe(true); // 3^3 + 5^3 + 1^3 = 153
expect(isArmstrong(9474)).toBe(true); // 9^4 + 4^4 + 7^4 + 4^4 = 9474
});
});