Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,21 +1,22 @@
# This solution cheats and uses only one generator!
def powers(m)
return enum_for(:powers, m) unless block_given?
return enum_for(__method__, 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?
return enum_for(__method__) unless block_given?
cubes = powers(3)
c = cubes.next
powers(2) do |s|
(c = cubes.next) until c >= 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

@ -1,25 +1,25 @@
# This solution uses three generators.
def powers(m)
return enum_for(:powers, m) unless block_given?
return enum_for(__method__, 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?
return enum_for(__method__) unless block_given?
cubes = powers(3)
c = cubes.next
squares = powers(2)
loop do
s = squares.next
(c = cubes.next) until c >= s
c = cubes.next while c < s
yield s unless c == s
end
end
answer = squares_without_cubes
20.times { answer.next }
p (1..10).map { 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+