Ruby 2.4参考手册
Prime
Prime::TrialDivision
Parent:ObjectIncluded modules:Singleton
Internal use. An implementation of prime table by trial division method.
Public Instance Methods
Show source
Returns the +index+th prime number.
index
is a 0-based index.
# File lib/prime.rb, line 400
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
@primes[index]
end
cache() Show source
Returns the cached prime numbers.
# File lib/prime.rb, line 391
def cache
@primes
end
Also aliased as: primes, primes_so_far
primes()
Alias for: cache
primes_so_far()
Alias for: cache
Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.
Prime相关

Ruby 是一种面向对象、命令式、函数式、动态的通用编程语言,是世界上最优美而巧妙的语言。
主页 | https://www.ruby-lang.org/ |
源码 | https://github.com/ruby/ruby |
版本 | 2.4 |
发布版本 | 2.4.1 |