prime-multiplication 1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/PrimeMultiplication.rb +150 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 8cbfe899a7a56eaa27de0c65363e61fa282000ea
|
|
4
|
+
data.tar.gz: 523bab3021fbcd7b23ba204ad3299de6f9d12ce7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: aabb6f6664bbbb85bf754bf6dfc46d46de32f51a412384db4bdee76782ba71e26ff177d9579639a14617e63525cbc88ba1e968501b9b1605efe68975138082e1
|
|
7
|
+
data.tar.gz: f52ff4fc07a1a717875c86fcfa1aff8553559a9a25d8243b4fe3d9359184461875204597cf770d6b5256277d3efe6b435e3227ddd66c08c870146bd88968f387
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# Time complexity for this program is O(n^2)
|
|
2
|
+
class PrimeMultiplication
|
|
3
|
+
|
|
4
|
+
# attribute accessors for:
|
|
5
|
+
# @@prime_array stores the first n prime numbers in an array
|
|
6
|
+
# @@no_of_primes stores the value of n for finding the first n prime numbers
|
|
7
|
+
# @@output_array stores the resulting multiplication matrix in a 2-Dimensional Array
|
|
8
|
+
# attr_accessor :@@output_array
|
|
9
|
+
|
|
10
|
+
# Get the program up and running by calling the functions in order
|
|
11
|
+
def self.run
|
|
12
|
+
get_user_input
|
|
13
|
+
get_primes
|
|
14
|
+
calculate_multiplication_table
|
|
15
|
+
print_prime_multiples
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Get the number of primes user wants
|
|
19
|
+
def self.get_user_input
|
|
20
|
+
input_right = false
|
|
21
|
+
@@no_of_primes = 0
|
|
22
|
+
loop do
|
|
23
|
+
print "Enter the number of primes you want: "
|
|
24
|
+
@@no_of_primes = gets.chomp.to_i
|
|
25
|
+
input_right = (@@no_of_primes.to_i > 0) ? true : false
|
|
26
|
+
if @@no_of_primes <= 0
|
|
27
|
+
print "\n Please enter a number( > 0 ) \n"
|
|
28
|
+
@@no_of_primes = 0
|
|
29
|
+
end
|
|
30
|
+
break if input_right
|
|
31
|
+
end
|
|
32
|
+
@@no_of_primes = @@no_of_primes
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# Same as get_user_input but for TDD to check if the program takes any other input other than a positive number
|
|
36
|
+
def self.get_user_input_for_test
|
|
37
|
+
@@no_of_primes = 0
|
|
38
|
+
print "\n \t Enter value for the number of primes you want: "
|
|
39
|
+
@@no_of_primes = gets.chomp.to_i
|
|
40
|
+
if @@no_of_primes <= 0
|
|
41
|
+
print "Please enter a number( > 0 ): "
|
|
42
|
+
@@no_of_primes = 0
|
|
43
|
+
return
|
|
44
|
+
end
|
|
45
|
+
@@no_of_primes = @@no_of_primes
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Check if a number is prime
|
|
49
|
+
# Time complexity of this method is O(√n)
|
|
50
|
+
def self.is_prime?(number)
|
|
51
|
+
return false if number == 1
|
|
52
|
+
(2..(Math.sqrt(number).to_i)).each do |i|
|
|
53
|
+
if number % i == 0
|
|
54
|
+
return false
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
true
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Get first n prime numbers
|
|
61
|
+
# Time complexity for this method is O(n√n)
|
|
62
|
+
def self.get_primes
|
|
63
|
+
@@prime_array = [""]
|
|
64
|
+
@@output_array = []
|
|
65
|
+
n = 2
|
|
66
|
+
loop do
|
|
67
|
+
if is_prime?(n)
|
|
68
|
+
@@prime_array << n
|
|
69
|
+
end
|
|
70
|
+
n = n + 1
|
|
71
|
+
break if @@prime_array.length == @@no_of_primes+1
|
|
72
|
+
end
|
|
73
|
+
return @@prime_array
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# calculate the Multiplication matrix for the prime numbers
|
|
77
|
+
# Time complexity for this method is O(n^2)
|
|
78
|
+
def self.calculate_multiplication_table
|
|
79
|
+
@@prime_array.each_with_index do |element1, index1|
|
|
80
|
+
@@output_array[index1] = []
|
|
81
|
+
@@prime_array.each_with_index do |element2, index2|
|
|
82
|
+
if(index1 == 0 && index2 == 0)
|
|
83
|
+
@@output_array[index1][index2] = element1
|
|
84
|
+
elsif (index1 == 0)
|
|
85
|
+
@@output_array[index1][index2] = element2
|
|
86
|
+
elsif (index2 == 0)
|
|
87
|
+
@@output_array[index1][index2] = element1
|
|
88
|
+
else
|
|
89
|
+
@@output_array[index1][index2] = element2*element1
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
return @@output_array
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# print the Multiplication table on to the console
|
|
97
|
+
# Time complexity for this function is O(n^2)
|
|
98
|
+
def self.print_prime_multiples
|
|
99
|
+
tabs = "\t" * (@@no_of_primes/2 - 3) rescue "\t"
|
|
100
|
+
print "\n #{tabs} Multiplication Table for first #{@@no_of_primes} Prime numbers \n\n"
|
|
101
|
+
|
|
102
|
+
@@prime_array.length.times do |i|
|
|
103
|
+
print " _ _ _"
|
|
104
|
+
end
|
|
105
|
+
print "\n"
|
|
106
|
+
|
|
107
|
+
@@prime_array.length.times do |i|
|
|
108
|
+
if(i == 0)
|
|
109
|
+
print "| \t"
|
|
110
|
+
else
|
|
111
|
+
print "| #{ordinal(i)} \t"
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
print "|\n"
|
|
115
|
+
|
|
116
|
+
@@prime_array.length.times do |i|
|
|
117
|
+
print " _ _ _"
|
|
118
|
+
end
|
|
119
|
+
print "\n"
|
|
120
|
+
|
|
121
|
+
@@output_array.each do |i|
|
|
122
|
+
print "\n"
|
|
123
|
+
i.each do |j|
|
|
124
|
+
print "| #{j} \t"
|
|
125
|
+
end
|
|
126
|
+
print "|\n"
|
|
127
|
+
i.each do |j|
|
|
128
|
+
print " _ _ _"
|
|
129
|
+
end
|
|
130
|
+
print "\n"
|
|
131
|
+
end
|
|
132
|
+
return
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
# helper method to give ordinal of a number eg. 1 should return 1st, 2 should return 2nd etc.
|
|
136
|
+
def self.ordinal(number)
|
|
137
|
+
ending =
|
|
138
|
+
case number % 100
|
|
139
|
+
when 11, 12, 13 then 'th'
|
|
140
|
+
else
|
|
141
|
+
case number % 10
|
|
142
|
+
when 1 then 'st'
|
|
143
|
+
when 2 then 'nd'
|
|
144
|
+
when 3 then 'rd'
|
|
145
|
+
else 'th'
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
return number.to_s + ending
|
|
149
|
+
end
|
|
150
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: prime-multiplication
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: '1.0'
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Kaushik Srivatsan
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2017-01-16 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Program to print a table of prime multiples
|
|
14
|
+
email: ksrivatsan89@gmail.com
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- lib/PrimeMultiplication.rb
|
|
20
|
+
homepage: http://rubygems.org/gems/prime-multiplication
|
|
21
|
+
licenses:
|
|
22
|
+
- MIT
|
|
23
|
+
metadata: {}
|
|
24
|
+
post_install_message:
|
|
25
|
+
rdoc_options: []
|
|
26
|
+
require_paths:
|
|
27
|
+
- lib
|
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
34
|
+
requirements:
|
|
35
|
+
- - ">="
|
|
36
|
+
- !ruby/object:Gem::Version
|
|
37
|
+
version: '0'
|
|
38
|
+
requirements: []
|
|
39
|
+
rubyforge_project:
|
|
40
|
+
rubygems_version: 2.4.8
|
|
41
|
+
signing_key:
|
|
42
|
+
specification_version: 4
|
|
43
|
+
summary: Prime Multiplication!
|
|
44
|
+
test_files: []
|