Sha256: 835f2670fae350acf62afa4e701abf49a530dc961b5a93bb94774e4683b93804

Contents?: true

Size: 1.12 KB

Versions: 2

Compression:

Stored size: 1.12 KB

Contents

class Primer
  def initialize n=10
    unless n.respond_to?(:integer?) && n.integer?
      raise ArgumentError.new("Argument must be an Integer")
    end

    @n = n
    @n_primes = get_primes
    @table = multiplication_table
  end

  def display_table
    pad = @table.last.last.to_s.length + 2

    dt = @table.map do |row|
      row.map do |entry|
        entry.to_s.rjust pad
      end.push("\n").join()
    end

    puts "\n"
    puts dt
  end

  private

  def multiplication_table
    table = []
    factors = [1, @n_primes].flatten

    factors.each do |row|
      slice = []
      factors.each do |col|
        slice.push(row * col)
      end
      table.push(slice)
    end

    # Formatting to remove first row and column entry
    table[0][0] = " "
    table
  end

  def get_primes
    primes = []
    counter = 0

    (1..Float::INFINITY).each do |prospect|
      if prospect.prime?
        counter += 1
        primes.push prospect
      end

      break if counter == @n
    end
    primes
  end
end

class Integer
  def prime?
    return false if self == 1
    (2..Math.sqrt(self)).none? {|prospect| self % prospect == 0}
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sb_prime_table-0.1.1 lib/sb_prime_table/primer.rb
sb_prime_table-0.1.0 lib/sb_prime_table/primer.rb