RosettaCodeData/Task/Generator-Exponential/Ruby/generator-exponential-1.rb

21 lines
457 B
Ruby
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
# This solution cheats and uses only one generator!
def powers(m)
2015-02-20 00:35:01 -05:00
return enum_for(__method__, m) unless block_given?
2016-12-05 22:15:40 +01:00
0.step{|n| yield n**m}
2013-04-10 21:29:02 -07:00
end
def squares_without_cubes
2015-02-20 00:35:01 -05:00
return enum_for(__method__) unless block_given?
2013-04-10 21:29:02 -07:00
cubes = powers(3)
c = cubes.next
powers(2) do |s|
2015-02-20 00:35:01 -05:00
c = cubes.next while c < s
2013-04-10 21:29:02 -07:00
yield s unless c == s
end
end
p squares_without_cubes.take(30).drop(20)
2015-02-20 00:35:01 -05:00
# p squares_without_cubes.lazy.drop(20).first(10) # Ruby 2.0+