This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,21 @@
# This solution cheats and uses only one generator!
def powers(m)
return enum_for(:powers, m) unless block_given?
n = 0
loop { yield n ** m; n += 1 }
end
def squares_without_cubes
return enum_for(:squares_without_cubes) unless block_given?
cubes = powers(3)
c = cubes.next
powers(2) do |s|
(c = cubes.next) until c >= s
yield s unless c == s
end
end
p squares_without_cubes.take(30).drop(20)

View file

@ -0,0 +1,25 @@
# This solution uses three generators.
def powers(m)
return enum_for(:powers, m) unless block_given?
n = 0
loop { yield n ** m; n += 1 }
end
def squares_without_cubes
return enum_for(:squares_without_cubes) unless block_given?
cubes = powers(3)
c = cubes.next
squares = powers(2)
loop do
s = squares.next
(c = cubes.next) until c >= s
yield s unless c == s
end
end
answer = squares_without_cubes
20.times { answer.next }
p (1..10).map { answer.next }