Skip to content

Commit 8c27d86

Browse files
Psytewpatrickwestervelt
andauthored
enhancement: FindLCM algorithm (TheAlgorithms#1222)
Co-authored-by: patrickwestervelt <pwestervelt3@gatech.edu>
1 parent 63a3394 commit 8c27d86

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

Maths/FindLcm.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@
1111

1212
'use strict'
1313

14+
import { findHCF } from './findHcf'
15+
1416
// Find the LCM of two numbers.
1517
const findLcm = (num1, num2) => {
1618
// If the input numbers are less than 1 return an error message.
1719
if (num1 < 1 || num2 < 1) {
18-
return 'Please enter values greater than zero.'
20+
throw Error('Numbers must be positive.')
1921
}
2022

2123
// If the input numbers are not integers return an error message.
2224
if (num1 !== Math.round(num1) || num2 !== Math.round(num2)) {
23-
return 'Please enter whole numbers.'
25+
throw Error('Numbers must be whole.')
2426
}
2527

2628
// Get the larger number between the two
@@ -33,4 +35,19 @@ const findLcm = (num1, num2) => {
3335
}
3436
}
3537

36-
export { findLcm }
38+
// Typically, but not always, more efficient
39+
const findLcmWithHcf = (num1, num2) => {
40+
// If the input numbers are less than 1 return an error message.
41+
if (num1 < 1 || num2 < 1) {
42+
throw Error('Numbers must be positive.')
43+
}
44+
45+
// If the input numbers are not integers return an error message.
46+
if (num1 !== Math.round(num1) || num2 !== Math.round(num2)) {
47+
throw Error('Numbers must be whole.')
48+
}
49+
50+
return num1 * num2 / findHCF(num1, num2)
51+
}
52+
53+
export { findLcm, findLcmWithHcf }

Maths/test/FindLcm.test.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,39 @@
1-
import { findLcm } from '../FindLcm'
1+
import { findLcm, findLcmWithHcf } from '../FindLcm'
22

33
describe('findLcm', () => {
44
it('should throw a statement for values less than 1', () => {
5-
expect(findLcm(0, 0)).toBe('Please enter values greater than zero.')
5+
expect(() => { findLcm(0, 0) }).toThrow(Error)
66
})
77

88
it('should throw a statement for one value less than 1', () => {
9-
expect(findLcm(1, 0)).toBe('Please enter values greater than zero.')
10-
expect(findLcm(0, 1)).toBe('Please enter values greater than zero.')
9+
expect(() => { findLcm(1, 0) }).toThrow(Error)
10+
expect(() => { findLcm(0, 1) }).toThrow(Error)
1111
})
1212

1313
it('should return an error for values non-integer values', () => {
14-
expect(findLcm(4.564, 7.39)).toBe('Please enter whole numbers.')
14+
expect(() => { findLcm(4.564, 7.39) }).toThrow(Error)
1515
})
1616

1717
it('should return the LCM of two given integers', () => {
1818
expect(findLcm(27, 36)).toBe(108)
1919
})
2020
})
21+
22+
describe('findLcmWithHcf', () => {
23+
it('should throw a statement for values less than 1', () => {
24+
expect(() => { findLcmWithHcf(0, 0) }).toThrow(Error)
25+
})
26+
27+
it('should throw a statement for one value less than 1', () => {
28+
expect(() => { findLcmWithHcf(1, 0) }).toThrow(Error)
29+
expect(() => { findLcmWithHcf(0, 1) }).toThrow(Error)
30+
})
31+
32+
it('should return an error for values non-integer values', () => {
33+
expect(() => { findLcmWithHcf(4.564, 7.39) }).toThrow(Error)
34+
})
35+
36+
it('should return the LCM of two given integers', () => {
37+
expect(findLcmWithHcf(27, 36)).toBe(108)
38+
})
39+
})

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy