Class: Prime::PseudoPrimeGenerator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb

Overview

An abstract class for enumerating pseudo-prime numbers.

Concrete subclasses should override succ, next, rewind.

Instance Method Summary collapse

Methods included from Enumerable

#chunk, #count, #cycle, #drop, #drop_while, #each_entry, #each_with_index_with_optional_args_and_block, #each_with_object, #entries_with_optional_arguments, #find_index, #first, #flat_map, #group_by, #inject_with_symbol, #lazy, #max_by, #min_by, #minmax, #minmax_by, #none?, #one?, #reverse_each, #slice_before, #sum, #take, #take_while, #to_a_with_optional_arguments

Constructor Details

#initialize(ubound = nil) ⇒ PseudoPrimeGenerator

Returns a new instance of PseudoPrimeGenerator.



228
229
230
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 228

def initialize(ubound = nil)
  @ubound = ubound
end

Instance Method Details

#each(&block) ⇒ Object

Iterates the given block for each prime numbers.



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 260

def each(&block)
  return self.dup unless block
  if @ubound
	last_value = nil
	loop do
	  prime = succ
	  break last_value if prime > @ubound
	  last_value = block.call(prime)
	end
  else
	loop do
	  block.call(succ)
	end
  end
end

#nextObject

alias of succ.

Raises:

  • (NotImplementedError)


248
249
250
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 248

def next
  raise NotImplementedError, "need to define `next'"
end

#rewindObject

Rewinds the internal position for enumeration.

See Enumerator#rewind.

Raises:

  • (NotImplementedError)


255
256
257
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 255

def rewind
  raise NotImplementedError, "need to define `rewind'"
end

#succObject

returns the next pseudo-prime number, and move the internal position forward.

PseudoPrimeGenerator#succ raises NotImplementedError.

Raises:

  • (NotImplementedError)


243
244
245
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 243

def succ
  raise NotImplementedError, "need to define `succ'"
end

#upper_boundObject



235
236
237
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 235

def upper_bound
  @ubound
end

#upper_bound=(ubound) ⇒ Object



232
233
234
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 232

def upper_bound=(ubound)
  @ubound = ubound
end

#with_object(obj) ⇒ Object

see Enumerator#with_object.



280
281
282
283
284
285
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 280

def with_object(obj)
  return enum_for(:with_object) unless block_given?
  each do |prime|
	yield prime, obj
  end
end