Skip to content

Commit 78d2157

Browse files
feat: add automorphic number algorithm
1 parent 5c39e87 commit 78d2157

2 files changed

Lines changed: 63 additions & 56 deletions

File tree

‎Maths/AutomorphicNumber.js‎

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,24 @@
11
/**
2-
* @function isAutomorphic
3-
* @author [SilverDragonOfR] (https://github.com/SilverDragonOfR)
4-
*
5-
* @see [Automorphic] (https://en.wikipedia.org/wiki/Automorphic_number)
6-
* @description This script will check whether a number is Automorphic or not
7-
* @description A number n is said to be a Automorphic number if the square of n ends in the same digits as n itself.
8-
*
9-
* @param {Integer} n - the n for nth Catalan Number
10-
* @return {Integer} - the nth Catalan Number
11-
* @complexity Time: O(log10(n)) , Space: O(1)
12-
*
13-
* @convention We define Automorphic only for whole number integers. For negetive integer we return False. For float or String we show error.
14-
* @examples 0, 1, 5, 6, 25, 76, 376, 625, 9376 are some Automorphic numbers
2+
* @function isAutomorphicNumber
3+
* @description An automorphic number is a number whose square ends
4+
* in the same digits as the number itself.
5+
* For example, 5² = 25, 6² = 36, 76² = 5776.
6+
* @param {number} n - A positive integer
7+
* @returns {boolean} - True if n is an automorphic number, false otherwise
8+
* @see https://en.wikipedia.org/wiki/Automorphic_number
9+
* @example isAutomorphicNumber(5) => true
10+
* @example isAutomorphicNumber(6) => true
11+
* @example isAutomorphicNumber(76) => true
12+
* @example isAutomorphicNumber(7) => false
1513
*/
16-
17-
// n is the number to be checked
18-
export const isAutomorphic = (n) => {
19-
if (typeof n !== 'number') {
20-
throw new Error('Type of n must be number')
21-
}
22-
if (!Number.isInteger(n)) {
23-
throw new Error('n cannot be a floating point number')
14+
const isAutomorphicNumber = (n) => {
15+
if (!Number.isInteger(n) || n < 1) {
16+
throw new TypeError('Input must be a positive integer')
2417
}
25-
if (n < 0) {
26-
return false
27-
}
28-
29-
// now n is a whole number integer >= 0
30-
let n_sq = n * n
31-
while (n > 0) {
32-
if (n % 10 !== n_sq % 10) {
33-
return false
34-
}
35-
n = Math.floor(n / 10)
36-
n_sq = Math.floor(n_sq / 10)
37-
}
38-
39-
return true
18+
const square = n * n
19+
const digits = String(n).length
20+
const lastDigits = square % Math.pow(10, digits)
21+
return lastDigits === n
4022
}
23+
24+
export { isAutomorphicNumber }
Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,51 @@
1-
import { isAutomorphic } from '../AutomorphicNumber'
1+
import { isAutomorphicNumber } from '../AutomorphicNumber.js'
22

3-
describe('AutomorphicNumber', () => {
4-
it('should throw Error when n is String', () => {
5-
expect(() => isAutomorphic('qwerty')).toThrow()
3+
describe('isAutomorphicNumber', () => {
4+
test('1 is automorphic (1² = 1)', () => {
5+
expect(isAutomorphicNumber(1)).toBe(true)
66
})
7-
it('should throw Error when n is floating point', () => {
8-
expect(() => isAutomorphic(13.6)).toThrow()
7+
8+
test('5 is automorphic (5² = 25)', () => {
9+
expect(isAutomorphicNumber(5)).toBe(true)
10+
})
11+
12+
test('6 is automorphic (6² = 36)', () => {
13+
expect(isAutomorphicNumber(6)).toBe(true)
14+
})
15+
16+
test('25 is automorphic (25² = 625)', () => {
17+
expect(isAutomorphicNumber(25)).toBe(true)
18+
})
19+
20+
test('76 is automorphic (76² = 5776)', () => {
21+
expect(isAutomorphicNumber(76)).toBe(true)
22+
})
23+
24+
test('376 is automorphic (376² = 141376)', () => {
25+
expect(isAutomorphicNumber(376)).toBe(true)
26+
})
27+
28+
test('2 is NOT automorphic (2² = 4)', () => {
29+
expect(isAutomorphicNumber(2)).toBe(false)
30+
})
31+
32+
test('7 is NOT automorphic (7² = 49)', () => {
33+
expect(isAutomorphicNumber(7)).toBe(false)
34+
})
35+
36+
test('10 is NOT automorphic', () => {
37+
expect(isAutomorphicNumber(10)).toBe(false)
38+
})
39+
40+
test('throws TypeError for zero', () => {
41+
expect(() => isAutomorphicNumber(0)).toThrow(TypeError)
942
})
1043

11-
test.each([
12-
{ n: -3, expected: false },
13-
{ n: -25, expected: false }
14-
])('should return false when n is negetive', ({ n, expected }) => {
15-
expect(isAutomorphic(n)).toBe(false)
44+
test('throws TypeError for negative input', () => {
45+
expect(() => isAutomorphicNumber(-5)).toThrow(TypeError)
1646
})
1747

18-
test.each([
19-
{ n: 7, expected: false },
20-
{ n: 83, expected: false },
21-
{ n: 0, expected: true },
22-
{ n: 1, expected: true },
23-
{ n: 376, expected: true },
24-
{ n: 90625, expected: true }
25-
])('should return $expected when n is $n', ({ n, expected }) => {
26-
expect(isAutomorphic(n)).toBe(expected)
48+
test('throws TypeError for float input', () => {
49+
expect(() => isAutomorphicNumber(5.5)).toThrow(TypeError)
2750
})
2851
})

0 commit comments

Comments
 (0)