# Finding smallest integer division?

**URL:** <https://www.ruby-forum.com/t/finding-smallest-integer-division/129249>\
**Category:** Ruby\
**Created:** [February 15, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/finding-smallest-integer-division/129249 "2008-02-15T19:50:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 15, 2008, 7:50pm UTC](https://www.ruby-forum.com/t/finding-smallest-integer-division/129249/1 "2008-02-15T19:50:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 15, 2008, 8:17pm UTC](https://www.ruby-forum.com/t/finding-smallest-integer-division/129249/2 "2008-02-15T20:17:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 15, 2008, 8:18pm UTC](https://www.ruby-forum.com/t/finding-smallest-integer-division/129249/3 "2008-02-15T20:18:50Z")

</div>

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](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 15, 2008, 11:20pm UTC](https://www.ruby-forum.com/t/finding-smallest-integer-division/129249/4 "2008-02-15T23:20:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [February 15, 2008, 11:42pm UTC](https://www.ruby-forum.com/t/finding-smallest-integer-division/129249/5 "2008-02-15T23:42:26Z")

</div>

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.
