class MultiplyPrimeNumbersTable

Public Class Methods

create_table(prime_num) click to toggle source
# File lib/multiply_prime_numbers_table.rb, line 39
def create_table(prime_num)
  # Define prime numbers for first row and column
  first_row = first_column = prime_num

  # Get longest num in array
  longest_num = longest_num_in_array(first_row)

  # Define size for cells in table
  cell_size = get_cell_size(longest_num)

  # Leave blank first cell position to match first row with second coulum
  (longest_num+1).times do
    print " "
  end

  # Print first row of table
  first_row.each { |row_num|
    # Each cell will leave blank a space before and after the number inside, as left and rigth padding
    print (" "+cell_size+" ") % row_num
  }

  # Print 2 empty rows
  print "\n\n"

  first_column.each { |column_num|
    # Print first colum of table
    # After first number of each row leave blank a space
    print (cell_size+" ") % column_num

    # Print inside cells
    first_row.each {|row_num|
      # Calculate the product of the primes for the corresponding row and column that intersecting in this cell.
      # Print the product number leaving blank a space before and after
      print (" "+cell_size+" ") % (row_num * column_num)
    }

    # Print 2 empty rows
    print "\n\n"
  }
end
get_cell_size(num) click to toggle source
# File lib/multiply_prime_numbers_table.rb, line 85
def get_cell_size(num)
  # With %-4d format you make sure to print every cell with same size even if a number have less digits
  "%-" + (num).to_s + "d"
end
longest_num_in_array(array) click to toggle source
# File lib/multiply_prime_numbers_table.rb, line 80
def longest_num_in_array(array)
  # Get length of the larger number in last cell of table
  length_larger_num = (array.last**2).to_s.size
end
start() click to toggle source
# File lib/multiply_prime_numbers_table.rb, line 5
def start
  entry = ""

  # The program will keep running until users enters an  X to Exit the program.
  until entry == "X"
    puts "\nMultiply prime numbers table"
    print "=============================\n\n"
    puts "Enter an integer N greater than zero to specify the table size."
    puts "Any other entry will display a table containing the first 10 primes by default."
    puts "If N is greater than 100000 first will calcualte the first N primes and then will confirm is user want to print the table."
    puts "keep in mind that display a good table figure it depends on your screen size.\n\n"
    integer = gets.strip.to_i
    # If user enters something different of an integer greater than zero , the size table will be 10x10 by default.
    size = integer != 0 ? integer  : 10
    if size > 100000
      puts "This may take a while ..."
      prime_num = PrimeArray.get_primes(size)
      puts "Warning! Print this size of table may cause trouble, but if you want to print it press Y"
      entry = gets.strip.upcase
      entry == "Y" ? create_table(prime_num) : false
    else
      # Get array of primes with the size defined
      prime_num = PrimeArray.get_primes(size)
      # Print the table
      create_table(prime_num)
    end

    puts "Press X to Exit or any other key to calculate another table"
    entry = gets.strip.upcase
  end

  puts "Exit"
end