Spec-Zone.ru › Ruby 2.5

класс Prime::TrialDivision

Родитель:
Объект
Включенные модули:
Singleton

Внутреннее использование. Реализация таблицы простых чисел методом пробного деления.

Методы публичного экземпляра

[](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

Возвращает +index+ -ое простое число.

index — это индекс, отсчитываемый с 0.

cache() Показать исходный код
# File lib/prime.rb, line 391
def cache
  @primes
end

Возвращает кэшированные простые числа.

Также алиасы: primes, primes_so_far
primes()
Псевдоним для: cache
primes_so_far()
Псевдоним для: cache

Ruby Core © 1993–2017 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API