class PrimeArray

Public Class Methods

get_primes(num) click to toggle source
# File lib/multiply_prime_numbers_table/prime_array.rb, line 5
def self.get_primes(num)
  time = Benchmark.measure {
    @primes_array = []
    counter = 2

    until @primes_array.size == num
      @primes_array << counter if counter.is_prime?
      # Avoid to check even numbers because only even prime number is 2
      (counter+1).even? ? counter += 1 : counter +=1
    end
  }

  # I add this report just to show illustrate how it gets exponentialy complex as it increases num
  puts "Time to takes to calculate first "+num.to_s+" prime numbers: "
  n = time.real
  Benchmark.bm(7) do |x|
    x.report("for:")   { for i in 1..n; a = "1"; end }
    x.report("times:") { (n.to_i).times do   ; a = "1"; end }
    x.report("upto:")  { 1.upto(n) do ; a = "1"; end }
  end
  puts "\n"

  @primes_array
end