[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Counting

Tom Clarke

10/17/2008 9:44:00 PM

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
--
Posted via http://www.ruby-....

17 Answers

Craig Demyanovich

10/17/2008 10:07:00 PM

0

[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

Will Hall

10/17/2008 10:10:00 PM

0

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

On Fri, Oct 17, 2008 at 4: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
> --
> Posted via http://www.ruby-....
>
>

Glen Holcomb

10/17/2008 10:10:00 PM

0

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

On Fri, Oct 17, 2008 at 4:07 PM, Craig Demyanovich
<cdemyanovich@gmail.com>wrote:

> 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

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

Rohit Namjoshi

10/17/2008 10:12:00 PM

0

[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

On Fri, Oct 17, 2008 at 4: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
> --
> Posted via http://www.ruby-....
>
>

Tom Clarke

10/17/2008 10:31:00 PM

0

Sorry i meant to say 2 and 5
--
Posted via http://www.ruby-....

Rob Biedenharn

10/17/2008 11:20:00 PM

0

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://agileconsult...
Rob@AgileConsultingLLC.com


Tom Clarke

10/17/2008 11:22:00 PM

0

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://agileconsult...
> Rob@AgileConsultingLLC.com

what youmean the proffesor
--
Posted via http://www.ruby-....

Aaron Turner

10/17/2008 11:30:00 PM

0

On Fri, Oct 17, 2008 at 4:21 PM, Tom Clarke
<thomas.clarke08@hotmail.co.uk> wrote:
> 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://agileconsult...
>> 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.

--
Aaron Turner
http://s...
http://tcpreplay.s... - 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

William James

10/18/2008 12:35:00 PM

0

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]

--

Craig Demyanovich

10/18/2008 1:27:00 PM

0

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