Last active
February 6, 2018 17:53
-
-
Save kuwa72/6dee680fdb08091929a9ce492f08148d to your computer and use it in GitHub Desktop.
Naive implement n/1 enumerator.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Reciprocal | |
include Enumerable | |
def initialize(i) | |
@denon = i | |
end | |
def each | |
numer = 1 | |
nums = [] | |
loop do | |
q,r = numer.divmod(@denon) | |
if (q == 0 && (nums.all?(0) || nums.empty?)) || q != 0 | |
nums << q | |
yield q | |
end | |
if q == 0 | |
numer *= 10 | |
redo | |
end | |
break if r == 0 | |
numer = r | |
end | |
return nums | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Class of pure implementation of reciprocal numbers.
It's enumerable.
You can get never ending numbers if you want.
Benchmark
思ったより速い。