Class: PrimeMultiplication
- Inherits:
-
Object
- Object
- PrimeMultiplication
- Defined in:
- lib/PrimeMultiplication.rb
Overview
Time complexity for this program is O(n^2)
Class Method Summary collapse
-
.calculate_multiplication_table ⇒ Object
calculate the Multiplication matrix for the prime numbers Time complexity for this method is O(n^2).
-
.get_primes ⇒ Object
Get first n prime numbers Time complexity for this method is O(n√n).
-
.get_user_input ⇒ Object
Get the number of primes user wants.
-
.get_user_input_for_test ⇒ Object
Same as get_user_input but for TDD to check if the program takes any other input other than a positive number.
-
.is_prime?(number) ⇒ Boolean
Check if a number is prime Time complexity of this method is O(√n).
-
.ordinal(number) ⇒ Object
helper method to give ordinal of a number eg.
-
.print_prime_multiples ⇒ Object
print the Multiplication table on to the console Time complexity for this function is O(n^2).
-
.run ⇒ Object
Get the program up and running by calling the functions in order.
Class Method Details
.calculate_multiplication_table ⇒ Object
calculate the Multiplication matrix for the prime numbers Time complexity for this method is O(n^2)
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/PrimeMultiplication.rb', line 78 def self.calculate_multiplication_table @@prime_array.each_with_index do |element1, index1| @@output_array[index1] = [] @@prime_array.each_with_index do |element2, index2| if(index1 == 0 && index2 == 0) @@output_array[index1][index2] = element1 elsif (index1 == 0) @@output_array[index1][index2] = element2 elsif (index2 == 0) @@output_array[index1][index2] = element1 else @@output_array[index1][index2] = element2*element1 end end end return @@output_array end |
.get_primes ⇒ Object
Get first n prime numbers Time complexity for this method is O(n√n)
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/PrimeMultiplication.rb', line 62 def self.get_primes @@prime_array = [""] @@output_array = [] n = 2 loop do if is_prime?(n) @@prime_array << n end n = n + 1 break if @@prime_array.length == @@no_of_primes+1 end return @@prime_array end |
.get_user_input ⇒ Object
Get the number of primes user wants
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/PrimeMultiplication.rb', line 19 def self.get_user_input input_right = false @@no_of_primes = 0 loop do print "Enter the number of primes you want: " @@no_of_primes = gets.chomp.to_i input_right = (@@no_of_primes.to_i > 0) ? true : false if @@no_of_primes <= 0 print "\n Please enter a number( > 0 ) \n" @@no_of_primes = 0 end break if input_right end @@no_of_primes = @@no_of_primes end |
.get_user_input_for_test ⇒ Object
Same as get_user_input but for TDD to check if the program takes any other input other than a positive number
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/PrimeMultiplication.rb', line 36 def self.get_user_input_for_test @@no_of_primes = 0 print "\n \t Enter value for the number of primes you want: " @@no_of_primes = gets.chomp.to_i if @@no_of_primes <= 0 print "Please enter a number( > 0 ): " @@no_of_primes = 0 return end @@no_of_primes = @@no_of_primes end |
.is_prime?(number) ⇒ Boolean
Check if a number is prime Time complexity of this method is O(√n)
50 51 52 53 54 55 56 57 58 |
# File 'lib/PrimeMultiplication.rb', line 50 def self.is_prime?(number) return false if number == 1 (2..(Math.sqrt(number).to_i)).each do |i| if number % i == 0 return false end end true end |
.ordinal(number) ⇒ Object
helper method to give ordinal of a number eg. 1 should return 1st, 2 should return 2nd etc.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/PrimeMultiplication.rb', line 136 def self.ordinal(number) ending = case number % 100 when 11, 12, 13 then 'th' else case number % 10 when 1 then 'st' when 2 then 'nd' when 3 then 'rd' else 'th' end end return number.to_s + ending end |
.print_prime_multiples ⇒ Object
print the Multiplication table on to the console Time complexity for this function is O(n^2)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/PrimeMultiplication.rb', line 98 def self.print_prime_multiples tabs = "\t" * (@@no_of_primes/2 - 3) rescue "\t" print "\n #{tabs} Multiplication Table for first #{@@no_of_primes} Prime numbers \n\n" @@prime_array.length.times do |i| print " _ _ _" end print "\n" @@prime_array.length.times do |i| if(i == 0) print "| \t" else print "| #{ordinal(i)} \t" end end print "|\n" @@prime_array.length.times do |i| print " _ _ _" end print "\n" @@output_array.each do |i| print "\n" i.each do |j| print "| #{j} \t" end print "|\n" i.each do |j| print " _ _ _" end print "\n" end return end |
.run ⇒ Object
Get the program up and running by calling the functions in order
11 12 13 14 15 16 |
# File 'lib/PrimeMultiplication.rb', line 11 def self.run get_user_input get_primes calculate_multiplication_table print_prime_multiples end |