Class Prime::TrialDivision
In: lib/backports/1.9.1/stdlib/prime.rb
Parent: Object

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

Methods

[]   cache   primes   primes_so_far  

Included Modules

Singleton

Public Instance methods

Returns the +index+th prime number.

index is a 0-based index.

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 386
386:     def [](index)
387:       while index >= @primes.length
388:         # Only check for prime factors up to the square root of the potential primes,
389:         #   but without the performance hit of an actual square root calculation.
390:         if @next_to_check + 4 > @ulticheck_next_squared
391:           @ulticheck_index += 1
392:           @ulticheck_next_squared = @primes.at(@ulticheck_index + 1) ** 2
393:         end
394:         # Only check numbers congruent to one and five, modulo six. All others
395: 
396:         #   are divisible by two or three.  This also allows us to skip checking against
397:         #   two and three.
398:         @primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
399:         @next_to_check += 4
400:         @primes.push @next_to_check if @primes[2..@ulticheck_index].find {|prime| @next_to_check % prime == 0 }.nil?
401:         @next_to_check += 2
402:       end
403:       return @primes[index]
404:     end

Returns the cached prime numbers.

[Source]

     # File lib/backports/1.9.1/stdlib/prime.rb, line 377
377:     def cache
378:       return @primes
379:     end
primes()

Alias for cache

primes_so_far()

Alias for cache

[Validate]