Class: Prime::Generator23

Inherits:
PseudoPrimeGenerator show all
Defined in:
lib/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, #upper_bound, #upper_bound=, #with_object

Constructor Details

#initializeGenerator23

Returns a new instance of Generator23


347
348
349
350
351
# File 'lib/prime.rb', line 347

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

Instance Method Details

#rewindObject


369
370
371
# File 'lib/prime.rb', line 369

def rewind
  initialize
end

#succObject Also known as: next


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

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