Skip to content

Instantly share code, notes, and snippets.

@wrq
Created January 15, 2016 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wrq/33f52ac5d087cede6af5 to your computer and use it in GitHub Desktop.
Save wrq/33f52ac5d087cede6af5 to your computer and use it in GitHub Desktop.
require 'prime'
PRIMES = (1..Float::INFINITY).lazy.select(&:prime?)
#while primes.length > 0 do puts primes.pop; sleep 1 end
def make_prime_sayer(starting_time)
dt = starting_time
c = 0
return lambda do ||
begin
c += 1 if (Time.new.to_i - dt) >= 5
p PRIMES.next if (Time.new.to_i - dt) >= 5
dt = Time.new.to_i if (Time.new.to_i - dt) >= 5
end
end
end
ps = make_prime_sayer(Time.new.to_i)
while true do ps.call end
# whatever, jhass
# the point of this was to print a prime number but to take five seconds
# the idea was that you would compute it very slowly
# you cant use sleep
# my thought was to attach a block to time so that it ran each second
# and then just print a prime on every second divisible by five
# i feel like a pussy for not having ever completed this
# and i wonder...
# what if I just had a busy loop with a counter that counted
# how long it had been since it last yielded to some &block or something like that?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment