Here's an algorithm that you can use to create a function that calculates the LCM (Least Common Multiple) without using the built-in lcm
function in MATLAB:
main.m411 chars19 lines
This function works by first finding the prime factors of both input numbers (a
and b
), and then combining them while removing duplicates. The LCM is then calculated by raising each prime factor to the highest power found in either a
or b
, and multiplying all of the resulting numbers together.
Note that this function assumes that the input arguments a
and b
are positive integers.
gistlibby LogSnag