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
26 changes: 26 additions & 0 deletions Maths/TechNumber.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @function isTechNumber
* @description A tech number is an even digit number where if the number
* is split into two halves, the square of the sum of the two halves
* equals the original number.
* For example, 2025: split into 20 and 25, (20+25)² = 45² = 2025.
* @param {number} n - A positive integer with even number of digits
* @returns {boolean} - True if n is a tech number, false otherwise
* @see https://www.geeksforgeeks.org/tech-number/
* @example isTechNumber(2025) => true
* @example isTechNumber(3025) => true
* @example isTechNumber(1234) => false
*/
const isTechNumber = (n) => {
if (!Number.isInteger(n) || n < 1) {
throw new TypeError('Input must be a positive integer')
}
const str = String(n)
if (str.length % 2 !== 0) return false
const half = str.length / 2
const left = Number(str.slice(0, half))
const right = Number(str.slice(half))
return Math.pow(left + right, 2) === n
}

export { isTechNumber }
43 changes: 43 additions & 0 deletions Maths/test/TechNumber.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { isTechNumber } from '../TechNumber.js'

describe('isTechNumber', () => {
test('2025 is a tech number (20+25=45, 45²=2025)', () => {
expect(isTechNumber(2025)).toBe(true)
})

test('3025 is a tech number (30+25=55, 55²=3025)', () => {
expect(isTechNumber(3025)).toBe(true)
})

test('9801 is a tech number (98+01=99, 99²=9801)', () => {
expect(isTechNumber(9801)).toBe(true)
})

test('1234 is NOT a tech number', () => {
expect(isTechNumber(1234)).toBe(false)
})

test('odd digit numbers are NOT tech numbers', () => {
expect(isTechNumber(123)).toBe(false)
})

test('100 is NOT a tech number', () => {
expect(isTechNumber(100)).toBe(false)
})

test('81 is a tech number (8+1=9, 9²=81)', () => {
expect(isTechNumber(81)).toBe(true)
})

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

test('throws TypeError for negative input', () => {
expect(() => isTechNumber(-2025)).toThrow(TypeError)
})

test('throws TypeError for float input', () => {
expect(() => isTechNumber(20.25)).toThrow(TypeError)
})
})
Loading