multiples 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 +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +95 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/multiples/multiples_enumerator.rb +78 -0
- data/lib/multiples/version.rb +3 -0
- data/lib/multiples.rb +8 -0
- data/multiples.gemspec +25 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e7edc53b90318e0babfb1c73ecad1b2d09966129
|
4
|
+
data.tar.gz: a3d791a1a8351b9d817e374cb6d8692a6caa7fd2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a447a6aa6fa6d97445eba165033eb4fe7695b6099ef0d35470a9872a8cd38ff18ac668fbe0e8dca01ad584e816b5be8f67b2b4599ff79657ef69c6b1ad50688
|
7
|
+
data.tar.gz: fb11443773d0f4823353d45c8c4528480cf3769fd73ec09225cfee1fb9869ab4bd6d8af152b2284f758ec89f3887fa360fa2df4ef849ee49c41ed8d097df03f0
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Daniel P. Clark
|
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,95 @@
|
|
1
|
+
# Multiples
|
2
|
+
|
3
|
+
Creates enumerators that step through integers from a union of two multiples.
|
4
|
+
A pattern from the two numbers is determined (much like two frequencies create
|
5
|
+
a pattern) and the resulting palindrome is given to a custom Enumerator for you
|
6
|
+
to use.
|
7
|
+
|
8
|
+
I've created this gem from a delightful discovery I made. When going through all
|
9
|
+
the numbers that are multiples of two different digits a pattern comes through.
|
10
|
+
This pattern is a palindrome. See for yourself:
|
11
|
+
|
12
|
+
```
|
13
|
+
For multiples of 3 with 5 we have.
|
14
|
+
|
15
|
+
3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45
|
16
|
+
3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3
|
17
|
+
```
|
18
|
+
|
19
|
+
The palindrome for the above differences example is `3,2,1,3,1,2,3`. I thought this
|
20
|
+
may be true for other numbers as well so lets look at 4 with 5.
|
21
|
+
|
22
|
+
```
|
23
|
+
4 5 8 10 12 15 16 20 24 25 28 30 32 35 36 40
|
24
|
+
4 1 3 2 2 3 1 4 4 1 3 2 2 3 1 4
|
25
|
+
```
|
26
|
+
|
27
|
+
As you can see from this example another palindrome has arisen `4,1,3,2,2,3,1,4`. Upon
|
28
|
+
further study of these patterns I have also found that the palindrome will always be the
|
29
|
+
same length as the two numbers added together minus one. So 3 and 5's palindrome is the
|
30
|
+
length `3+5-1=7` and 4 and 5's palindromes length is `4+5-1=8`. I wrote code that tests
|
31
|
+
any two different digits and those are accurate lengths.
|
32
|
+
|
33
|
+
So if you'd like an iterator that steps through multiples without calculating every step
|
34
|
+
of the way you may use this gem for that.
|
35
|
+
|
36
|
+
### NOTES
|
37
|
+
|
38
|
+
* If one of the numbers is divisible by the other than this rule doesn't apply.
|
39
|
+
* The gem doesn't take into account zero and negative numbers. If you wanted to you may progress backwards through negative numbers.
|
40
|
+
|
41
|
+
## Installation
|
42
|
+
|
43
|
+
Add this line to your application's Gemfile:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
gem 'multiples'
|
47
|
+
```
|
48
|
+
|
49
|
+
And then execute:
|
50
|
+
|
51
|
+
$ bundle
|
52
|
+
|
53
|
+
Or install it yourself as:
|
54
|
+
|
55
|
+
$ gem install multiples
|
56
|
+
|
57
|
+
## Usage
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
x = Multiples.new(3,5)
|
61
|
+
x.next
|
62
|
+
# => 3
|
63
|
+
|
64
|
+
x.each do |val|
|
65
|
+
break val if val > 14
|
66
|
+
end
|
67
|
+
# => 15
|
68
|
+
|
69
|
+
x.lazy.select {|v| v % 10 == 0}.take(2).to_a
|
70
|
+
# => [20, 30]
|
71
|
+
x.next
|
72
|
+
# => 33
|
73
|
+
x.phase
|
74
|
+
# => 1
|
75
|
+
x.prev
|
76
|
+
# => 30
|
77
|
+
x.phase
|
78
|
+
# => 0
|
79
|
+
```
|
80
|
+
|
81
|
+
## Development
|
82
|
+
|
83
|
+
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.
|
84
|
+
|
85
|
+
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).
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/danielpclark/multiples.
|
90
|
+
|
91
|
+
|
92
|
+
## License
|
93
|
+
|
94
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
95
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "multiples"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
module Multiples
|
2
|
+
module MultiplesEnumGenerator
|
3
|
+
class << self
|
4
|
+
def new a, b
|
5
|
+
@a, @b = (a..Float::INFINITY).step(a), (b..Float::INFINITY).step(b)
|
6
|
+
@palindrome_length = a + b - 1
|
7
|
+
@stack = [[@a.peek, @b.peek].min.to_i]
|
8
|
+
send :build_palindrome
|
9
|
+
return MultiplesEnumerator.new(@stack)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
def build_palindrome
|
14
|
+
until @stack.length == @palindrome_length
|
15
|
+
@stack << step
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def step
|
20
|
+
val = [@a.peek, @b.peek].max - [@a.peek, @b.peek].min
|
21
|
+
if val > @stack[0]
|
22
|
+
val = @stack[0]
|
23
|
+
end
|
24
|
+
h = {@a.peek => @a, @b.peek => @b}
|
25
|
+
h[h.keys.min].next
|
26
|
+
val.to_i
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
class MultiplesEnumerator
|
32
|
+
attr_reader :palindrome, :phase
|
33
|
+
def initialize palindrome
|
34
|
+
@base = 0
|
35
|
+
@palindrome = palindrome
|
36
|
+
@phase = 0
|
37
|
+
end
|
38
|
+
|
39
|
+
def current
|
40
|
+
@base
|
41
|
+
end
|
42
|
+
|
43
|
+
def each
|
44
|
+
loop do
|
45
|
+
send :next
|
46
|
+
yield @base
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def lazy
|
51
|
+
Enumerator::Lazy.new(self) do |yielder, *vals|
|
52
|
+
yielder.<<(*vals)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def next
|
57
|
+
@base += progress_phase
|
58
|
+
end
|
59
|
+
|
60
|
+
def prev
|
61
|
+
@base -= regress_phase
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
def progress_phase
|
66
|
+
val = @palindrome[@phase]
|
67
|
+
@phase += 1
|
68
|
+
@phase = 0 if @phase > @palindrome.length - 1
|
69
|
+
val
|
70
|
+
end
|
71
|
+
|
72
|
+
def regress_phase
|
73
|
+
@phase -= 1
|
74
|
+
@phase = @palindrome.length - 1 if @phase < 0
|
75
|
+
@palindrome[@phase]
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/multiples.rb
ADDED
data/multiples.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'multiples/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "multiples"
|
8
|
+
spec.version = Multiples::VERSION
|
9
|
+
spec.authors = ["Daniel P. Clark"]
|
10
|
+
spec.email = ["6ftdan@gmail.com"]
|
11
|
+
spec.summary = %q{Creates enumerators that step through integers from a union of two multiples"}
|
12
|
+
spec.description = %q{Creates enumerators that step through integers from a union of two multiples. A pattern from the two numbers is determined (much like two frequencies create a pattern) and the resulting palindrome is given to a custom Enumerator for you to use."}
|
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_development_dependency "bundler", "~> 1.13"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: multiples
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel P. Clark
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
description: Creates enumerators that step through integers from a union of two multiples.
|
56
|
+
A pattern from the two numbers is determined (much like two frequencies create a
|
57
|
+
pattern) and the resulting palindrome is given to a custom Enumerator for you to
|
58
|
+
use."
|
59
|
+
email:
|
60
|
+
- 6ftdan@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- ".gitignore"
|
66
|
+
- ".travis.yml"
|
67
|
+
- Gemfile
|
68
|
+
- LICENSE.txt
|
69
|
+
- README.md
|
70
|
+
- Rakefile
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/multiples.rb
|
74
|
+
- lib/multiples/multiples_enumerator.rb
|
75
|
+
- lib/multiples/version.rb
|
76
|
+
- multiples.gemspec
|
77
|
+
homepage:
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.6.4
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: Creates enumerators that step through integers from a union of two multiples"
|
101
|
+
test_files: []
|