Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Pranav changes#1839

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
pranavdate8788 wants to merge2 commits intoTheAlgorithms:master
base:master
Choose a base branch
Loading
frompranavdate8788:pranav-changes
Open
Show file tree
Hide file tree
Changes fromall 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
36 changes: 36 additions & 0 deletionsMaths/LCM.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
/**
* @function lcm
* @description Compute the least common multiple (LCM) of two integers.
* @param {number} a - first integer
* @param {number} b - second integer
* @return {number} LCM of a and b
* @example lcm(4,6) -> 12
* @example lcm(7,3) -> 21
*/
const gcd = (x, y) => {
let a = Math.abs(x)
let b = Math.abs(y)
if (Number.isNaN(a) || Number.isNaN(b)) throw new TypeError('Argument is NaN - Not a Number')
while (b !== 0) {
const t = a % b
a = b
b = t
}
return a
}

const lcm = (a, b) => {
const na = Number(a)
const nb = Number(b)

if (Number.isNaN(na) || Number.isNaN(nb) || typeof a === 'object' || typeof b === 'object') {
throw new TypeError('Argument is NaN - Not a Number')
}

if (na === 0 || nb === 0) return 0

// lcm(a,b) = |a * b| / gcd(a,b)
return Math.abs((na / gcd(na, nb)) * nb)
}

export { lcm }
28 changes: 28 additions & 0 deletionsMaths/test/LCM.test.js
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
import { describe, it, expect } from 'vitest'
import { lcm } from '../LCM'

describe('lcm()', () => {
it('returns 0 when either argument is 0', () => {
expect(lcm(0, 5)).toBe(0)
expect(lcm(7, 0)).toBe(0)
})

it('computes LCM for positive integers', () => {
expect(lcm(4, 6)).toBe(12)
expect(lcm(7, 3)).toBe(21)
expect(lcm(21, 6)).toBe(42)
})

it('computes LCM when inputs are negative', () => {
expect(lcm(-4, 6)).toBe(12)
expect(lcm(4, -6)).toBe(12)
expect(lcm(-4, -6)).toBe(12)
})

it('throws for non-numeric inputs', () => {
// @ts-ignore
expect(() => lcm('a', 5)).toThrow()
// @ts-ignore
expect(() => lcm(4, {})).toThrow()
})
})
4 changes: 2 additions & 2 deletionspackage.json
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,11 +13,11 @@
"author": "TheAlgorithms",
"license": "GPL-3.0",
"devDependencies": {
"@vitest/coverage-v8": "^1.2.1",
"globby": "^13.2.2",
"husky": "^8.0.3",
"prettier": "^3.0.3",
"vitest": "^1.2.1",
"@vitest/coverage-v8": "^1.2.1"
"vitest": "^3.2.4"
},
"engines": {
"node": ">=20.6.0"
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp