Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,20 @@
# This solution cheats and uses only one generator!
def powers(m)
return enum_for(__method__, m) unless block_given?
0.step{|n| yield n**m}
end
def squares_without_cubes
return enum_for(__method__) unless block_given?
cubes = powers(3)
c = cubes.next
powers(2) do |s|
c = cubes.next while c < s
yield s unless c == s
end
end
p squares_without_cubes.take(30).drop(20)
# p squares_without_cubes.lazy.drop(20).first(10) # Ruby 2.0+

View file

@ -0,0 +1,23 @@
# This solution uses three generators.
def powers(m)
return enum_for(__method__, m) unless block_given?
0.step{|n| yield n**m}
end
def squares_without_cubes
return enum_for(__method__) unless block_given?
cubes = powers(3) #no block, so this is the first generator
c = cubes.next
squares = powers(2) # second generator
loop do
s = squares.next
c = cubes.next while c < s
yield s unless c == s
end
end
answer = squares_without_cubes # third generator
20.times { answer.next }
p 10.times.map { answer.next }

View file

@ -0,0 +1,14 @@
def filtered(s1, s2)
return enum_for(__method__, s1, s2) unless block_given?
v, f = s1.next, s2.next
loop do
v > f and f = s2.next and next
v < f and yield v
v = s1.next
end
end
squares, cubes = powers(2), powers(3)
f = filtered(squares, cubes)
p f.take(30).last(10)
# p f.lazy.drop(20).first(10) # Ruby 2.0+