Class: Prime::TrialDivision

### Home Classes Methods

### In Files

- lib/backports/1.9.1/stdlib/prime.rb

### Parent

Object

### Methods

- #[]
- #cache
- #primes
- #primes_so_far

### Included Modules

- Singleton

### Class/Module Index

Quicksearch

- Prime
- Prime::EratosthenesGenerator
- Prime::EratosthenesSieve
- Prime::Generator23
- Prime::OldCompatibility
- Prime::PseudoPrimeGenerator
- Prime::TrialDivision
- Prime::TrialDivisionGenerator
- Backports
- Backports::MOST_EXTREME_OBJECT_EVER
- Backports::StdLib
- Backports::StdLib::LoadedFeatures
- Enumerator
- Enumerator::GeneratorBP
- Enumerator::Lazy
- Enumerator::Yielder
- Matrix
- Matrix::EigenvalueDecomposition
- Matrix::LUPDecomposition
- Random
- Random::Implementation
- Random::MT19937
- Enumerable
- Enumerable::Enumerator
- File
- File::Constants
- ARGF
- Array
- BasicObject
- Binding
- Complex
- Dir
- ENV
- Fixnum
- Float
- GC
- Hash
- IO
- Integer
- Kernel
- Math
- Method
- Module
- NilClass
- Numeric
- Object
- ObjectSpace
- OpenStruct
- Proc
- Process
- Range
- Regexp
- Set
- StopIteration
- String
- Struct
- Symbol
- UnboundMethod

No matching classes.

# Prime::TrialDivision

Internal use. An implementation of prime table by trial division method.

### Public Instance Methods

[](index) click to toggle source

Returns the +index+th prime number.

`index` is a 0-based index.

    # File lib/backports/1.9.1/stdlib/prime.rb, line 387
    def [](index)
      while index >= @primes.length
        # Only check for prime factors up to the square root of the potential primes,
        #   but without the performance hit of an actual square root calculation.
        if @next_to_check + 4 > @ulticheck_next_squared
          @ulticheck_index += 1
          @ulticheck_next_squared = @primes.at(@ulticheck_index + 1) ** 2
        end
        # Only check numbers congruent to one and five, modulo six. All others

        #   are divisible by two or three.  This also allows us to skip checking against
        #   two and three.
        @primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
        @next_to_check += 4
        @primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
        @next_to_check += 2
      end
      return @primes[index]
    end

cache() click to toggle source

Returns the cached prime numbers.

    # File lib/backports/1.9.1/stdlib/prime.rb, line 378
    def cache
      return @primes
    end

Also aliased as: primes, primes_so_far
primes() click to toggle source
Alias for: cache
primes_so_far() click to toggle source
Alias for: cache

[Validate]

Generated with the Darkfish Rdoc Generator 2.