Class: Prime

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/rubysl/mathn/mathn.rb

Instance Method Summary collapse

Constructor Details

#initializePrime

Returns a new instance of Prime.



77
78
79
80
81
# File 'lib/rubysl/mathn/mathn.rb', line 77

def initialize
  @seed = 1
  @primes = []
  @counts = []
end

Instance Method Details

#eachObject



107
108
109
110
111
# File 'lib/rubysl/mathn/mathn.rb', line 107

def each
  loop do
    yield succ
  end
end

#succObject Also known as: next



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rubysl/mathn/mathn.rb', line 83

def succ
  i = -1
  size = @primes.size
  while i < size
    if i == -1
      @seed += 1
      i += 1
    else
      while @seed > @counts[i]
        @counts[i] += @primes[i]
      end
      if @seed != @counts[i]
        i += 1
      else
        i = -1
      end
    end
  end
  @primes.push @seed
  @counts.push @seed + @seed
  return @seed
end