mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
Add Algorithms to Math with tests (#429)
* chore: add area and area test * chore: add armstrong number * chore: add factors * chore: add perfect cube * chore: add perfect square * chore: add perfect number * chore: add number of digits * chore: fix according to standardjs * chore: remove conflicting files
This commit is contained in:

committed by
GitHub

parent
fc0901282b
commit
cd6ec65e54
24
Maths/ArmstrongNumber.js
Normal file
24
Maths/ArmstrongNumber.js
Normal file
@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Author: dephraiim
|
||||
* License: GPL-3.0 or later
|
||||
*
|
||||
* An Armstrong number is equal to the sum of the cubes of its digits.
|
||||
* For example, 370 is an Armstrong number because 3*3*3 + 7*7*7 + 0*0*0 = 370.
|
||||
* An Armstrong number is often called Narcissistic number.
|
||||
*
|
||||
*/
|
||||
|
||||
const armstrongNumber = (num) => {
|
||||
if (num < 0 || typeof num !== 'number') return false
|
||||
|
||||
let newSum = 0
|
||||
|
||||
const numArr = num.toString().split('')
|
||||
numArr.forEach((num) => {
|
||||
newSum += parseInt(num) ** numArr.length
|
||||
})
|
||||
|
||||
return newSum === num
|
||||
}
|
||||
|
||||
export { armstrongNumber }
|
16
Maths/Factors.js
Normal file
16
Maths/Factors.js
Normal file
@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Author: dephraiim
|
||||
* License: GPL-3.0 or later
|
||||
*
|
||||
* More on Factors:
|
||||
* https://www.mathsisfun.com/definitions/factor.html
|
||||
*
|
||||
*/
|
||||
|
||||
const factorsOfANumber = (number = 0) => {
|
||||
return Array.from(Array(number + 1).keys()).filter(
|
||||
(num) => number % num === 0
|
||||
)
|
||||
}
|
||||
|
||||
export { factorsOfANumber }
|
12
Maths/NumberOfDigits.js
Normal file
12
Maths/NumberOfDigits.js
Normal file
@ -0,0 +1,12 @@
|
||||
/**
|
||||
*
|
||||
* Author: dephraiim
|
||||
* License: GPL-3.0 or later
|
||||
*
|
||||
* Returns the number of digits of a given integer
|
||||
*
|
||||
*/
|
||||
|
||||
const numberOfDigit = (n) => Math.abs(n).toString().length
|
||||
|
||||
export { numberOfDigit }
|
9
Maths/PerfectCube.js
Normal file
9
Maths/PerfectCube.js
Normal file
@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Author: dephraiim
|
||||
* License: GPL-3.0 or later
|
||||
*
|
||||
*/
|
||||
|
||||
const perfectCube = (num) => Math.round(num ** (1 / 3)) ** 3 === num
|
||||
|
||||
export { perfectCube }
|
30
Maths/PerfectNumber.js
Normal file
30
Maths/PerfectNumber.js
Normal file
@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Author: dephraiim
|
||||
* License: GPL-3.0 or later
|
||||
*
|
||||
* == Perfect Number ==
|
||||
* In number theory, a perfect number is a positive integer that is equal to the sum of
|
||||
* its positive divisors(factors), excluding the number itself.
|
||||
* For example: 6 ==> divisors[1, 2, 3, 6]
|
||||
* Excluding 6, the sum(divisors) is 1 + 2 + 3 = 6
|
||||
* So, 6 is a Perfect Number
|
||||
* Other examples of Perfect Numbers: 28, 486, ...
|
||||
*
|
||||
* More on Perfect Number:
|
||||
* https://en.wikipedia.org/wiki/Perfect_number
|
||||
*
|
||||
*/
|
||||
|
||||
const factorsExcludingNumber = (n) => {
|
||||
return [...Array(n).keys()].filter((num) => n % num === 0)
|
||||
}
|
||||
|
||||
const perfectNumber = (n) => {
|
||||
const factorSum = factorsExcludingNumber(n).reduce((num, initialValue) => {
|
||||
return num + initialValue
|
||||
}, 0)
|
||||
|
||||
return factorSum === n
|
||||
}
|
||||
|
||||
export { perfectNumber }
|
9
Maths/PerfectSquare.js
Normal file
9
Maths/PerfectSquare.js
Normal file
@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Author: dephraiim
|
||||
* License: GPL-3.0 or later
|
||||
*
|
||||
*/
|
||||
|
||||
const perfectSquare = (num) => Math.sqrt(num) ** 2 === num
|
||||
|
||||
export { perfectSquare }
|
14
Maths/test/ArmstrongNumber.test.js
Normal file
14
Maths/test/ArmstrongNumber.test.js
Normal file
@ -0,0 +1,14 @@
|
||||
import { armstrongNumber } from '../ArmstrongNumber'
|
||||
|
||||
describe('ArmstrongNumber', () => {
|
||||
it('should return true for an armstrong number', () => {
|
||||
expect(armstrongNumber(371)).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should return false for a non-armstrong number', () => {
|
||||
expect(armstrongNumber(300)).toBeFalsy()
|
||||
})
|
||||
it('should return false for negative values', () => {
|
||||
expect(armstrongNumber(-2)).toBeFalsy()
|
||||
})
|
||||
})
|
10
Maths/test/Factors.test.js
Normal file
10
Maths/test/Factors.test.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { factorsOfANumber } from '../Factors'
|
||||
|
||||
describe('Factors', () => {
|
||||
factorsOfANumber(50).forEach((num) => {
|
||||
it(`${num} is a factor of 50`, () => {
|
||||
const isFactor = 50 % num === 0
|
||||
expect(isFactor).toBeTruthy()
|
||||
})
|
||||
})
|
||||
})
|
11
Maths/test/NumberOfDigits.test.js
Normal file
11
Maths/test/NumberOfDigits.test.js
Normal file
@ -0,0 +1,11 @@
|
||||
import { numberOfDigit } from '../NumberOfDigits'
|
||||
|
||||
describe('NumberOfDigits', () => {
|
||||
it('should return the correct number of digits for an integer', () => {
|
||||
expect(numberOfDigit(1234000)).toBe(7)
|
||||
})
|
||||
|
||||
it('should return the correct number of digits for a negative number', () => {
|
||||
expect(numberOfDigit(-2346243)).toBe(7)
|
||||
})
|
||||
})
|
10
Maths/test/PerfectCube.test.js
Normal file
10
Maths/test/PerfectCube.test.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { perfectCube } from '../PerfectCube'
|
||||
|
||||
describe('PerfectCube', () => {
|
||||
it('should return true for a perfect cube', () => {
|
||||
expect(perfectCube(125)).toBeTruthy()
|
||||
})
|
||||
it('should return false for a non perfect cube', () => {
|
||||
expect(perfectCube(100)).toBeFalsy()
|
||||
})
|
||||
})
|
10
Maths/test/PerfectNumber.test.js
Normal file
10
Maths/test/PerfectNumber.test.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { perfectNumber } from '../PerfectNumber'
|
||||
|
||||
describe('PerfectNumber', () => {
|
||||
it('should return true for a perfect cube', () => {
|
||||
expect(perfectNumber(28)).toBeTruthy()
|
||||
})
|
||||
it('should return false for a non perfect cube', () => {
|
||||
expect(perfectNumber(10)).toBeFalsy()
|
||||
})
|
||||
})
|
10
Maths/test/PerfectSquare.test.js
Normal file
10
Maths/test/PerfectSquare.test.js
Normal file
@ -0,0 +1,10 @@
|
||||
import { perfectSquare } from '../PerfectSquare'
|
||||
|
||||
describe('PerfectSquare', () => {
|
||||
it('should return true for a perfect cube', () => {
|
||||
expect(perfectSquare(16)).toBeTruthy()
|
||||
})
|
||||
it('should return false for a non perfect cube', () => {
|
||||
expect(perfectSquare(10)).toBeFalsy()
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user