Post: JavaScript: finding the least common multiple

Lately, I’ve been testing my JavaScript knowledge by solving programming problems at Project Euler. While solving the first, quite elementary, practices, it occurred to that some of them can prove useful in real-world applications. So, I decided to extract the useful code from these exercises and release them in nicely packed libraries for use in any JavaScript application.

In this first post in what hopefully will become a series, I release a script to find the least common multiple of a list of numbers. A function to get the least common multiple of a set of numbers is very rudimentary, I know, yet surprisingly lacking from the Math object included in a standard JavaScript installation. I hope that this little pre-made and pre-tested library will save its users the time of writing it themselves.

The small function in this file will add an lcm function to the Math object. You can call the Math.lcm function with one, two or more integers as parameters, and it will return the least common multiple of the given numbers. For example, Math.lcm(3, 4); will return 12, Math.lcm(1, 2, 3, 4, 5, 6); will return 60.

Advertisement

Comments on "JavaScript: finding the least common multiple"

RSS

There's one comment to this post. Add yours below.

Replies

Trackbacks

The trackback URI of this post is http://jw.x10hosting.com/blog/2007/08/22/javascript-finding-the-least-common-multiple/trackback/

Pingbacks

Leave a reply on "JavaScript: finding the least common multiple"

Feel free to express your opinions, remarks, comments, ideas, observations, notes, interpretations, thoughts, love or hate here. You can even use Textile to format your comments, some nice shortcuts are built in into the toolbar below.

Concerning spam: don't provide links to your website if it's completely irrelevant, I will remove links to websites that for instance provide loans or cheap insurances. Please also write your comment in clear English, not in some gibberish English no one really understands. Comments are filtered by Spam Karma 2 to prevent all kinds of spam.

(This will not be published.)

Textile controls (requires JavaScript):