Class: Prime::Generator23

Inherits:
PseudoPrimeGenerator show all
Defined in:
lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb

Overview

Generates all integer which are greater than 2 and are not divided by 2 nor 3.

This is a pseudo-prime generator, suitable on checking primality of a integer by brute force method.

Instance Method Summary collapse

Methods inherited from PseudoPrimeGenerator

#each, #upper_bound, #upper_bound=, #with_object

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

#initializeGenerator23

Returns a new instance of Generator23.



330
331
332
333
334
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 330

def initialize
  @prime = 1
  @step = nil
  super
end

Instance Method Details

#rewindObject



352
353
354
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 352

def rewind
  initialize
end

#succObject Also known as: next



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/vendor/backports-3.3.5/lib/backports/1.9.1/stdlib/prime.rb', line 336

def succ
  loop do
	if (@step)
	  @prime += @step
	  @step = 6 - @step
	else
	  case @prime
	  when 1; @prime = 2
	  when 2; @prime = 3
	  when 3; @prime = 5; @step = 2
	  end
	end
	return @prime
  end
end