A-M baby
This commit is contained in:
parent
764da6cbbb
commit
db842d013d
19005 changed files with 197040 additions and 7 deletions
21
Task/Generator-Exponential/Ruby/generator-exponential-1.rb
Normal file
21
Task/Generator-Exponential/Ruby/generator-exponential-1.rb
Normal 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)
|
||||
25
Task/Generator-Exponential/Ruby/generator-exponential-2.rb
Normal file
25
Task/Generator-Exponential/Ruby/generator-exponential-2.rb
Normal 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 }
|
||||
Loading…
Add table
Add a link
Reference in a new issue