Forum: Ruby Counting

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B5234b4e1be5824cbf77b566eb613bbc?d=identicon&s=25 Tom Clarke (jokertom)
on 2008-10-17 23:44
How would i go about making Ruby count to say 1000 usin only multiples
of say 2 and 6. Then i am looking to add all of the outputted numbers.
Can anyone help
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-10-18 00:08
(Received via mailing list)
Here's one way:

multiples = []
(1..1000).each do |number|
  if (number % 2) == 0 && (number % 6) == 0
    multiples << number
  end
end
puts "multiples of 2 and 6 between 1 and 1000: #{multiples.join(', ')}"
print "sum of multiples: "
puts multiples.inject(0) { |sum, multiple| sum + multiple }

Regards,
Craig
6dc99607cfc06eed4636695a0d16531e?d=identicon&s=25 Will (Guest)
on 2008-10-18 00:10
(Received via mailing list)
Can you elaborate a bit more?
Are you looking for all multiples of 2 and 6 under 1000?
Mathematically,
that's just all multiples of 6 under 1000, so you could do something
like
n=6
i = 1
array = []
while n*i < 1000 do
  array.push(n*i)
  i += 1
end
p array

I'm not sure what you're asking, but maybe if you can give an example of
what you're looking for I can be more useful.

On Fri, Oct 17, 2008 at 4:44 PM, Tom Clarke
9d4960f8319664f0f7896230eebace73?d=identicon&s=25 Glen Holcomb (Guest)
on 2008-10-18 00:11
(Received via mailing list)
On Fri, Oct 17, 2008 at 4:07 PM, Craig Demyanovich
<cdemyanovich@gmail.com>wrote:

> puts multiples.inject(0) { |sum, multiple| sum + multiple }
>
> Regards,
> Craig
>

You should be able to ignore the % 2 bit as if it's divisible by 6 then
it's
divisible by 2

--
"Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can't hear a word you're saying."

-Greg Graffin (Bad Religion)
0f36963d10e423a204db6aa255fd308f?d=identicon&s=25 Rohit Namjoshi (Guest)
on 2008-10-18 00:12
(Received via mailing list)
Using the fact that a multiple of 6 must be a multiple of 2 - apart from
2
and 4:

(6..1000).inject(0) {|sum, i| i % 6 == 0 ? sum + i : sum} + 6

On Fri, Oct 17, 2008 at 4:44 PM, Tom Clarke
B5234b4e1be5824cbf77b566eb613bbc?d=identicon&s=25 Tom Clarke (jokertom)
on 2008-10-18 00:31
Sorry i meant to say 2 and 5
Ef3aa7f7e577ea8cd620462724ddf73b?d=identicon&s=25 Rob Biedenharn (Guest)
on 2008-10-18 01:21
(Received via mailing list)
On Oct 17, 2008, at 6:31 PM, Tom Clarke wrote:

> Sorry i meant to say 2 and 5


You should just have the professor post the assignment directly and it
would avoid such confusion.

-Rob

Rob Biedenharn    http://agileconsultingllc.com
Rob@AgileConsultingLLC.com
B5234b4e1be5824cbf77b566eb613bbc?d=identicon&s=25 Tom Clarke (jokertom)
on 2008-10-18 01:22
Rob Biedenharn wrote:
> On Oct 17, 2008, at 6:31 PM, Tom Clarke wrote:
>
>> Sorry i meant to say 2 and 5
>
>
> You should just have the professor post the assignment directly and it
> would avoid such confusion.
>
> -Rob
>
> Rob Biedenharn    http://agileconsultingllc.com
> Rob@AgileConsultingLLC.com

what youmean the proffesor
3b1756d05466b4a78afd9aea7bb845c2?d=identicon&s=25 Aaron Turner (Guest)
on 2008-10-18 01:31
(Received via mailing list)
On Fri, Oct 17, 2008 at 4:21 PM, Tom Clarke
<thomas.clarke08@hotmail.co.uk> wrote:
>>
>> Rob Biedenharn    http://agileconsultingllc.com
>> Rob@AgileConsultingLLC.com
>
> what youmean the proffesor

Professor: A teacher or instructor usually in a class room environment
who hands out homework assignments asking for arbitrary and generally
worthless tasks to be completed like the one you have posted.

Ie: If you want us to do your homework for you, just paste in the
question next time.
2ee1a7960cc761a6e92efb5000c0f2c9?d=identicon&s=25 William James (Guest)
on 2008-10-18 14:40
(Received via mailing list)
Tom Clarke wrote:

> How would i go about making Ruby count to say 1000 usin only multiples
> of say 2 and 6. Then i am looking to add all of the outputted numbers.
> Can anyone help

"We don't need no stinkin' loops!"

(1..1000).select{|n| 0 == n % 10 }
    ==>
[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140,
150, 160, 170, 180, 190, 200, 210, 220, 230, 240, 250, 260, 270,
280, 290, 300, 310, 320, 330, 340, 350, 360, 370, 380, 390, 400,
410, 420, 430, 440, 450, 460, 470, 480, 490, 500, 510, 520, 530,
540, 550, 560, 570, 580, 590, 600, 610, 620, 630, 640, 650, 660,
670, 680, 690, 700, 710, 720, 730, 740, 750, 760, 770, 780, 790,
800, 810, 820, 830, 840, 850, 860, 870, 880, 890, 900, 910, 920,
930, 940, 950, 960, 970, 980, 990, 1000]
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-10-18 15:28
(Received via mailing list)
I like that approach, William. Thanks for the deodorant. ;-)

Craig

P.S. The OP wanted n % 6, not n % 10.
73ba5e17cf39ee5aa4ada6eefdb80177?d=identicon&s=25 Joe Wölfel (Guest)
on 2008-10-18 16:19
(Received via mailing list)
Don't need no stinkin' select either;)  It's much faster if you don't
iterate at all.

n=1000
d = 10
(n+d)*(n/(d*2))
=> 50500

The formula is all you need because
1000 + 10 = 1010
990 + 20 = 1010
etc.  50 times.

The following select gives the same answer but is much slower.
(1..n).select{|a| 0 == a % d }.inject {|sum, x| sum+x}
=> 50500

One caveat.  The formula I gave assumes that 'n' is divisible by
'd'.  It shouldn't be hard to change the formula so that isn't
necessary though.

Cheers,
Joe
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-10-18 17:04
(Received via mailing list)
The OP needs multiples of 2 and 6. Since all multiples of 6 are also
multiples of 2, just checking for multiples of 6 suffices. However, if d
=
6, your two approaches don't produce the same result. For d = 6, they
produce 83498 and 83166, respectively. 83166 is the correct answer, I
believe.

Regards,
Craig
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-10-18 22:31
(Received via mailing list)
On Sat, Oct 18, 2008 at 10:02 AM, Craig Demyanovich
<cdemyanovich@gmail.com> wrote:
> The OP needs multiples of 2 and 6. Since all multiples of 6 are also
> multiples of 2, just checking for multiples of 6 suffices. However, if d =
> 6, your two approaches don't produce the same result. For d = 6, they
> produce 83498 and 83166, respectively. 83166 is the correct answer, I
> believe.

Well, I don't think he meant all multiples of (2 AND 6), or in this
case (2 AND 5).  That will always be simply always be multiples of the
LCM, which, for (2 AND 6) is 6, for (12 AND 18) is 36, for (2 AND 5),
10, etc.

lcm = 2.lcm 5
puts (1..1000/lcm).map.inject {|s, i| s + i * lcm}

I think he meant (multiples of 2) AND (multiples of 6), or in this
case (multiples of 2) AND (multiples of 5), which would be multiples
of either.  In that case check whether the number is cleanly divisible
by number a and number b using %.

Todd
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2008-10-18 22:35
(Received via mailing list)
On Sat, Oct 18, 2008 at 3:30 PM, Todd Benson <caduceass@gmail.com>
wrote:
> I think he meant (multiples of 2) AND (multiples of 6), or in this
> case (multiples of 2) AND (multiples of 5)

With 'he', I meant the OP.

Todd
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2008-10-19 00:30
(Received via mailing list)
On Fri, Oct 17, 2008 at 5:44 PM, Tom Clarke
<thomas.clarke08@hotmail.co.uk> wrote:
> How would i go about making Ruby count to say 1000 usin only multiples
> of say 2 and 6. Then i am looking to add all of the outputted numbers.
> Can anyone help

FizzBuzz FAIL!
73ba5e17cf39ee5aa4ada6eefdb80177?d=identicon&s=25 Joe Wölfel (Guest)
on 2008-10-20 16:20
(Received via mailing list)
Right, as per my caveat n must be divisible by d*2.   You can't
divide 1000 by twelve.  But 996 is divisible by 12.   So you can use
that for d instead for that particular case.  If you played around
with remainders for a bit you could adjust the formula I gave to work
for all cases.

The point is you go from linear time for all the iterative methods to
constant time if you just work out a formula.  For very large numbers
the iterative methods will take more time than you are willing to
spend and perhaps never finish at all.
84dc575c33a123789521d53cad0f62ae?d=identicon&s=25 Lloyd Linklater (lloyd)
on 2008-10-21 17:16
Tom Clarke wrote:
> How would i go about making Ruby count to say 1000 usin only multiples
> of say 2 and 6. Then i am looking to add all of the outputted numbers.
> Can anyone help

If you want numbers that are divisible by 2 *or* 6, then that is all
even numbers.  If you want numbers that are divisible by 2 *and* 6, then
that you really need only to check multiples of 6.  Why can't you use
something like this:

6.step(1000, 6)

You need only start with 6 to find multiples.  Every sixth number will
be a multiple.
This topic is locked and can not be replied to.