Thursday, April 17, 2008

Short math program I remember writing but I don't remember why

I was cleaning off my desktop and I came across this ruby script I wrote June 5th, 2007. I vaguely remember writing this but I have no recollection why. But if you ever needed the ruby code to generate the numbers divisible by one number and not by another, here it is. At least that's what I think this code does.


small = 13
big = 17

upper=1000000
start = Time.now
def printOnlyDivisibleBy(top, divBy, notBy)
a = (1..(top/divBy)/notBy).inject([]){|outerList, x|
(-notBy+1..-1).inject(outerList){|innerList, y|
innerList << divBy*(y+notBy*x)}}
(1..top/divBy%notBy).inject(a){|soloList, y|
soloList << divBy*(y+notBy*(top/divBy/notBy))}
if top/divBy%notBy != 0
a
end

a1 = printOnlyDivisibleBy(upper, small, big)
a2 = printOnlyDivisibleBy(upper, big, small)
a3 = (1..upper/big/small).collect {|x| small*big*x}

final = Time.now
puts a1.size
puts a2.size
puts a3.size
puts final-start

No comments: