Maintenance of Ruby 2.0.0 will end on February 24, 2016. Read more

In Files

  • prime.rb



Included Modules


Internal use. An implementation of eratosthenes’s sieve

Public Instance Methods

next_to(n) click to toggle source

returns the least odd prime number which is greater than n.

               # File prime.rb, line 441
def next_to(n)
  n = (n-1).div(2)*2+3 # the next odd number to given n
  table_index, integer_index, bit_index = indices(n)
  loop do
    extend_table until @tables.length > table_index
    for j in integer_index...ENTRIES_PER_TABLE
      if !@tables[table_index][j].zero?
        for k in bit_index...BITS_PER_ENTRY
          return NUMS_PER_TABLE*table_index + NUMS_PER_ENTRY*j + 2*k+1 if !@tables[table_index][j][k].zero?
      bit_index = 0
    table_index += 1; integer_index = 0

Commenting is here to help enhance the documentation. For example, code samples, or clarification of the documentation.

If you have questions about Ruby or the documentation, please post to one of the Ruby mailing lists. You will get better, faster, help that way.

If you wish to post a correction of the docs, please do so, but also file bug report so that it can be corrected for the next release. Thank you.

If you want to help improve the Ruby documentation, please visit