View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Least Common Multiple Set

5.0
5.0 | 8 ratings Rate this file 13 Downloads (last 30 days) File Size: 1.26 KB File ID: #24670 Version: 1.3

Least Common Multiple Set

by

Josh (view profile)

 

08 Jul 2009 (Updated )

Input a set of numbers as an n-dimension array, get their least common multiple.

| Watch this File

File Information
Description

I wanted to find the least common multiple of a group of more than two numbers. Matlab's built in lcm function does not have this capability. It accepts two integer inputs, but cannot handle more. So I wrote lcms. lcms uses the prime factorization method to determine the least common multiple of a set of numbers. It is pretty simple.

z = lcms(numberArray)

This function accepts any n-dimensional array of natural numbers as input (Zeros do not change the output of the program).

Example usage of lcms:

>> lcms([1 2 3 4 6 8 12 24])

ans =

   24

>> A = [5 6; 8 10; 12 14]

A =

     5 6
     8 10
    12 14

>> lcms(A)

ans =

   840

----

Having a 0 in your input array will not affect the program.

example:

lcms([0 1 2 3]) returns a 6.
lcms([1 2 3]) also returns a 6.

Having negative numbers or non-integer types in your input error will result in an error message.

example:

lcms([2.5 3 2 8]) results in an error.
lcms([5 2 -3 1]) results in an error.

Acknowledgements

This file inspired N Argument Least Common Multiple Calculator With Arbitrary Precision.

MATLAB release MATLAB 7.8 (R2009a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
18 Feb 2017 Raul Emir Gutierrez Lopez

03 Feb 2017 Danilo Gaspar

Very useful. Thank you.

06 Jun 2016 Thong

Thong (view profile)

04 May 2016 sarah southern

Thank you! I wish I'd found this 12 hours ago :)

04 Mar 2016 Yavor Kamer

31 Dec 2014 Muhammad Salman Bashir

19 Jul 2011 Justin

Justin (view profile)

26 Aug 2010 Liber Eleutherios

I had the same problem and found your function. Thanks, Josh, very useful indeed.

Updates
10 Jul 2009 1.3

Input is more robust. Now you use any n-dimensional array instead of a row vector.

Error behavior is more clear.

Contact us