Skip to content

Instantly share code, notes, and snippets.

@tdantas
Created November 14, 2012 11:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdantas/4071681 to your computer and use it in GitHub Desktop.
Save tdantas/4071681 to your computer and use it in GitHub Desktop.
class Fixnum
def prime?
(2..Math.sqrt(self).to_i).each do |number|
return false if (self % number == 0)
end
true
end
end
class PrimeMatrix
def initialize(size = 10)
@matrixSize = size
end
def axis
@axis = @axis || generateAxis
end
def generateAxis
number = 2
axis = []
until (axis.size == @matrixSize) do
axis.push(number) if number.prime?
number = number + 1
end
axis
end
def calculate
(0...@matrixSize).each do |i|
Printer.draw(MatrixLine.new(i, axis).calculate)
end
end
end
class MatrixLine
def initialize(line, primersLine)
@line = line
@primers = primersLine
end
def calculate
factor = @primers[@line]
@primers.map {|i| i * factor}
end
end
class Printer
def self.draw(line)
line.each do |elements|
print "#{elements} |"
end
print "\n"
end
end
PrimeMatrix.new(10).calculate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment