123456789_123456789_123456789_123456789_123456789_

Class: Prime::Generator23

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
Instance Chain:
self, PseudoPrimeGenerator, Enumerable
Inherits: Prime::PseudoPrimeGenerator
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.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

PseudoPrimeGenerator - Inherited

#each

Iterates the given block for each prime number.

#next

alias of #succ.

#rewind

Rewinds the internal position for enumeration.

#succ

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

#with_index

see Enumerator#with_index.

#with_object

see Enumerator#with_object.

Constructor Details

.newGenerator23

[ GitHub ]

  
# File 'lib/prime.rb', line 346

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

Instance Method Details

#next

Alias for #succ.

[ GitHub ]

  
# File 'lib/prime.rb', line 365

alias next succ

#rewind

[ GitHub ]

  
# File 'lib/prime.rb', line 366

def rewind
  initialize
end

#succ Also known as: #next

[ GitHub ]

  
# File 'lib/prime.rb', line 352

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
  return @prime
end