sweet_pi 0.1.3 → 0.2.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 +4 -4
- data/.github/workflows/gempush.yml +44 -0
- data/Gemfile.lock +1 -1
- data/lib/sweet_pi.rb +4 -33
- data/lib/sweet_pi/generator.rb +50 -0
- data/lib/sweet_pi/pi.rb +21 -18
- data/lib/sweet_pi/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 887f34ad314738bb6eaa5323069ccaf742d0662aab5c27925229af8a4a223cdb
|
4
|
+
data.tar.gz: 25519ab4f25156cb958e4808ec4c43744cfc3283107b3826ba2a783ee812fe6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b30e4539df9936bdd4f136dd8eacaedfaa3762a91d9062cb26dcf0c34c1ffd866f10fa8327c23ce362a3a9cb3a4019a943a2eb914457fd355f9a6b34219f77fa
|
7
|
+
data.tar.gz: 01435ee4b2825c913bb796f9576614b60ccf720739d054236f75afe8f88d6f83efb9b4462dcc8a38d878087c276f1fe98e8dbb0f0ad7fb23cd5b1e0fc344f713
|
@@ -0,0 +1,44 @@
|
|
1
|
+
name: Ruby Gem
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
push:
|
8
|
+
branches:
|
9
|
+
- master
|
10
|
+
|
11
|
+
jobs:
|
12
|
+
build:
|
13
|
+
name: Build + Publish
|
14
|
+
runs-on: ubuntu-latest
|
15
|
+
|
16
|
+
steps:
|
17
|
+
- uses: actions/checkout@master
|
18
|
+
- name: Set up Ruby 2.7
|
19
|
+
uses: actions/setup-ruby@v1
|
20
|
+
with:
|
21
|
+
version: 2.7.x
|
22
|
+
|
23
|
+
- name: Publish to GPR
|
24
|
+
run: |
|
25
|
+
mkdir -p $HOME/.gem
|
26
|
+
touch $HOME/.gem/credentials
|
27
|
+
chmod 0600 $HOME/.gem/credentials
|
28
|
+
printf -- "---\n:github: Bearer ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
29
|
+
gem build *.gemspec
|
30
|
+
gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
|
31
|
+
env:
|
32
|
+
GEM_HOST_API_KEY: ${{secrets.GPR_AUTH_TOKEN}}
|
33
|
+
OWNER: username
|
34
|
+
|
35
|
+
- name: Publish to RubyGems
|
36
|
+
run: |
|
37
|
+
mkdir -p $HOME/.gem
|
38
|
+
touch $HOME/.gem/credentials
|
39
|
+
chmod 0600 $HOME/.gem/credentials
|
40
|
+
printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
|
41
|
+
gem build *.gemspec
|
42
|
+
gem push *.gem
|
43
|
+
env:
|
44
|
+
GEM_HOST_API_KEY: ${{secrets.RUBYGEMS_AUTH_TOKEN}}
|
data/Gemfile.lock
CHANGED
data/lib/sweet_pi.rb
CHANGED
@@ -2,47 +2,18 @@
|
|
2
2
|
require 'sweet_pi/version'
|
3
3
|
require 'sweet_pi/runner'
|
4
4
|
require 'sweet_pi/pi'
|
5
|
+
require 'sweet_pi/generator'
|
5
6
|
|
6
7
|
require 'bigdecimal'
|
7
8
|
require 'bigdecimal/util'
|
8
9
|
|
9
10
|
|
10
11
|
module SweetPi
|
11
|
-
class
|
12
|
+
class << self
|
12
13
|
|
13
|
-
def
|
14
|
-
|
15
|
-
@pi = SweetPi::PI.new(process_size)
|
16
|
-
|
17
|
-
@from = 0
|
18
|
-
@to = 2
|
19
|
-
end
|
20
|
-
|
21
|
-
def range(from = 0, to)
|
22
|
-
@from = from
|
23
|
-
@to = to
|
24
|
-
self
|
25
|
-
end
|
26
|
-
|
27
|
-
def calc
|
28
|
-
@value = @pi.calc(@to)
|
29
|
-
self
|
14
|
+
def generator(process_size)
|
15
|
+
SweetPi::Generator.new(process_size)
|
30
16
|
end
|
31
17
|
|
32
|
-
def each_char
|
33
|
-
self.calc.to_s.each_char do |c|
|
34
|
-
yield c
|
35
|
-
end
|
36
|
-
|
37
|
-
self
|
38
|
-
end
|
39
|
-
|
40
|
-
def to_s
|
41
|
-
from = @from + 1 if 1 <= @from
|
42
|
-
to = @to + 2
|
43
|
-
|
44
|
-
@value ||= Math::PI.to_d
|
45
|
-
@value.to_s('f')[from..to]
|
46
|
-
end
|
47
18
|
end
|
48
19
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'sweet_pi/pi'
|
3
|
+
|
4
|
+
require 'bigdecimal'
|
5
|
+
require 'bigdecimal/util'
|
6
|
+
|
7
|
+
|
8
|
+
module SweetPi
|
9
|
+
class Generator
|
10
|
+
|
11
|
+
def initialize(process_count = 1)
|
12
|
+
@pi = SweetPi::PI.new
|
13
|
+
@pi.process_count = process_count
|
14
|
+
|
15
|
+
@from = 0
|
16
|
+
@to = 2
|
17
|
+
end
|
18
|
+
|
19
|
+
def process_count=(count)
|
20
|
+
@pi.process_count = count
|
21
|
+
end
|
22
|
+
|
23
|
+
def range(from = 0, to)
|
24
|
+
@from = from
|
25
|
+
@to = to
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def calc
|
30
|
+
@value = @pi.calc(@to)
|
31
|
+
self
|
32
|
+
end
|
33
|
+
|
34
|
+
def each_char
|
35
|
+
self.calc.to_s.each_char do |c|
|
36
|
+
yield c
|
37
|
+
end
|
38
|
+
|
39
|
+
self
|
40
|
+
end
|
41
|
+
|
42
|
+
def to_s
|
43
|
+
from = @from + 1 if 1 <= @from
|
44
|
+
to = @to + 2
|
45
|
+
|
46
|
+
@value ||= Math::PI.to_d
|
47
|
+
@value.to_s('f')[from..to]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/sweet_pi/pi.rb
CHANGED
@@ -16,19 +16,23 @@ module SweetPi
|
|
16
16
|
|
17
17
|
using SweetPi::Math::Factorial
|
18
18
|
|
19
|
-
def initialize(
|
20
|
-
|
21
|
-
|
19
|
+
def initialize()
|
20
|
+
@process_count = 1
|
21
|
+
end
|
22
|
+
|
23
|
+
def process_count=(count)
|
24
|
+
raise ArgumentError unless count.is_a?(Integer) and 1 <= count
|
25
|
+
@process_count = count
|
22
26
|
end
|
23
27
|
|
24
28
|
def calc(digit)
|
25
29
|
raise ArgumentError unless digit.is_a?(Integer) and 1 <= digit
|
26
30
|
accuracy = calc_accuracy(digit)
|
27
31
|
|
28
|
-
result = if @
|
32
|
+
result = if @process_count == 1
|
29
33
|
single_process(accuracy)
|
30
34
|
else
|
31
|
-
multi_process(accuracy, @
|
35
|
+
multi_process(accuracy, @process_count)
|
32
36
|
end
|
33
37
|
|
34
38
|
@prev_acc = accuracy
|
@@ -59,16 +63,16 @@ module SweetPi
|
|
59
63
|
|
60
64
|
def single_process(prev_acc = 0, accuracy)
|
61
65
|
sum = SweetPi::Math.sum(prev_acc, accuracy) do |k|
|
62
|
-
|
66
|
+
chudnovsky(k)
|
63
67
|
end
|
64
68
|
|
65
69
|
12 * sum
|
66
70
|
end
|
67
71
|
|
68
|
-
def multi_process(accuracy,
|
72
|
+
def multi_process(accuracy, process_count)
|
69
73
|
processes = []
|
70
|
-
|
71
|
-
processes << SweetPi::Fork.new(accuracy,
|
74
|
+
process_count.times do |p_n|
|
75
|
+
processes << SweetPi::Fork.new(accuracy, process_count, p_n) do |a, p_s, p_n|
|
72
76
|
each_process(a, p_s, p_n)
|
73
77
|
end
|
74
78
|
end
|
@@ -76,10 +80,10 @@ module SweetPi
|
|
76
80
|
processes.map(&:value).reduce(:+)
|
77
81
|
end
|
78
82
|
|
79
|
-
def each_process(accuracy,
|
83
|
+
def each_process(accuracy, process_count, process_num)
|
80
84
|
f = Proc.new do |x|
|
81
|
-
base =
|
82
|
-
a = (
|
85
|
+
base = process_count * x
|
86
|
+
a = (process_count - 1) * (x % 2)
|
83
87
|
b = process_num * (1 - (x % 2) * 2)
|
84
88
|
base + a + b
|
85
89
|
end
|
@@ -88,7 +92,7 @@ module SweetPi
|
|
88
92
|
x = 0
|
89
93
|
k = f.call(x)
|
90
94
|
while k <= accuracy do
|
91
|
-
sum +=
|
95
|
+
sum += chudnovsky(k)
|
92
96
|
x += 1
|
93
97
|
k = f.call(x)
|
94
98
|
end
|
@@ -99,12 +103,11 @@ module SweetPi
|
|
99
103
|
(digit / DIGIT_PER_ACCURACY).ceil.to_i
|
100
104
|
end
|
101
105
|
|
102
|
-
def
|
103
|
-
(-1)**k * (6 * k).! * (A + B * k)
|
104
|
-
|
106
|
+
def chudnovsky(k)
|
107
|
+
numerator = (-1)**k * (6 * k).! * (A + B * k)
|
108
|
+
denominator = k.!**3 * (3 * k).! * C**(3 * k + '1.5'.to_d)
|
105
109
|
|
106
|
-
|
107
|
-
k.!**3 * (3 * k).! * C**(3 * k + '1.5'.to_d)
|
110
|
+
Rational(numerator, denominator)
|
108
111
|
end
|
109
112
|
|
110
113
|
def fix(digit, pi)
|
data/lib/sweet_pi/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sweet_pi
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Toshiki
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-01-
|
11
|
+
date: 2020-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -88,6 +88,7 @@ executables:
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
+
- ".github/workflows/gempush.yml"
|
91
92
|
- ".gitignore"
|
92
93
|
- ".rspec"
|
93
94
|
- ".travis.yml"
|
@@ -103,6 +104,7 @@ files:
|
|
103
104
|
- exe/sweet_pi
|
104
105
|
- lib/sweet_pi.rb
|
105
106
|
- lib/sweet_pi/fork.rb
|
107
|
+
- lib/sweet_pi/generator.rb
|
106
108
|
- lib/sweet_pi/math.rb
|
107
109
|
- lib/sweet_pi/pi.rb
|
108
110
|
- lib/sweet_pi/runner.rb
|