primegrid 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 587ed20c6253dd126cd445f827947b4fe3cd9ee9
4
+ data.tar.gz: ea94f1b84a2a7532523bacfa1b169d80f079b8bd
5
+ SHA512:
6
+ metadata.gz: 322447596aa2590174a0b1f2cfac4b270882566b0b5eb9f38b95c8c81e9fb3787ae28d1329d04e874f176de8538180f2d92f535ed01f5a5b0aed655704f33f01
7
+ data.tar.gz: 46c2519b8ef80f7e40fa579d1df5e932717ddc0a6b0aafd49e5e07981a9a7777bd52614c80211ad5aa7cb8d8faf8b58a0b90e1194aff66516c20e04bbe013935
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+ require 'primegrid'
5
+
6
+ options = { :n => 10, :headers => true }
7
+
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: primegrid [options]"
10
+
11
+ opts.on("-n", "--num-primes [NUM]", Integer, "Number of primes to find") do |n|
12
+ options[:n] = n
13
+ end
14
+
15
+ opts.on("-h", "--no-headers", "Don't print row and column headers") do |h|
16
+ options[:headers] = false
17
+ end
18
+
19
+ end.parse!
20
+
21
+ primes = Primegrid::Prime.find(options[:n])
22
+ prime_prod = primes.product(primes).map { |x, y| x * y }
23
+ print_opts = if options[:headers] then { :col_header => primes, :row_header => primes } else {} end
24
+
25
+ Primegrid::MatrixPrinter.print(prime_prod, primes.length, print_opts)
@@ -0,0 +1,3 @@
1
+ require 'primegrid/matrix_printer'
2
+ require 'primegrid/prime'
3
+ require 'primegrid/version'
@@ -0,0 +1,83 @@
1
+ module Primegrid
2
+ class MatrixPrinter
3
+ # Prints out an un-rolled matrix to $stdout
4
+ #
5
+ # Example:
6
+ # >> MatrixPrinter.print([1, 0, 0, 0, 1, 0, 0, 0, 1], 3)
7
+ # => 1 0 0
8
+ # => 0 1 0
9
+ # => 0 0 1
10
+ #
11
+ # Arguments:
12
+ # array: the unrolled matrix
13
+ # width: the number of columns in the matrix
14
+ # opts : a hash containing optional arrays for row and column headers
15
+ # e.g. { :col_header => [0, 1, 2], :row_header => [0, 1, 2] }
16
+
17
+ @@gutter_width = 2
18
+ @@h_border_char = "-"
19
+ @@v_border_char = "|"
20
+
21
+ def self.print(array, width, opts={})
22
+ matrix = self.build_matrix(array, width, opts)
23
+ if !matrix.empty?
24
+ $stdout.puts matrix
25
+ end
26
+ end
27
+
28
+ def self.build_matrix(array, width, opts={})
29
+ if array.length == 0 then return "" end
30
+
31
+ padding = self.maxlen_in array
32
+ gutter = " " * @@gutter_width
33
+ has_header = opts.has_key? :col_header
34
+ has_row_header = opts.has_key? :row_header
35
+ buf = []
36
+
37
+ # Add an optional header row
38
+ if has_header
39
+ header_sep = "-"
40
+ padding = [padding, self.maxlen_in(opts[:col_header])].max
41
+ header_str = opts[:col_header].map{|h| h.to_s.rjust(padding)}.join(gutter)
42
+ buf.push "#{header_str}"
43
+ buf.push "#{@@h_border_char * header_str.length}"
44
+ end
45
+
46
+ # Build the body of the matrix
47
+ line = ""
48
+ array.each_with_index do |p, i|
49
+ line = "#{line}#{p.to_s.rjust(padding)}#{gutter}"
50
+ if (i+1) % width == 0
51
+ buf.push "#{line.rstrip}"
52
+ line = ""
53
+ end
54
+ end
55
+
56
+ # Add row headers
57
+ if has_row_header
58
+ padding = self.maxlen_in(opts[:row_header])
59
+ padded_headers = opts[:row_header].map{|r| r.to_s.rjust(padding)}
60
+ offset = 0
61
+ if has_header
62
+ offset = 2
63
+ buf[0] = "#{' ' * (padding+1)}#{buf[0]}"
64
+ buf[1] = "#{' ' * (padding+1)}#{buf[1]}"
65
+ end
66
+
67
+ padded_headers.each_with_index do |r, i|
68
+ if i + offset < buf.length
69
+ buf[i + offset] = "#{r}#{@@v_border_char}#{buf[i + offset]}"
70
+ end
71
+ end
72
+ end
73
+
74
+ return buf.join("\n")
75
+ end
76
+
77
+ def self.maxlen_in(array)
78
+ as_lengths = array.map {|x| x.to_s.length}
79
+ return as_lengths.max
80
+ end
81
+
82
+ end
83
+ end
@@ -0,0 +1,57 @@
1
+ module Primegrid
2
+ class Prime
3
+
4
+ # Finds the first n primes
5
+ #
6
+ # Example:
7
+ # >> Prime.find(10)
8
+ # => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
9
+ #
10
+ # Arguments:
11
+ # n: an integer > 0
12
+ def self.find(n)
13
+ if n <= 0
14
+ return []
15
+ elsif n <= 10
16
+ return [2, 3, 5, 7, 11, 13, 17, 19, 23, 29].slice(0, n)
17
+ end
18
+
19
+ # (Over)estimate the nth prime: Pn/n < ln n + ln ln n for n >= 6
20
+ # See: http://en.wikipedia.org/wiki/Prime_number_theorem#Approximations_for_the_nth_prime_number
21
+ p = n * (Math.log(n) + Math.log(Math.log(n)))
22
+ return self.sieve(p).slice(0, n)
23
+ end
24
+
25
+ # Finds all the primes less than or equal to n
26
+ #
27
+ # An implementation of the Sieve of Eratosthenes
28
+ # See: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
29
+ #
30
+ # Example:
31
+ # >> Prime.sieve(30)
32
+ # => [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
33
+ #
34
+ # Arguments:
35
+ # n: an integer > 1
36
+ def self.sieve(n)
37
+ if n < 2 then return [] end
38
+ n = n + 1
39
+ a = Array.new(n){|i| if i >= 2 then true else false end }
40
+ for i in 2..Math.sqrt(n)
41
+ if a[i]
42
+ i_sq = i * i
43
+ j = i_sq
44
+ count = 0
45
+ until j >= n
46
+ a[j] = false
47
+ j = i_sq + (count * i)
48
+ count = count + 1
49
+ end
50
+ end
51
+ end
52
+ return a.each_with_index.map{ |x,i| if x then i end }.compact
53
+ end
54
+
55
+
56
+ end
57
+ end
@@ -0,0 +1,3 @@
1
+ module Primegrid
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ require 'test/unit'
2
+ require 'primegrid'
3
+
4
+ class TestMatrixPrinter < Test::Unit::TestCase
5
+
6
+ def test_should_find_maxlen
7
+ assert_equal(4, Primegrid::MatrixPrinter.maxlen_in([1000, 100, 10]))
8
+ assert_equal(4, Primegrid::MatrixPrinter.maxlen_in(["1000", "100", "10"]))
9
+ end
10
+
11
+ def test_should_print_correctly
12
+ output = Primegrid::MatrixPrinter.build_matrix([1, 0, 0, 0, 1, 0, 0, 0, 1], 3)
13
+ assert_equal("1 0 0\n0 1 0\n0 0 1", output)
14
+ end
15
+
16
+ def test_should_have_header_row
17
+ output = Primegrid::MatrixPrinter.build_matrix([10, 0, 0, 0, 10, 0, 0, 0, 10], 3, { :col_header => [0, 1, 2] })
18
+ assert_equal(" 0 1 2\n----------\n10 0 0\n 0 10 0\n 0 0 10", output)
19
+ end
20
+
21
+ def test_should_have_col_and_row_headers
22
+ output = Primegrid::MatrixPrinter.build_matrix([10, 0, 0, 0, 10, 0, 0, 0, 10], 3, { :col_header => [10, 1000, 10000], :row_header => [100, 1000, 10000] })
23
+ assert_equal(" 10 1000 10000\n -------------------\n 100| 10 0 0\n 1000| 0 10 0\n10000| 0 0 10", output)
24
+ end
25
+
26
+ end
@@ -0,0 +1,24 @@
1
+ require 'test/unit'
2
+ require 'primegrid'
3
+
4
+ class TestPrime < Test::Unit::TestCase
5
+
6
+ def test_should_find_primes
7
+ assert_equal([2, 3, 5, 7, 11, 13, 17, 19, 23, 29], Primegrid::Prime.find(10))
8
+ assert_equal([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71], Primegrid::Prime.find(20))
9
+ assert_equal([2], Primegrid::Prime.find(1))
10
+ assert_equal([2, 3], Primegrid::Prime.find(2))
11
+ assert_equal([], Primegrid::Prime.find(0))
12
+ assert_equal([], Primegrid::Prime.find(-1))
13
+ end
14
+
15
+ def test_should_sieve_primes
16
+ assert_equal([2, 3, 5, 7, 11, 13, 17, 19, 23, 29], Primegrid::Prime.sieve(30))
17
+ assert_equal([2], Primegrid::Prime.sieve(2))
18
+ assert_equal([2, 3], Primegrid::Prime.sieve(4))
19
+ assert_equal([], Primegrid::Prime.sieve(1))
20
+ assert_equal([], Primegrid::Prime.sieve(0))
21
+ assert_equal([], Primegrid::Prime.sieve(-1))
22
+ end
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: primegrid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - George Agnelli
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2013-11-04 00:00:00 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ prerelease: false
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - &id003
20
+ - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ type: :development
24
+ version_requirements: *id001
25
+ - !ruby/object:Gem::Dependency
26
+ name: rake
27
+ prerelease: false
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.2
33
+ type: :development
34
+ version_requirements: *id002
35
+ description:
36
+ email:
37
+ - george.agnelli@gmail.com
38
+ executables:
39
+ - primegrid
40
+ extensions: []
41
+
42
+ extra_rdoc_files: []
43
+
44
+ files:
45
+ - lib/primegrid.rb
46
+ - lib/primegrid/version.rb
47
+ - lib/primegrid/matrix_printer.rb
48
+ - lib/primegrid/prime.rb
49
+ - test/test_matrix_printer.rb
50
+ - test/test_prime.rb
51
+ - bin/primegrid
52
+ homepage: ""
53
+ licenses: []
54
+
55
+ metadata: {}
56
+
57
+ post_install_message:
58
+ rdoc_options: []
59
+
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "1.9"
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - *id003
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 2.1.10
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: Display an n x n matrix of prime products
77
+ test_files:
78
+ - test/test_matrix_printer.rb
79
+ - test/test_prime.rb