Counting

T

Tom Clarke

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
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

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
 
W

Will

[Note: parts of this message were removed to make it a legal post.]

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.
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

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

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

Rohit Namjoshi

[Note: parts of this message were removed to make it a legal post.]

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
 
A

Aaron Turner

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.

--
Aaron Turner
http://synfin.net/
http://tcpreplay.synfin.net/ - Pcap editing and replay tools for Unix & Windows
They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety. -- Benjamin Franklin
 
W

William James

Tom said:
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]

--
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

I like that approach, William. Thanks for the deodorant. ;-)

Craig

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

Joe Wölfel

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



Tom said:
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]
 
C

Craig Demyanovich

[Note: parts of this message were removed to make it a legal post.]

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
 
T

Todd Benson

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
 
G

Gregory Brown

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!
 
J

Joe Wölfel

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.
 
L

Lloyd Linklater

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

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,197
Messages
2,571,040
Members
47,635
Latest member
SkyePurves

Latest Threads

Top