Class: PrintPrimesTable::Primes

Inherits:
Object
  • Object
show all
Defined in:
lib/print_primes_table/primes.rb

Overview

To handle the primes numbers

Author:

Instance Method Summary (collapse)

Constructor Details

- (Primes) initialize(total = DEFAULT_TOTAL)

Returns a new instance of Primes

Parameters:

  • total (Integer) (defaults to: DEFAULT_TOTAL)

    the quantity of the primes collection



11
12
13
14
15
# File 'lib/print_primes_table/primes.rb', line 11

def initialize(total = DEFAULT_TOTAL)
	@total=total
	@collection = []
	fill_collection
end

Instance Method Details

- (Object) fill_collection

fill the collection with primes number end when the collection.count is equal total



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/print_primes_table/primes.rb', line 18

def fill_collection
	eval_number = 2
	begin
		has_divisors = false
		@collection.each do |prime|
			if (eval_number % prime) == 0
				has_divisors = true
				break
			end
		end
		# only the numbers has not divisors are primes
		@collection.push eval_number unless has_divisors
		eval_number += 1
	end until @collection.count == @total
end

- (Object) list_numbers

print the list of primes collection separed by tab



34
35
36
# File 'lib/print_primes_table/primes.rb', line 34

def list_numbers
	puts @collection.join("\t")
end

print the table of TOTAL x TOTAL collection each coordinate contain the product



40
41
42
43
44
45
46
47
48
# File 'lib/print_primes_table/primes.rb', line 40

def print_table
	headings =[' '] + @collection
	rows=[]
	@collection.each do |prime|
		rows.push [prime] + @collection.map { |a| prime * a}
	end
	table = Terminal::Table.new :headings => headings, :rows => rows
	puts table
end