ulam_spiral 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ulam_spiral.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 michal szyma
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # UlamSpiral
2
+
3
+ This gem can generate Ulam spiral and also shows result on console
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ulam_spiral'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ulam_spiral
18
+
19
+ ## Usage
20
+
21
+ require "ulam_spiral"
22
+
23
+ ulam_spiral = UlamSpiral.new(first_number: 3, last_number: 13)
24
+ ulam_spiral.generate_spiral
25
+ ulam_spiral.show
26
+ #=> 7 5
27
+ #=>
28
+ #=> 3 13
29
+ #=>
30
+ #=> 11
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ class UlamSpiral
2
+ VERSION = "0.0.2"
3
+ end
@@ -0,0 +1,71 @@
1
+ # encoding: utf-8
2
+ require "ulam_spiral/version"
3
+ require "prime"
4
+
5
+ class UlamSpiral
6
+
7
+ attr_reader :spiral
8
+
9
+ def initialize(params = {})
10
+ @first_number = params[:first_number] || 1
11
+ @last_number = params[:last_number] || 100
12
+ @spiral = (0...square_length).to_a.map{ [nil] * (square_length) }
13
+ @row = (square_length / 2.0).floor
14
+ @column = ((square_length - 1) / 2.0).floor
15
+ @spiral[@row][@column] = @first_number
16
+ end
17
+
18
+ # generate ulam spiral
19
+ def generate_spiral
20
+ direct = 2
21
+ ((@first_number + 1)..@last_number).each do |number|
22
+ @row, @column = get_position(direct)
23
+ if Prime.prime?(number)
24
+ @spiral[@row][@column] = number if Prime.prime?(number)
25
+ else
26
+ @spiral[@row][@column] = ""
27
+ end
28
+
29
+ x, y = get_position(direct + 1)
30
+ direct = (direct + 1) % 4 if @spiral[x][y].nil?
31
+ end
32
+ string_to_nil
33
+ end
34
+
35
+ # Show on console spiral
36
+ def show
37
+ length = @last_number.to_s.length + 1
38
+ @spiral.each do |row|
39
+ puts "\n"
40
+ puts row.map { |element| element.nil? ? " " * length : element.to_s.rjust(length, " ") }.join(" ")
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def get_position(direct)
47
+ case direct
48
+ when 0
49
+ return [@row, @column - 1]
50
+ when 1
51
+ return [@row + 1, @column]
52
+ when 2
53
+ return [@row, @column + 1]
54
+ when 3
55
+ return [@row - 1, @column]
56
+ else
57
+ return get_position(0)
58
+ end
59
+ end
60
+
61
+ def string_to_nil
62
+ @spiral.map! do |line|
63
+ line.map{ |element| (element.eql?("") ? nil : element)}
64
+ end
65
+ end
66
+
67
+ def square_length
68
+ Math.sqrt(@last_number - @first_number + 1 ).ceil
69
+ end
70
+
71
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+
3
+ require 'ulam_spiral'
4
+
5
+ describe UlamSpiral do
6
+
7
+ it "should properly initialize class" do
8
+ expect { UlamSpiral.new }.to_not raise_error
9
+ end
10
+
11
+
12
+ it "should properly generate table of first 2 numbers and with first number 5" do
13
+ ulam = UlamSpiral.new(first_number: 5, last_number: 7)
14
+ ulam.generate_spiral
15
+ ulam.spiral.should eq(
16
+ [
17
+ [nil, 7],
18
+ [5, nil]
19
+ ]
20
+ )
21
+ end
22
+
23
+ it "should properly generate table of first 2 numbers" do
24
+ ulam = UlamSpiral.new(last_number: 2)
25
+ ulam.generate_spiral
26
+ ulam.spiral.should eq(
27
+ [
28
+ [nil, nil],
29
+ [1, 2]
30
+ ]
31
+ )
32
+ end
33
+
34
+ it "should properly generate table of first 10 numbers" do
35
+ ulam = UlamSpiral.new(last_number: 10)
36
+ ulam.generate_spiral
37
+ ulam.spiral.should eq(
38
+ [
39
+ [nil, nil, nil, nil],
40
+ [5, nil, 3, nil],
41
+ [nil, 1, 2, nil],
42
+ [7, nil, nil, nil]
43
+ ]
44
+ )
45
+ end
46
+
47
+ it "should properly generate table of first 5 numbers" do
48
+ ulam = UlamSpiral.new(last_number: 5)
49
+ ulam.generate_spiral
50
+ ulam.spiral.should eq(
51
+ [
52
+ [5, nil, 3],
53
+ [nil, 1, 2],
54
+ [nil, nil, nil]
55
+ ]
56
+ )
57
+ end
58
+
59
+ it "should properly generate table of first 19 numbers" do
60
+ ulam = UlamSpiral.new(last_number: 19)
61
+ ulam.generate_spiral
62
+ ulam.spiral.should eq(
63
+ [
64
+ [17, nil, nil, nil, 13],
65
+ [nil, 5, nil, 3, nil],
66
+ [19, nil, 1, 2, 11],
67
+ [nil, 7, nil, nil, nil],
68
+ [nil, nil, nil, nil, nil]
69
+ ]
70
+ )
71
+ end
72
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ulam_spiral/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ulam_spiral"
8
+ spec.version = UlamSpiral::VERSION
9
+ spec.authors = ["Michał szyma"]
10
+ spec.email = ["raglub.ruby@gmail.com"]
11
+ spec.description = %q{This gem can generate Ulam spiral and also shows result on console}
12
+ spec.summary = %q{Ulam spiral}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(spec)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_dependency "prime"
25
+
26
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ulam_spiral
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michał szyma
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: prime
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: This gem can generate Ulam spiral and also shows result on console
79
+ email:
80
+ - raglub.ruby@gmail.com
81
+ executables: []
82
+ extensions: []
83
+ extra_rdoc_files: []
84
+ files:
85
+ - .gitignore
86
+ - Gemfile
87
+ - LICENSE.txt
88
+ - README.md
89
+ - Rakefile
90
+ - lib/ulam_spiral.rb
91
+ - lib/ulam_spiral/version.rb
92
+ - spec/ulam_spiral_spec.rb
93
+ - ulam_spiral.gemspec
94
+ homepage: ''
95
+ licenses:
96
+ - MIT
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.24
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: Ulam spiral
119
+ test_files:
120
+ - spec/ulam_spiral_spec.rb