Class: Prime::Generator23

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

Overview

Generates all integers which are greater than 2 and are not divisible by either 2 or 3.

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

Instance Method Summary collapse

Methods inherited from PseudoPrimeGenerator

#each, #size, #upper_bound, #upper_bound=, #with_index, #with_object

Methods included from Enumerable

#all_with_pattern?, #any_with_pattern?, #chain, #chunk, #chunk_while, #count, #cycle, #drop, #drop_while, #each_entry, #each_with_index_with_optional_args_and_block, #each_with_object, #entries_with_optional_arguments, #filter_map, #find_index, #first, #flat_map, #grep_v, #group_by, #inject_with_symbol, #lazy, #max_by, #min_by, #minmax, #minmax_by, #none?, #none_with_pattern?, #one?, #one_with_pattern?, #reverse_each, #slice_after, #slice_before, #slice_when, #sum, #take, #take_while, #tally, #to_a_with_optional_arguments, #to_h, #to_h_with_block, #uniq

Constructor Details

#initializeGenerator23

Returns a new instance of Generator23.


348
349
350
351
352
# File 'lib/backports/1.9.1/stdlib/prime.rb', line 348

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

Instance Method Details

#rewindObject


368
369
370
# File 'lib/backports/1.9.1/stdlib/prime.rb', line 368

def rewind
  initialize
end

#succObject Also known as: next


354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/backports/1.9.1/stdlib/prime.rb', line 354

def succ
  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
  @prime
end