factoradic 1.0.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
- checksums.yaml.gz.sig +0 -0
- data/.gitignore +8 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/certs/pdkl95.pem +25 -0
- data/exe/factoradic +187 -0
- data/factoradic.gemspec +29 -0
- data/lib/factoradic/version.rb +3 -0
- data/lib/factoradic.rb +194 -0
- data.tar.gz.sig +0 -0
- metadata +86 -0
- metadata.gz.sig +1 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 651d57aa6f064e12180804ad826db5b7bf1acbc514895c2c5e18dce037c8eb0d
|
4
|
+
data.tar.gz: 689d295a573868e9f9c62c3e31aab98f7ba6d3ea08cf2fb48390ea9865221a0c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 336752cb22d8f52b4de0cf22b0626da77d8e2b3360e891d12a3b3d6a14d1b1b54d79744fd8dc93f7b6b07dd621b5e64a2d0a8668276b0d69699108f5b124a9ea
|
7
|
+
data.tar.gz: e7e6b64f1a2961830064957146a9996f4845a5ac28bf9a44a1ae6742c695d930056d2ad35666a7d9d34aa719603a63109ee62d2cf6e826080052bac3e35bcc42
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
factoradic (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
minitest (5.15.0)
|
10
|
+
rake (12.3.3)
|
11
|
+
|
12
|
+
PLATFORMS
|
13
|
+
ruby
|
14
|
+
|
15
|
+
DEPENDENCIES
|
16
|
+
factoradic!
|
17
|
+
minitest (~> 5.0)
|
18
|
+
rake (~> 12.0)
|
19
|
+
|
20
|
+
BUNDLED WITH
|
21
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Brent Sanders
|
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,83 @@
|
|
1
|
+
# Factoradic
|
2
|
+
|
3
|
+
Factorial base integer conversion.
|
4
|
+
|
5
|
+
[https://en.wikipedia.org/wiki/Factorial_number_system](https://en.wikipedia.org/wiki/Factorial_number_system)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'factoradic'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install factoradic
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
### The command line utility
|
26
|
+
|
27
|
+
#### Options
|
28
|
+
|
29
|
+
Usage: exe/factoradic [options] <number> [...]
|
30
|
+
|
31
|
+
OPTIONS
|
32
|
+
-i, --input=BASE Interpret input strings in <BASE> format
|
33
|
+
(valid bases: d, f, decimal, factorial)
|
34
|
+
-o, --output=BASE Print converted numbers in <BASE> format
|
35
|
+
(valid bases: d, f, decimal, factorial)
|
36
|
+
|
37
|
+
--d2f Shorthand for --input=decimal --output=factorial
|
38
|
+
--f2d Shorthand for --input=factorial --output=decimal
|
39
|
+
|
40
|
+
-s, --separator=CHAR Use <CHAR> as the factorial base position separator
|
41
|
+
(default: ":")
|
42
|
+
--[no-]cache Enable/disable caching calculated factorial values.
|
43
|
+
(default: On)
|
44
|
+
|
45
|
+
-h, --help Show this help message
|
46
|
+
--version Show version
|
47
|
+
|
48
|
+
#### Examples
|
49
|
+
|
50
|
+
$ factoradic 5:4:3:2:1:0
|
51
|
+
719
|
52
|
+
|
53
|
+
$ factoradic 1:0:0:0:0:0:0
|
54
|
+
720
|
55
|
+
|
56
|
+
$ factoradic 463
|
57
|
+
3:4:1:0:1:0
|
58
|
+
|
59
|
+
$ factoradic --separator=',' 463
|
60
|
+
3,4,1,0,1,0
|
61
|
+
|
62
|
+
$ echo 1:3:0:0:0 | factoradic --f2d -
|
63
|
+
42
|
64
|
+
|
65
|
+
$ ( echo 719 ; echo 720 ) | factoradic --d2f -
|
66
|
+
5:4:3:2:1:0
|
67
|
+
1:0:0:0:0:0:0
|
68
|
+
|
69
|
+
|
70
|
+
## Development
|
71
|
+
|
72
|
+
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.
|
73
|
+
|
74
|
+
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).
|
75
|
+
|
76
|
+
## Contributing
|
77
|
+
|
78
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/pdkl95/factoradic.
|
79
|
+
|
80
|
+
|
81
|
+
## License
|
82
|
+
|
83
|
+
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 "factoradic"
|
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/certs/pdkl95.pem
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
-----BEGIN CERTIFICATE-----
|
2
|
+
MIIEMDCCApigAwIBAgIBATANBgkqhkiG9w0BAQsFADAhMR8wHQYDVQQDDBZwZGts
|
3
|
+
OTUvREM9Z21haWwvREM9Y29tMB4XDTIyMDIwMTIxNDQxMFoXDTIzMDIwMTIxNDQx
|
4
|
+
MFowITEfMB0GA1UEAwwWcGRrbDk1L0RDPWdtYWlsL0RDPWNvbTCCAaIwDQYJKoZI
|
5
|
+
hvcNAQEBBQADggGPADCCAYoCggGBAJrJqYa1q3ckmQ6L0v5r2KpJLFrks7L9cmpO
|
6
|
+
qdo3QdUZqnBbIIuhkUOy+rzUM//lCvZeT2Rym8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yPskr53EKYR6bzvF4V+w5+Lq/JxWIbaB5UxY1kCxGrOubzQLvwE
|
22
|
+
tf/k74CEmUNN71A4MXGVuydmgrlok/LBZO8Lqa9Dt+95nBdqKz5vIcO6szPGZIid
|
23
|
+
NMwK52EhsO4bDuwRB7r1zYTj42O24s0o0yJQPzqhfQEGssdLDNkEr147K0symobj
|
24
|
+
l35DiP6VyFMH/Y0JWgs65E5bGzk=
|
25
|
+
-----END CERTIFICATE-----
|
data/exe/factoradic
ADDED
@@ -0,0 +1,187 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'factoradic'
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
Options = Struct.new(:instream, :outstream, :input, :output)
|
7
|
+
options = Options.new
|
8
|
+
options.instream = STDIN
|
9
|
+
options.outstream = STDOUT
|
10
|
+
|
11
|
+
optparse = OptionParser.new do |opts|
|
12
|
+
opts.banner = "Converts between factorial base and decimal"
|
13
|
+
opts.define_head "Usage: #{$0} [options] <number> [...]"
|
14
|
+
opts.summary_width = 24
|
15
|
+
opts.summary_indent = ' '
|
16
|
+
|
17
|
+
opts.separator ''
|
18
|
+
opts.separator 'OPTIONS'
|
19
|
+
|
20
|
+
VALID_BASES = ['d', 'f', 'decimal', 'factorial']
|
21
|
+
VALID_BASES_HELP = "(valid bases: #{VALID_BASES.join(', ')})"
|
22
|
+
|
23
|
+
def get_base_opt(str)
|
24
|
+
case str
|
25
|
+
when 'd', 'decimal'
|
26
|
+
:decimal
|
27
|
+
when 'f', 'factorial'
|
28
|
+
:factorial
|
29
|
+
else
|
30
|
+
raise ArgumentError, "invalid BASE: #{str.inspect} - rxpected: #{VALID_BASES.join(', ')}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
opts.on('-iBASE', '--input=BASE',
|
35
|
+
'Interpret input strings in <BASE> format',
|
36
|
+
VALID_BASES_HELP) do |base|
|
37
|
+
options.input = get_base_opt(base)
|
38
|
+
end
|
39
|
+
|
40
|
+
opts.on('-oBASE', '--output=BASE',
|
41
|
+
'Print converted numbers in <BASE> format',
|
42
|
+
VALID_BASES_HELP) do |base|
|
43
|
+
options.output = get_base_opt(base)
|
44
|
+
end
|
45
|
+
|
46
|
+
opts.separator ''
|
47
|
+
|
48
|
+
opts.on('--d2f', 'Shorthand for --input=decimal --output=factorial') do
|
49
|
+
options.input = :decimal
|
50
|
+
options.output = :factorial
|
51
|
+
end
|
52
|
+
|
53
|
+
opts.on('--f2d', 'Shorthand for --input=factorial --output=decimal') do
|
54
|
+
options.input = :factorial
|
55
|
+
options.output = :decimal
|
56
|
+
end
|
57
|
+
|
58
|
+
opts.separator ''
|
59
|
+
opts.on('-s', '--separator=CHAR',
|
60
|
+
'Use <CHAR> as the factorial base position separator',
|
61
|
+
"(default: \"#{Factoradic.options.separator}\")") do |sep|
|
62
|
+
Factoradic.options.separator = sep
|
63
|
+
end
|
64
|
+
|
65
|
+
cache_default = if Factoradic.options.memoize_factorial_values
|
66
|
+
'On'
|
67
|
+
else
|
68
|
+
'Off'
|
69
|
+
end
|
70
|
+
opts.on('--[no-]cache',
|
71
|
+
'Enable/disable caching calculated factorial values.',
|
72
|
+
"(default: #{cache_default})") do |cache|
|
73
|
+
Factoradic.options.memoize_factorial_values = cache
|
74
|
+
end
|
75
|
+
|
76
|
+
opts.separator ''
|
77
|
+
|
78
|
+
opts.on('-h', '--help', 'Show this help message') do
|
79
|
+
puts opts
|
80
|
+
puts <<USAGE_END
|
81
|
+
|
82
|
+
BEHAVIOR
|
83
|
+
|
84
|
+
If multiple strings are given, each converted value will be
|
85
|
+
printed, one value per-line.
|
86
|
+
|
87
|
+
If the first arg is the single character string '-', strings
|
88
|
+
will be read from STDIN instead, one value per-line.
|
89
|
+
|
90
|
+
AUTOMAGIC BASE DETECTION
|
91
|
+
|
92
|
+
By default, if an input string contains ',' or ':' separators,
|
93
|
+
(or the character provided as a --separator) it will be parsed
|
94
|
+
as a factoradic (factorial base) number and the decimal
|
95
|
+
representation will be printed.
|
96
|
+
|
97
|
+
Otherwise, the input string will be parsed as a decimal (base-10)
|
98
|
+
number and the factoradic (factorial base) representation of
|
99
|
+
the number will be printed.
|
100
|
+
|
101
|
+
Automagic base detection is disabled with the explicit input
|
102
|
+
and output bases are provided with the --input and --output
|
103
|
+
options (or --d2f/--f2d).
|
104
|
+
|
105
|
+
CACHING CALCULATED FACTORIAL VALUES
|
106
|
+
|
107
|
+
The --cache option will enable a RAM cache to memoize
|
108
|
+
calculated factorial values. This may be faster when
|
109
|
+
converting many numbers at a time, at the cost of
|
110
|
+
potentially much higher RAM usage.
|
111
|
+
|
112
|
+
Caching calculated values is only useful if the
|
113
|
+
same factorial value needs to be calculated
|
114
|
+
multiple times. Enabling the cache while providing
|
115
|
+
only a single number to convert is just wastes RAM.
|
116
|
+
|
117
|
+
EXAMPLES
|
118
|
+
|
119
|
+
$ factoradic 5:4:3:2:1:0
|
120
|
+
719
|
121
|
+
|
122
|
+
$ factoradic 1:0:0:0:0:0:0
|
123
|
+
720
|
124
|
+
|
125
|
+
$ factoradic 463
|
126
|
+
3:4:1:0:1:0
|
127
|
+
|
128
|
+
$ factoradic --separator=',' 463
|
129
|
+
3,4,1,0,1,0
|
130
|
+
|
131
|
+
$ echo 1:3:0:0:0 | factoradic --f2d -
|
132
|
+
42
|
133
|
+
|
134
|
+
$ ( echo 719 ; echo 720 ) | factoradic --d2f -
|
135
|
+
5:4:3:2:1:0
|
136
|
+
1:0:0:0:0:0:0
|
137
|
+
|
138
|
+
USAGE_END
|
139
|
+
|
140
|
+
exit
|
141
|
+
end
|
142
|
+
|
143
|
+
opts.on('--version', 'Show version') do
|
144
|
+
puts Factoradic::VERSION
|
145
|
+
exit
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
optparse.parse!
|
150
|
+
|
151
|
+
if ARGV.length > 0
|
152
|
+
unless ARGV.first == '-'
|
153
|
+
options.instream = ARGV
|
154
|
+
end
|
155
|
+
else
|
156
|
+
puts optparse
|
157
|
+
exit 1
|
158
|
+
end
|
159
|
+
|
160
|
+
options.instream.each do |arg|
|
161
|
+
n, t = case options.input
|
162
|
+
when :decimal
|
163
|
+
[Factoradic.parse_decimal(arg), :decimal]
|
164
|
+
when :factorial
|
165
|
+
[Factoradic.parse_factoradic(arg), :factorial]
|
166
|
+
else
|
167
|
+
Factoradic.parse(arg)
|
168
|
+
end
|
169
|
+
|
170
|
+
outmode = if options.output.nil?
|
171
|
+
case t
|
172
|
+
when :factorial then :decimal
|
173
|
+
when :decimal then :factorial
|
174
|
+
else
|
175
|
+
:decimal
|
176
|
+
end
|
177
|
+
else
|
178
|
+
options.output
|
179
|
+
end
|
180
|
+
|
181
|
+
options.outstream.puts case outmode
|
182
|
+
when :decimal
|
183
|
+
n.to_i.to_s
|
184
|
+
when :factorial
|
185
|
+
n.to_s
|
186
|
+
end
|
187
|
+
end
|
data/factoradic.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'lib/factoradic/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "factoradic"
|
5
|
+
spec.version = Factoradic::VERSION
|
6
|
+
spec.authors = ["Brent Sanders"]
|
7
|
+
spec.email = ["pdkl95@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Factorial base integer conversion.}
|
10
|
+
spec.description = %q{Parse factorial base integers and generate factorial base representations of integers.}
|
11
|
+
spec.homepage = "https://github.com/pdkl95/factoradic"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
17
|
+
|
18
|
+
spec.cert_chain = ['certs/pdkl95.pem']
|
19
|
+
spec.signing_key = File.expand_path("~/.ssh/gem-private_key.pem") if $0 =~ /gem\z/
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
end
|
data/lib/factoradic.rb
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
require_relative 'factoradic/version'
|
2
|
+
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
class Factoradic
|
6
|
+
DEFAULT_OPTIONS = {
|
7
|
+
# disabling this will save ram when computing
|
8
|
+
# very large (Bignum) factorial values
|
9
|
+
memoize_factorial_values: false,
|
10
|
+
|
11
|
+
# the separator used between placex in factorial base
|
12
|
+
separator: ':'
|
13
|
+
}
|
14
|
+
|
15
|
+
@options = OpenStruct.new(DEFAULT_OPTIONS)
|
16
|
+
|
17
|
+
# cache of basic factorial vslues
|
18
|
+
@factorial_sequence = [1]
|
19
|
+
|
20
|
+
class << self
|
21
|
+
attr_accessor :options
|
22
|
+
|
23
|
+
def nonstandard_separator?
|
24
|
+
options.separator != DEFAULT_OPTIONS[:separator]
|
25
|
+
end
|
26
|
+
|
27
|
+
def string_is_factoradic?(str)
|
28
|
+
re = if nonstandard_separator?
|
29
|
+
/\A\d+([#{options.separator}]\d+)+\Z/
|
30
|
+
else
|
31
|
+
/\A\d+([,:]\d+)+\Z/
|
32
|
+
end
|
33
|
+
!!(str =~ re)
|
34
|
+
end
|
35
|
+
|
36
|
+
def parse_factoradic(str)
|
37
|
+
new.tap do |f|
|
38
|
+
f.parse_factoradic(str)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def parse_decimal(decimal)
|
43
|
+
intvalue = case decimal
|
44
|
+
when Integer
|
45
|
+
decimal
|
46
|
+
when String
|
47
|
+
decimal.to_i(10)
|
48
|
+
else
|
49
|
+
raise ArgumentError, "Expected an Integer or String{"
|
50
|
+
end
|
51
|
+
|
52
|
+
new.tap do |f|
|
53
|
+
f.value = intvalue
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def parse(str)
|
58
|
+
if string_is_factoradic?(str)
|
59
|
+
[parse_factoradic(str), :factorial]
|
60
|
+
else
|
61
|
+
[parse_decimal(str), :decimal]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def autoconvert(str)
|
66
|
+
if string_is_factoradic?(str)
|
67
|
+
convert_factoradic_to_decimal(str)
|
68
|
+
else
|
69
|
+
convert_decimal_to_factoradic(str)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def convert_factoradic_to_decimal(str)
|
74
|
+
f = parse_factoradic(str)
|
75
|
+
f.to_i.to_s(10)
|
76
|
+
end
|
77
|
+
alias f2d convert_factoradic_to_decimal
|
78
|
+
|
79
|
+
def convert_decimal_to_factoradic(decimal)
|
80
|
+
f = parse_decimal(decimal)
|
81
|
+
f.to_s
|
82
|
+
end
|
83
|
+
alias d2f convert_decimal_to_factoradic
|
84
|
+
|
85
|
+
def valid_factoradic_digits?(digit_list)
|
86
|
+
digit_list.reverse.map.with_index do |digit, idx|
|
87
|
+
(digit >= 0) && (digit <= idx)
|
88
|
+
end.all?(true)
|
89
|
+
end
|
90
|
+
|
91
|
+
def memoized_factorial(n)
|
92
|
+
if @factorial_sequence.length > n
|
93
|
+
@factorial_sequence[n]
|
94
|
+
else
|
95
|
+
@factorial_sequence[n] = n * memoized_factorial(n - 1)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def basic_factorial(n)
|
100
|
+
(1..n).reduce(1, :*)
|
101
|
+
end
|
102
|
+
|
103
|
+
def factorial(n)
|
104
|
+
if options.memoize_factorial_values
|
105
|
+
memoized_factorial(n)
|
106
|
+
else
|
107
|
+
basic_factorial(n)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def initialize
|
113
|
+
@value = 0
|
114
|
+
@digits = [0]
|
115
|
+
end
|
116
|
+
|
117
|
+
def options
|
118
|
+
self.class.options
|
119
|
+
end
|
120
|
+
|
121
|
+
def recompute_digits!
|
122
|
+
x = @value
|
123
|
+
divisor = 2
|
124
|
+
@digits = [0]
|
125
|
+
|
126
|
+
while x > 0
|
127
|
+
x, r = x.divmod(divisor)
|
128
|
+
@digits.push(r)
|
129
|
+
divisor += 1
|
130
|
+
end
|
131
|
+
|
132
|
+
@digits.reverse!
|
133
|
+
end
|
134
|
+
|
135
|
+
def recompute_value!
|
136
|
+
@value = @digits.reverse.map.with_index do |digit, idx|
|
137
|
+
digit * Factoradic.factorial(idx)
|
138
|
+
end.reduce(&:+)
|
139
|
+
end
|
140
|
+
|
141
|
+
def [](i)
|
142
|
+
@digits[i]
|
143
|
+
end
|
144
|
+
|
145
|
+
def []=(i, rvalue)
|
146
|
+
new_places = @digits.dup
|
147
|
+
new_places[i] = rvalue.to_i
|
148
|
+
self.digits = new_digits
|
149
|
+
end
|
150
|
+
|
151
|
+
def parse_factoradic(str)
|
152
|
+
sep = if Factoradic.nonstandard_separator?
|
153
|
+
/[#{Factoradic.options.separator}]/
|
154
|
+
else
|
155
|
+
/[,:]/
|
156
|
+
end
|
157
|
+
|
158
|
+
self.digits = str.split(sep).map{ |x| x.to_i }
|
159
|
+
end
|
160
|
+
|
161
|
+
def digits=(new_digits)
|
162
|
+
new_digits = new_digits.map do |x|
|
163
|
+
if x.nil?
|
164
|
+
0
|
165
|
+
else
|
166
|
+
x
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
unless Factoradic.valid_factoradic_digits?(new_digits)
|
171
|
+
raise ArgumentError, "Invalid factoradic digits: #{new_digits.inspect}"
|
172
|
+
end
|
173
|
+
|
174
|
+
@digits = new_digits
|
175
|
+
recompute_value!
|
176
|
+
end
|
177
|
+
|
178
|
+
def value=(new_value)
|
179
|
+
@value = new_value.to_i
|
180
|
+
recompute_digits!
|
181
|
+
end
|
182
|
+
|
183
|
+
def to_s(separator = options.separator)
|
184
|
+
@digits.join(separator)
|
185
|
+
end
|
186
|
+
|
187
|
+
def to_i
|
188
|
+
@value
|
189
|
+
end
|
190
|
+
|
191
|
+
def inspect
|
192
|
+
"#<Factoradic #{to_s}>"
|
193
|
+
end
|
194
|
+
end
|
data.tar.gz.sig
ADDED
Binary file
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: factoradic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brent Sanders
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12
|
+
-----BEGIN CERTIFICATE-----
|
13
|
+
MIIEMDCCApigAwIBAgIBATANBgkqhkiG9w0BAQsFADAhMR8wHQYDVQQDDBZwZGts
|
14
|
+
OTUvREM9Z21haWwvREM9Y29tMB4XDTIyMDIwMTIxNDQxMFoXDTIzMDIwMTIxNDQx
|
15
|
+
MFowITEfMB0GA1UEAwwWcGRrbDk1L0RDPWdtYWlsL0RDPWNvbTCCAaIwDQYJKoZI
|
16
|
+
hvcNAQEBBQADggGPADCCAYoCggGBAJrJqYa1q3ckmQ6L0v5r2KpJLFrks7L9cmpO
|
17
|
+
qdo3QdUZqnBbIIuhkUOy+rzUM//lCvZeT2Rym8zysJWslATnjhqoKdC6HhAnM7u8
|
18
|
+
puI1Xgr0vy84YCc4Ec0iRyrUQkwRB0flQ1XzmH4CJzMD9HafjF0JbCakoawxYsWu
|
19
|
+
A8DJ4mZn8sdz0BjGF5nHaLtt/cTx4FQpHLcU6AJSep0BP+fn87HGsrvhLEcITx5v
|
20
|
+
8KYnWAdCEiSVCprmANn0rEeD/vCONotRdUBGSmx6aYV/ijujS+ND7a0hk8d3lhkk
|
21
|
+
yvW9L4GM6i3lDsM/w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00:00:00.000000000 Z
|
38
|
+
dependencies: []
|
39
|
+
description: Parse factorial base integers and generate factorial base representations
|
40
|
+
of integers.
|
41
|
+
email:
|
42
|
+
- pdkl95@gmail.com
|
43
|
+
executables:
|
44
|
+
- factoradic
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- Gemfile.lock
|
51
|
+
- LICENSE.txt
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- bin/console
|
55
|
+
- bin/setup
|
56
|
+
- certs/pdkl95.pem
|
57
|
+
- exe/factoradic
|
58
|
+
- factoradic.gemspec
|
59
|
+
- lib/factoradic.rb
|
60
|
+
- lib/factoradic/version.rb
|
61
|
+
homepage: https://github.com/pdkl95/factoradic
|
62
|
+
licenses:
|
63
|
+
- MIT
|
64
|
+
metadata:
|
65
|
+
homepage_uri: https://github.com/pdkl95/factoradic
|
66
|
+
source_code_uri: https://github.com/pdkl95/factoradic
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options: []
|
69
|
+
require_paths:
|
70
|
+
- lib
|
71
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 2.3.0
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements: []
|
82
|
+
rubygems_version: 3.1.4
|
83
|
+
signing_key:
|
84
|
+
specification_version: 4
|
85
|
+
summary: Factorial base integer conversion.
|
86
|
+
test_files: []
|
metadata.gz.sig
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
���f�����d��|�7��'��}Z\W���9���e�3"�U�v|�A�c90`�����K�W��>��FB[������S!��U�"�X̎����hXZ���ٱ�D��;����M6y,!Z�-�"�c�H@�C�/tO =w�{�O�~Y��Z4�������փ>��"�v�Cׁ��Ú�A�Z���8v��՟{��Sik�}d��Q�x/g`���<!�e��/ì�7���� �v���0 �4��[�j�4̞����ްr��)���z���F�@9H,��h�� �[�����1��꾋�[Ú�$a,�.��C)��c����u�����]I���q�*+��f�&�2��#�������D٬OG��v��s]�)���t-
|