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

on 2008-10-18 01:44

on 2008-10-18 02:08

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

on 2008-10-18 02:10

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

on 2008-10-18 02:11

On Fri, Oct 17, 2008 at 4:07 PM, Craig D. <removed_email_address@domain.invalid>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)

on 2008-10-18 02:12

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

on 2008-10-18 03:21

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 B. http://agileconsultingllc.com removed_email_address@domain.invalid

on 2008-10-18 03:22

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 > > Rob B. http://agileconsultingllc.com > removed_email_address@domain.invalid what youmean the proffesor

on 2008-10-18 03:31

On Fri, Oct 17, 2008 at 4:21 PM, Tom Clarke <removed_email_address@domain.invalid> wrote: >> >> Rob B. http://agileconsultingllc.com >> removed_email_address@domain.invalid > > 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.

on 2008-10-18 16:40

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]

on 2008-10-18 17:28

I like that approach, William. Thanks for the deodorant. ;-) Craig P.S. The OP wanted n % 6, not n % 10.

on 2008-10-18 18:19

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

on 2008-10-18 19:04

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 2008-10-19 00:31

On Sat, Oct 18, 2008 at 10:02 AM, Craig D. <removed_email_address@domain.invalid> 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

on 2008-10-19 00:35

On Sat, Oct 18, 2008 at 3:30 PM, Todd B. <removed_email_address@domain.invalid> 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 2008-10-19 02:30

On Fri, Oct 17, 2008 at 5:44 PM, Tom Clarke <removed_email_address@domain.invalid> 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 2008-10-20 18:20

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.

on 2008-10-21 19: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.