pi_calc 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/pi_calc.rb +100 -0
- metadata +44 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ee18049810ffb35fe302c5db19727ac9e912fd18
|
4
|
+
data.tar.gz: 7bf704f86b512d044196883c849e32af78d74cd2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 97df3578a683dcdb89ad9f16124b7b407a87e83bc1ed7e93143869deed54d41f0f92906db2a9e00c5e0172f422bb5d93b7e3f5eb9885c80470effc5647f58b9b
|
7
|
+
data.tar.gz: e2dd0908b6b35ad238af371d98bd2a994bc4c7feb4806b230078bdeaa60b4552f57157c16b58bf51f44f0b4ad2c269613f2e848af22c783399c843cd160b3e22
|
data/lib/pi_calc.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
module PiCalc
|
2
|
+
PI = Math::PI.freeze
|
3
|
+
|
4
|
+
# Pi calculators
|
5
|
+
|
6
|
+
def self.constant(total=100000)
|
7
|
+
PI
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.buffons_needle(total = 100000)
|
11
|
+
running = 0
|
12
|
+
|
13
|
+
total.times do
|
14
|
+
ln = line
|
15
|
+
running += 1 if ln[:y][0].floor != ln[:y][1].floor
|
16
|
+
end
|
17
|
+
2.0 * total / running
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.buffons_needle_optimized(total = 100000)
|
21
|
+
running = 0
|
22
|
+
|
23
|
+
total.times do
|
24
|
+
ln = line_only_y
|
25
|
+
running += 1 if ln[:y][0].floor != ln[:y][1].floor
|
26
|
+
end
|
27
|
+
2.0 * total / running
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.gregory_leibniz(total = 100000)
|
31
|
+
running = 0
|
32
|
+
(1..total).each do |n|
|
33
|
+
x = 4.0 / ((n * 2) - 1)
|
34
|
+
running += n%2==0 ? x*-1 : x
|
35
|
+
end
|
36
|
+
running
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.nilakantha(total = 100000)
|
40
|
+
running = 3
|
41
|
+
total.times do |n|
|
42
|
+
z = (n * 2) + 2
|
43
|
+
x = 4.0 / (z * (z+1) * (z+2))
|
44
|
+
running += n%2==0 ? x : x*-1
|
45
|
+
end
|
46
|
+
running
|
47
|
+
end
|
48
|
+
|
49
|
+
# Timer
|
50
|
+
|
51
|
+
def self.time(methods = [], first = 100000, verbose = true)
|
52
|
+
times = {}
|
53
|
+
methods.each do |n|
|
54
|
+
sym = n.to_sym
|
55
|
+
t = Time.now
|
56
|
+
output = PiCalc.send(sym, first)
|
57
|
+
time = Time.now - t
|
58
|
+
times[n] = {'time' => time, 'output' => output}
|
59
|
+
puts "'#{n}' took #{time} seconds\n\tIt ouputted '#{output}'\n\n\n" if verbose
|
60
|
+
end
|
61
|
+
times
|
62
|
+
end
|
63
|
+
|
64
|
+
# Testing
|
65
|
+
|
66
|
+
def self.test(total = 100000, verbose = true)
|
67
|
+
output = PiCalc.time([:buffons_needle, :buffons_needle_optimized, :gregory_leibniz, :nilakantha, :constant], total, verbose).inspect
|
68
|
+
puts output if verbose
|
69
|
+
output
|
70
|
+
end
|
71
|
+
|
72
|
+
# Line constructors
|
73
|
+
|
74
|
+
def self.line
|
75
|
+
angle0 = rand * PI * 2
|
76
|
+
angle1 = rand * PI * 2
|
77
|
+
|
78
|
+
length_on_line = rand * 100
|
79
|
+
|
80
|
+
x0 = Math.cos(angle0) * length_on_line
|
81
|
+
x1 = Math.cos(angle1) + x0
|
82
|
+
|
83
|
+
y0 = Math.sin(angle0) * length_on_line
|
84
|
+
y1 = Math.cos(angle1) + y0
|
85
|
+
|
86
|
+
{:x => [x0, x1], :y => [y0, y1], :angle => angle1}
|
87
|
+
end
|
88
|
+
|
89
|
+
def self.line_only_y
|
90
|
+
angle0 = rand * PI * 2
|
91
|
+
angle1 = rand * PI * 2
|
92
|
+
|
93
|
+
length_on_line = rand * 100
|
94
|
+
|
95
|
+
y0 = Math.sin(angle0) * length_on_line
|
96
|
+
y1 = Math.cos(angle1) + y0
|
97
|
+
|
98
|
+
{:y => [y0, y1], :angle => angle1}
|
99
|
+
end
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pi_calc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Winston Durand
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-28 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A simple gem that adds multiple ways to calculate pi.
|
14
|
+
email: me@winstondurand.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/pi_calc.rb
|
20
|
+
homepage: http://rubygems.org/gems/pi_calc
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ">="
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.2.2
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: A pi calculator
|
44
|
+
test_files: []
|