primtetable 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +13 -0
- data/README.md +16 -0
- data/bin/primetable +24 -0
- data/lib/composite_sequence.rb +26 -0
- data/lib/multiplication_table.rb +13 -0
- data/lib/ordered_composite_sequences.rb +44 -0
- data/lib/primes.rb +28 -0
- data/lib/table_printer.rb +58 -0
- metadata +56 -0
data/LICENSE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Copyright 2014 William DePhillips
|
2
|
+
|
3
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
you may not use this file except in compliance with the License.
|
5
|
+
You may obtain a copy of the License at
|
6
|
+
|
7
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
|
9
|
+
Unless required by applicable law or agreed to in writing, software
|
10
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
See the License for the specific language governing permissions and
|
13
|
+
limitations under the License.
|
data/README.md
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
A small ruby app that prints a multiplication table of prime numbers.
|
2
|
+
|
3
|
+
It uses a simplistic incremental Sieve of Eratosthenes algorithm to find primes based on this academic paper: http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
|
4
|
+
|
5
|
+
|
6
|
+
Instructions
|
7
|
+
============
|
8
|
+
|
9
|
+
> gem install primetable
|
10
|
+
> primetable 10
|
11
|
+
|
12
|
+
This will print the first 10 prime numbers. You can try raising that number and seeing how far you can go.
|
13
|
+
|
14
|
+
|
15
|
+
Interesting Prime Number Resources
|
16
|
+
----------------------------------
|
data/bin/primetable
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
require_relative '../lib/primes'
|
6
|
+
require_relative '../lib/table_printer'
|
7
|
+
|
8
|
+
if ARGV && ARGV.size == 1 && ARGV[0].to_i > 0
|
9
|
+
n = ARGV[0].to_i
|
10
|
+
primes = nil
|
11
|
+
time = Benchmark.realtime { primes = Primes.first(n) }
|
12
|
+
|
13
|
+
puts "Generating table for the first #{n} primes."
|
14
|
+
puts "found primes in #{time} sec"
|
15
|
+
puts "\n"
|
16
|
+
|
17
|
+
TablePrinter.new(primes).print
|
18
|
+
|
19
|
+
puts "\n\n"
|
20
|
+
else
|
21
|
+
puts %{ usage: #{$0} N
|
22
|
+
N is the number of primes to make a table for }
|
23
|
+
exit -1
|
24
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class CompositeSequence
|
2
|
+
|
3
|
+
def self.of_base_prime(prime)
|
4
|
+
self.new(prime)
|
5
|
+
end
|
6
|
+
|
7
|
+
def initialize(prime)
|
8
|
+
@base = prime
|
9
|
+
@current = 2 * prime
|
10
|
+
end
|
11
|
+
|
12
|
+
def next
|
13
|
+
@current
|
14
|
+
end
|
15
|
+
|
16
|
+
def <=>(other)
|
17
|
+
self.next <=> other.next
|
18
|
+
end
|
19
|
+
|
20
|
+
def take_next!
|
21
|
+
self.next.tap do
|
22
|
+
@current += @base
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require_relative 'composite_sequence'
|
2
|
+
|
3
|
+
class OrderedCompositeSequences
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@sequences = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def empty?
|
10
|
+
@sequences.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
def take_next_composite!
|
14
|
+
next_sequence.take_next!.tap do |return_value|
|
15
|
+
|
16
|
+
@sequences.sort!
|
17
|
+
@sequences.each do |other_sequence|
|
18
|
+
upcoming_value = other_sequence.next
|
19
|
+
if upcoming_value == return_value
|
20
|
+
other_sequence.take_next!
|
21
|
+
end
|
22
|
+
break if upcoming_value > return_value
|
23
|
+
end
|
24
|
+
|
25
|
+
@sequences.sort!
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def next_composite
|
30
|
+
next_sequence.next
|
31
|
+
end
|
32
|
+
|
33
|
+
def add_sequence_for(prime)
|
34
|
+
(@sequences << CompositeSequence.of_base_prime(prime)).sort!
|
35
|
+
nil
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def next_sequence
|
41
|
+
@sequences[0]
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
data/lib/primes.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'ordered_composite_sequences'
|
2
|
+
|
3
|
+
class Primes
|
4
|
+
|
5
|
+
# based on incremental sieve of eratosthenes algorithm described by:
|
6
|
+
# http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
|
7
|
+
def self.first(n)
|
8
|
+
composites = OrderedCompositeSequences.new
|
9
|
+
primes = []
|
10
|
+
current = 2
|
11
|
+
|
12
|
+
while primes.size < n
|
13
|
+
if composites.empty? || composites.next_composite > current
|
14
|
+
primes << current
|
15
|
+
composites.add_sequence_for current
|
16
|
+
current += 1
|
17
|
+
elsif composites.next_composite == current
|
18
|
+
composites.take_next_composite!
|
19
|
+
current += 1
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
primes
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require_relative '../lib/multiplication_table'
|
2
|
+
|
3
|
+
class TablePrinter
|
4
|
+
|
5
|
+
def initialize(primes, io=STDOUT)
|
6
|
+
@primes = primes
|
7
|
+
@table = MultiplicationTable.new(@primes).generate
|
8
|
+
@io = io
|
9
|
+
end
|
10
|
+
|
11
|
+
def print
|
12
|
+
print_header_row
|
13
|
+
print_rows
|
14
|
+
end
|
15
|
+
|
16
|
+
def longest_digits
|
17
|
+
@longest_digits ||= @table.flatten.max.to_s.size
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def print_header_row
|
23
|
+
@io.write spacer
|
24
|
+
@io.write formatted_list(@primes)
|
25
|
+
@io.write "\n"
|
26
|
+
@io.write "=" * ((@primes.size + 1) * field_size)
|
27
|
+
@io.write "\n"
|
28
|
+
end
|
29
|
+
|
30
|
+
def print_rows
|
31
|
+
@table.each_with_index do |row, i|
|
32
|
+
@io.write(format % @primes[i] + separator)
|
33
|
+
@io.write formatted_list(row)
|
34
|
+
@io.write "\n"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def formatted_list(array)
|
39
|
+
array.map { |elem| format % elem }.join(separator)
|
40
|
+
end
|
41
|
+
|
42
|
+
def spacer
|
43
|
+
@spacer ||= " " * field_size
|
44
|
+
end
|
45
|
+
|
46
|
+
def separator
|
47
|
+
" | "
|
48
|
+
end
|
49
|
+
|
50
|
+
def format
|
51
|
+
@format ||= "%#{longest_digits}d"
|
52
|
+
end
|
53
|
+
|
54
|
+
def field_size
|
55
|
+
@field_size ||= longest_digits + separator.size
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: primtetable
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Bill DePhillips
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-09-25 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: command line tool that prints a table of the first N prime numbers, (e.g.
|
15
|
+
2,3,5 x 2,3,5)
|
16
|
+
email:
|
17
|
+
- bill.dephillips@gmail.com
|
18
|
+
executables:
|
19
|
+
- primetable
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- lib/primes.rb
|
24
|
+
- lib/ordered_composite_sequences.rb
|
25
|
+
- lib/table_printer.rb
|
26
|
+
- lib/multiplication_table.rb
|
27
|
+
- lib/composite_sequence.rb
|
28
|
+
- LICENSE
|
29
|
+
- README.md
|
30
|
+
- bin/primetable
|
31
|
+
homepage: https://github.com/rearadmiral/primetable
|
32
|
+
licenses:
|
33
|
+
- Apache
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.21
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: prints multiplication tables of first N prime numbers
|
56
|
+
test_files: []
|