Finding smallest integer division?

In Ruby, what would be the most efficient way to find the smallest
number that an array of different numbers can be divided by with integer
math?

For example:
[1,2] would return 2
[1,2,3] would return 6
[1,2,4] would return 4
[1,2,3,4] would return 12

Best regards,

Jari W.

what would be the most efficient way to find the smallest
number that an array of different numbers can be divided by with integer
math?

Are you looking for the lcm?

[1,2,4].inject(1) {|l, n| l.lcm(n)}

On Feb 15, 2008, at 1:49 PM, Jari W. wrote:

Best regards,

Jari W.

Ask Google about “least common multiple”

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

On 15.02.2008 19:49, Jari W. wrote:

In Ruby, what would be the most efficient way to find the smallest
number that an array of different numbers can be divided by with integer
math?

For example:
[1,2] would return 2
[1,2,3] would return 6
[1,2,4] would return 4
[1,2,3,4] would return 12

Somehow your wording puzzles me: you write that you look for the
smallest number that an array can be divided by but yet you rather seem
to be looking for the smallest number that can be divided by all numbers
in the array. I think Rob is spot on with his suggestion.

Kind regards

robert

Robert K. wrote:

Somehow your wording puzzles me: you write that you look for the
smallest number that an array can be divided by but yet you rather seem
to be looking for the smallest number that can be divided by all numbers
in the array. I think Rob is spot on with his suggestion.

Yepp, I got it reversed. Using inject and lcm worked great!

Best regards,

Jari W.