Finding smallest integer division?

J

Jari Williamsson

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 Williamsson
 
T

ThoML

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)}
 
R

Rob Biedenharn

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 Williamsson

Ask Google about "least common multiple"

-Rob

Rob Biedenharn http://agileconsultingllc.com
(e-mail address removed)
 
R

Robert Klemme

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
 
J

Jari Williamsson

Robert said:
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.

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


Best regards,

Jari Williamsson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top