Skip to content

enhancement: FindLCM algorithm #1222

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2022
Merged
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
23 changes: 20 additions & 3 deletions Maths/FindLcm.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@

'use strict'

import { findHCF } from './findHcf'

// Find the LCM of two numbers.
const findLcm = (num1, num2) => {
// If the input numbers are less than 1 return an error message.
if (num1 < 1 || num2 < 1) {
return 'Please enter values greater than zero.'
throw Error('Numbers must be positive.')
}

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

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

export { findLcm }
// Typically, but not always, more efficient
const findLcmWithHcf = (num1, num2) => {
// If the input numbers are less than 1 return an error message.
if (num1 < 1 || num2 < 1) {
throw Error('Numbers must be positive.')
}

// If the input numbers are not integers return an error message.
if (num1 !== Math.round(num1) || num2 !== Math.round(num2)) {
throw Error('Numbers must be whole.')
}

return num1 * num2 / findHCF(num1, num2)
}

export { findLcm, findLcmWithHcf }
29 changes: 24 additions & 5 deletions Maths/test/FindLcm.test.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,39 @@
import { findLcm } from '../FindLcm'
import { findLcm, findLcmWithHcf } from '../FindLcm'

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

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

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

it('should return the LCM of two given integers', () => {
expect(findLcm(27, 36)).toBe(108)
})
})

describe('findLcmWithHcf', () => {
it('should throw a statement for values less than 1', () => {
expect(() => { findLcmWithHcf(0, 0) }).toThrow(Error)
})

it('should throw a statement for one value less than 1', () => {
expect(() => { findLcmWithHcf(1, 0) }).toThrow(Error)
expect(() => { findLcmWithHcf(0, 1) }).toThrow(Error)
})

it('should return an error for values non-integer values', () => {
expect(() => { findLcmWithHcf(4.564, 7.39) }).toThrow(Error)
})

it('should return the LCM of two given integers', () => {
expect(findLcmWithHcf(27, 36)).toBe(108)
})
})