ulaminate 0.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/.gitignore +9 -0
- data/.travis.yml +12 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +26 -0
- data/LICENSE.txt +21 -0
- data/README.md +37 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/ulaminate +37 -0
- data/lib/prime_sieve.rb +50 -0
- data/lib/ulam_sequence.rb +56 -0
- data/lib/ulaminate.rb +8 -0
- data/lib/ulaminate/exceptions.rb +5 -0
- data/lib/ulaminate/version.rb +3 -0
- data/ulaminate.gemspec +28 -0
- metadata +131 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a687564ea5792117028a2dd14b914f67964ab993132f917209fbbb8d1597f875
|
4
|
+
data.tar.gz: a65bffe55a528dfd57eb726bef7f3aa9645d7a3a315bd87f2ab4402824701f37
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d41fd49edad4deee1f360e989f0afb27c4bd5196bf4a2de5abe0a32ef86dbaf7e7e99ee77afba7d968edcc0dea1ad6231168c19588fbd1520c655a9618bf939
|
7
|
+
data.tar.gz: ea9a6179415e753dd871a57687ac9d776245ceb731a260ecfedf2cfd575ced70c9e6e136a3c00a97ab0eb23c633394feb4afd57603bd93cc102daa3d436e32f5
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
ulaminate (0.1.0)
|
5
|
+
bitset (~> 1.1)
|
6
|
+
chunky_png (~> 1.3)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
bitset (1.1.0)
|
12
|
+
chunky_png (1.3.10)
|
13
|
+
minitest (5.11.3)
|
14
|
+
rake (10.5.0)
|
15
|
+
|
16
|
+
PLATFORMS
|
17
|
+
ruby
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
bundler (~> 1.16)
|
21
|
+
minitest (~> 5.0)
|
22
|
+
rake (~> 10.0)
|
23
|
+
ulaminate!
|
24
|
+
|
25
|
+
BUNDLED WITH
|
26
|
+
1.16.1
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Alex Cruice
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# Ulaminate [](https://travis-ci.org/alexcruice/ulaminate)
|
2
|
+
|
3
|
+
[Ulam spirals](https://en.wikipedia.org/wiki/Ulam_spiral) are neat. `ulaminate` can generate them.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
$ gem install ulaminate
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```
|
12
|
+
$ ulaminate --help
|
13
|
+
Usage: ulaminate [options]
|
14
|
+
-h, --help Prints this help
|
15
|
+
--size SIZE The size of the side of the spiral
|
16
|
+
```
|
17
|
+
|
18
|
+
Generate a 200-pixel square, black-on-white, Ulam spiral PNG in the current directory:
|
19
|
+
|
20
|
+
```
|
21
|
+
$ ulaminate --size 200
|
22
|
+
ulam_spiral_200.png
|
23
|
+
```
|
24
|
+
|
25
|
+
## Development
|
26
|
+
|
27
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
28
|
+
|
29
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
30
|
+
|
31
|
+
## Contributing
|
32
|
+
|
33
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/alexcruice/ulaminate.
|
34
|
+
|
35
|
+
## License
|
36
|
+
|
37
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "ulaminate"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/ulaminate
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "ulaminate"
|
4
|
+
require "optparse"
|
5
|
+
require "chunky_png"
|
6
|
+
|
7
|
+
Options = Struct.new(:size)
|
8
|
+
options = Options.new(7)
|
9
|
+
|
10
|
+
OptionParser.new do |parser|
|
11
|
+
parser.banner = "Usage: ulaminate [options]"
|
12
|
+
|
13
|
+
parser.on("-h", "--help", "Prints this help") do
|
14
|
+
puts parser
|
15
|
+
exit
|
16
|
+
end
|
17
|
+
|
18
|
+
parser.on("--size SIZE", Integer, "The size of the side of the spiral") do |size|
|
19
|
+
options.size = size
|
20
|
+
end
|
21
|
+
end.parse!
|
22
|
+
|
23
|
+
png = ChunkyPNG::Image.new(options.size, options.size, ChunkyPNG::Color::WHITE)
|
24
|
+
|
25
|
+
begin
|
26
|
+
sequence = Ulaminate.ulam_sequence(options.size)
|
27
|
+
|
28
|
+
loop do
|
29
|
+
x, y, _, is_prime = sequence.next
|
30
|
+
next unless is_prime
|
31
|
+
png[x, y] = ChunkyPNG::Color::BLACK
|
32
|
+
end
|
33
|
+
rescue StopIteration
|
34
|
+
# swallow sequence end
|
35
|
+
end
|
36
|
+
|
37
|
+
puts png.save("ulam_spiral_#{options.size}.png", :best_compression).path
|
data/lib/prime_sieve.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require "bitset"
|
2
|
+
require "ulaminate/exceptions"
|
3
|
+
|
4
|
+
class PrimeSieve
|
5
|
+
# Frozen array of prime numbers up to the pre-calculation limit.
|
6
|
+
#
|
7
|
+
# @return [Array<Integer>]
|
8
|
+
attr_reader :primes
|
9
|
+
|
10
|
+
# Create a new PrimeSieve.
|
11
|
+
#
|
12
|
+
# @param precalculation_limit [#to_i] calculate primality lookup, and
|
13
|
+
# generate prime numbers up to this value
|
14
|
+
# @raise [Ulaminate::TypeError] if precalculation_limit is not a positive
|
15
|
+
# integer
|
16
|
+
def initialize(precalculation_limit = 10**7)
|
17
|
+
@precalculation_limit = precalculation_limit.to_i
|
18
|
+
@precalculation_limit > 0 or
|
19
|
+
raise Ulaminate::TypeError, "#{@precalculation_limit} must be positive"
|
20
|
+
|
21
|
+
@is_prime = ~Bitset.new(@precalculation_limit + 1)
|
22
|
+
@is_prime.clear(0, 1)
|
23
|
+
@primes = []
|
24
|
+
|
25
|
+
2.upto(@precalculation_limit) do |i|
|
26
|
+
next unless @is_prime[i]
|
27
|
+
|
28
|
+
(i**2..@precalculation_limit).step(i) do |j|
|
29
|
+
@is_prime[j] = false
|
30
|
+
end
|
31
|
+
|
32
|
+
primes << i
|
33
|
+
end
|
34
|
+
|
35
|
+
@primes.freeze
|
36
|
+
end
|
37
|
+
|
38
|
+
# Determines whether the given number is prime.
|
39
|
+
#
|
40
|
+
# @param number [#to_i]
|
41
|
+
# @return [Boolean] whether number is prime
|
42
|
+
# @raise [Ulaminate::TypeError] if number is not a positive integer
|
43
|
+
def prime?(number)
|
44
|
+
n = number.to_i
|
45
|
+
n > 0 or raise Ulaminate::TypeError, "#{n} must be positive"
|
46
|
+
|
47
|
+
return @is_prime[n] if n <= @precalculation_limit
|
48
|
+
primes.all? { |prime| n % prime != 0 }
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "prime_sieve"
|
2
|
+
|
3
|
+
class UlamSequence
|
4
|
+
include Enumerable
|
5
|
+
|
6
|
+
attr_reader :max, :size
|
7
|
+
|
8
|
+
def initialize(size)
|
9
|
+
@size = size.to_i
|
10
|
+
@max = @size**2
|
11
|
+
@sieve = PrimeSieve.new(@max)
|
12
|
+
end
|
13
|
+
|
14
|
+
def each
|
15
|
+
return to_enum { size } unless block_given?
|
16
|
+
|
17
|
+
points = Enumerator.new do |yielder|
|
18
|
+
walk_cycle = [
|
19
|
+
lambda { |x, y| [x + 1, y] }, # east
|
20
|
+
lambda { |x, y| [x, y - 1] }, # north
|
21
|
+
lambda { |x, y| [x - 1, y] }, # west
|
22
|
+
lambda { |x, y| [x, y + 1] }, # south
|
23
|
+
].cycle
|
24
|
+
|
25
|
+
x, y = origin
|
26
|
+
i = 1
|
27
|
+
advance_point = walk_cycle.next
|
28
|
+
|
29
|
+
loop do
|
30
|
+
((i + 1) / 2).times do
|
31
|
+
yielder << [x, y]
|
32
|
+
x, y = advance_point.call(x, y)
|
33
|
+
end
|
34
|
+
|
35
|
+
advance_point = walk_cycle.next
|
36
|
+
i += 1
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
1.upto(max) do |n|
|
41
|
+
x, y = points.next
|
42
|
+
yield [x, y, n, sieve.prime?(n)]
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def origin
|
47
|
+
[
|
48
|
+
(size - 1) / 2,
|
49
|
+
size / 2,
|
50
|
+
]
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
attr_reader :sieve
|
56
|
+
end
|
data/lib/ulaminate.rb
ADDED
data/ulaminate.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "ulaminate/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ulaminate"
|
7
|
+
spec.version = Ulaminate::VERSION
|
8
|
+
spec.authors = ["Alex Cruice"]
|
9
|
+
spec.email = ["alex.cruice@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = "Generate an Ulam spiral sequence. PNG output available."
|
12
|
+
spec.homepage = "https://github.com/alexcruice/ulaminate"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
16
|
+
f.match(%r{^(test|spec|features)/})
|
17
|
+
end
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_runtime_dependency "bitset", "~> 1.1"
|
23
|
+
spec.add_runtime_dependency "chunky_png", "~> 1.3"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,131 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ulaminate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alex Cruice
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bitset
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: chunky_png
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- alex.cruice@gmail.com
|
86
|
+
executables:
|
87
|
+
- ulaminate
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- Gemfile.lock
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- exe/ulaminate
|
101
|
+
- lib/prime_sieve.rb
|
102
|
+
- lib/ulam_sequence.rb
|
103
|
+
- lib/ulaminate.rb
|
104
|
+
- lib/ulaminate/exceptions.rb
|
105
|
+
- lib/ulaminate/version.rb
|
106
|
+
- ulaminate.gemspec
|
107
|
+
homepage: https://github.com/alexcruice/ulaminate
|
108
|
+
licenses:
|
109
|
+
- MIT
|
110
|
+
metadata: {}
|
111
|
+
post_install_message:
|
112
|
+
rdoc_options: []
|
113
|
+
require_paths:
|
114
|
+
- lib
|
115
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
requirements: []
|
126
|
+
rubyforge_project:
|
127
|
+
rubygems_version: 2.7.6
|
128
|
+
signing_key:
|
129
|
+
specification_version: 4
|
130
|
+
summary: Generate an Ulam spiral sequence. PNG output available.
|
131
|
+
test_files: []
|