# Counting

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

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

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 ni < 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

On Fri, Oct 17, 2008 at 4:07 PM, Craig D.
[email protected]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

speak
so loud, I can’t hear a word you’re saying.”

Sorry i meant to say 2 and 5

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

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

Rob B. 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

what youmean the proffesor

On Fri, Oct 17, 2008 at 4:21 PM, Tom Clarke
[email protected] wrote:

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.

I like that approach, William. Thanks for the deodorant. Craig

P.S. The OP wanted n % 6, not n % 10.

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]

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/(d2))
=> 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

# 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

On Sat, Oct 18, 2008 at 3:30 PM, Todd B. [email protected]
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

On Fri, Oct 17, 2008 at 5:44 PM, Tom Clarke
[email protected] 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!

On Sat, Oct 18, 2008 at 10:02 AM, Craig D.
[email protected] 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

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.

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.