lib/primetable.rb
# Loads precalculated primes from a data file. The file is formatted with 10 primes per line.
# Each line is enclosed in square brackets, and the primes are comma-delimited. Thus, it's
# trivial to read each line in as an array of ten primes. Please note that this file is 995K
# and contains 13413 lines. You don't want to just slurp this file. That would be rude. Instead
# we read lines one at a time and only keep what we need. Also note the highest prime in this
# file is currently 2000003, although conceivably we could use the :calc method and extend it.
Loads precalculated primes from a data file. The file is formatted with 10 primes per line.
Each line is enclosed in square brackets, and the primes are comma-delimited. Thus, it's
trivial to read each line in as an array of ten primes. Please note that this file is 995K
and contains 13413 lines. You don't want to just slurp this file. That would be rude. Instead
we read lines one at a time and only keep what we need. Also note the highest prime in this
file is currently 2000003, although conceivably we could use the :calc method and extend it.
Loads precalculated primes from a data file . The file is formatted with 10 primes per line . Each line is enclosed in square brackets and the primes are comma - delimited . Thus it ' s trivial to read each line in as an array of ten primes . Please note that this file is 995K and contains 13413 lines . You don ' t want to just slurp this file . That would be rude . Instead we read lines one at a time and only keep what we need . Also note the highest prime in this file is currently 2000003 although conceivably we could use the : calc method and extend it .
def load_primes(first, count)
    primes = []
    if first == 1
      first = 2
    end
    if first == 2
      wb_before = ""
    else
      wb_before = ","
    end
    if first >= 2000003
      wb_after = ""
    else
      wb_after = ","
    end
    grep_result = []
    while grep_result == []
      grep_result = `grep -n -e "#{wb_before}#{first}#{wb_after}" #{ROOT_DIR}/data/prime.dat`.split(":")
      if grep_result == []
        first = first + 1
      end
    end
    first_line = grep_result[0].to_i-1
    first_index = YAML::load(grep_result[1]).index(first)
    last_line = (((first_line)*10+first_index+count)/10).ceil
    if last_line > 13413
      throw "We only have the first 134130 primes in the data file right now. Please pass -c to use calculated primes."
    end
    IO.readlines("#{ROOT_DIR}/data/prime.dat").each_with_index{|line,ln|
      if (ln >= first_line and ln <= last_line)
         primes.concat(YAML::load(line))
      end
    }
    primes.slice!(first_index,count)
  end
def load_primes ( first , count ) primes = [ ] if first == 1 first = 2 end if first == 2 wb_before = "" else wb_before = "," end if first >= 2000003 wb_after = "" else wb_after = "," end grep_result = [ ] while grep_result == [ ] grep_result = ` grep -n -e " #{ wb_before } #{ first } #{ wb_after } "  #{ ROOT_DIR } /data/prime.dat ` . split ( ":" ) if grep_result == [ ] first = first + 1 end end first_line = grep_result [ 0 ] . to_i - 1 first_index = YAML :: load ( grep_result [ 1 ] ) . index ( first ) last_line = ( ( ( first_line ) * 10 + first_index + count ) / 10 ) . ceil if last_line > 13413 throw "We only have the first 134130 primes in the data file right now. Please pass -c to use calculated primes." end IO . readlines ( "#{ROOT_DIR}/data/prime.dat" ) . each_with_index { | line , ln | if ( ln >= first_line and ln <= last_line ) primes . concat ( YAML :: load ( line ) ) end } primes . slice! ( first_index , count ) end
Loads precalculated primes from a data file.
Loads precalculated primes from a data file .
# An array to keep our prime numbers in, right? # Previously, the 'first' argument was an index, now it's a best guess for the first prime, # so we have to find the index in order to find the line number # Again, we only have the first 134130 primes in here right now (on 13413 lines) # Here we read in the file one line at a time. Yes I looked for a faster way. Try :fast (-f). #  1) We don't want to slurp the whole file...what if the file was even larger? Not cool. #  2) IO.readlines gets the file one line at a time, so we need a line number, not an index. # When we find the lines we want, we concatenate them into a single array. We get extra. # The extra is trimmed off in this step. We know what we want. Just this, thanks!
first
<not_specific>
count
<not_specific>