Skip to content
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
56 changes: 20 additions & 36 deletions Maths/AutomorphicNumber.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,24 @@
/**
* @function isAutomorphic
* @author [SilverDragonOfR] (https://github.com/SilverDragonOfR)
*
* @see [Automorphic] (https://en.wikipedia.org/wiki/Automorphic_number)
* @description This script will check whether a number is Automorphic or not
* @description A number n is said to be a Automorphic number if the square of n ends in the same digits as n itself.
*
* @param {Integer} n - the n for nth Catalan Number
* @return {Integer} - the nth Catalan Number
* @complexity Time: O(log10(n)) , Space: O(1)
*
* @convention We define Automorphic only for whole number integers. For negetive integer we return False. For float or String we show error.
* @examples 0, 1, 5, 6, 25, 76, 376, 625, 9376 are some Automorphic numbers
* @function isAutomorphicNumber
* @description An automorphic number is a number whose square ends
* in the same digits as the number itself.
* For example, 5² = 25, 6² = 36, 76² = 5776.
* @param {number} n - A positive integer
* @returns {boolean} - True if n is an automorphic number, false otherwise
* @see https://en.wikipedia.org/wiki/Automorphic_number
* @example isAutomorphicNumber(5) => true
* @example isAutomorphicNumber(6) => true
* @example isAutomorphicNumber(76) => true
* @example isAutomorphicNumber(7) => false
*/

// n is the number to be checked
export const isAutomorphic = (n) => {
if (typeof n !== 'number') {
throw new Error('Type of n must be number')
}
if (!Number.isInteger(n)) {
throw new Error('n cannot be a floating point number')
const isAutomorphicNumber = (n) => {
if (!Number.isInteger(n) || n < 1) {
throw new TypeError('Input must be a positive integer')
}
if (n < 0) {
return false
}

// now n is a whole number integer >= 0
let n_sq = n * n
while (n > 0) {
if (n % 10 !== n_sq % 10) {
return false
}
n = Math.floor(n / 10)
n_sq = Math.floor(n_sq / 10)
}

return true
const square = n * n
const digits = String(n).length
const lastDigits = square % Math.pow(10, digits)
return lastDigits === n
}

export { isAutomorphicNumber }
63 changes: 43 additions & 20 deletions Maths/test/AutomorphicNumber.test.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,51 @@
import { isAutomorphic } from '../AutomorphicNumber'
import { isAutomorphicNumber } from '../AutomorphicNumber.js'

describe('AutomorphicNumber', () => {
it('should throw Error when n is String', () => {
expect(() => isAutomorphic('qwerty')).toThrow()
describe('isAutomorphicNumber', () => {
test('1 is automorphic (1² = 1)', () => {
expect(isAutomorphicNumber(1)).toBe(true)
})
it('should throw Error when n is floating point', () => {
expect(() => isAutomorphic(13.6)).toThrow()

test('5 is automorphic (5² = 25)', () => {
expect(isAutomorphicNumber(5)).toBe(true)
})

test('6 is automorphic (6² = 36)', () => {
expect(isAutomorphicNumber(6)).toBe(true)
})

test('25 is automorphic (25² = 625)', () => {
expect(isAutomorphicNumber(25)).toBe(true)
})

test('76 is automorphic (76² = 5776)', () => {
expect(isAutomorphicNumber(76)).toBe(true)
})

test('376 is automorphic (376² = 141376)', () => {
expect(isAutomorphicNumber(376)).toBe(true)
})

test('2 is NOT automorphic (2² = 4)', () => {
expect(isAutomorphicNumber(2)).toBe(false)
})

test('7 is NOT automorphic (7² = 49)', () => {
expect(isAutomorphicNumber(7)).toBe(false)
})

test('10 is NOT automorphic', () => {
expect(isAutomorphicNumber(10)).toBe(false)
})

test('throws TypeError for zero', () => {
expect(() => isAutomorphicNumber(0)).toThrow(TypeError)
})

test.each([
{ n: -3, expected: false },
{ n: -25, expected: false }
])('should return false when n is negetive', ({ n, expected }) => {
expect(isAutomorphic(n)).toBe(false)
test('throws TypeError for negative input', () => {
expect(() => isAutomorphicNumber(-5)).toThrow(TypeError)
})

test.each([
{ n: 7, expected: false },
{ n: 83, expected: false },
{ n: 0, expected: true },
{ n: 1, expected: true },
{ n: 376, expected: true },
{ n: 90625, expected: true }
])('should return $expected when n is $n', ({ n, expected }) => {
expect(isAutomorphic(n)).toBe(expected)
test('throws TypeError for float input', () => {
expect(() => isAutomorphicNumber(5.5)).toThrow(TypeError)
})
})
Loading