Oompa-euler 1.0.2 → 1.0.3
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.
- data/History.txt +10 -0
- data/Manifest.txt +8 -0
- data/{LICENSE → README.txt} +31 -0
- data/lib/euler.rb +16 -29
- data/test/tc_integer_methods.rb +44 -0
- data/test/tc_module.rb +40 -0
- data/test/test_euler.rb +4 -0
- metadata +14 -11
- data/README.mkdn +0 -5
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/{LICENSE → README.txt}
RENAMED
@@ -1,3 +1,33 @@
|
|
1
|
+
= euler
|
2
|
+
|
3
|
+
== DESCRIPTION:
|
4
|
+
|
5
|
+
libeuler is a simple library you can include in your code to take away some of the boring repetitive parts of solving Project Euler problems.
|
6
|
+
|
7
|
+
== FEATURES:
|
8
|
+
|
9
|
+
* Adds various methods to the Integer class, such as factorial, prime?, is_fibonacci?, etc.
|
10
|
+
* Provides various module methods, such as generate_sieve, get_primes, etc.
|
11
|
+
|
12
|
+
== SYNOPSIS:
|
13
|
+
|
14
|
+
require 'rubygems'
|
15
|
+
require 'euler'
|
16
|
+
|
17
|
+
5.factorial # => 120
|
18
|
+
|
19
|
+
== REQUIREMENTS:
|
20
|
+
|
21
|
+
* none
|
22
|
+
|
23
|
+
== INSTALL:
|
24
|
+
|
25
|
+
* sudo gem install euler
|
26
|
+
|
27
|
+
== LICENSE:
|
28
|
+
|
29
|
+
(BSD License)
|
30
|
+
|
1
31
|
Copyright (c) 2008, Mike "Oompa" Skalnik ("THE AUTHOR")
|
2
32
|
All rights reserved. mike.skalnik@gmail.com
|
3
33
|
|
@@ -22,3 +52,4 @@ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
22
52
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
23
53
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
24
54
|
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
55
|
+
|
data/lib/euler.rb
CHANGED
@@ -1,13 +1,6 @@
|
|
1
|
-
|
2
|
-
libeuler is a simple library you can simply +require+ in your code.
|
3
|
-
It's goal is to help reduce the repetativeness of some Project
|
4
|
-
Euler problems.
|
5
|
-
|
6
|
-
Author:: Mike Skalnik(mailto:mike.skalnik@gmail.com)
|
7
|
-
Copyright:: 2008, Mike Skalnik; All Rights Reserved
|
8
|
-
License:: See LICENSE file
|
9
|
-
=end
|
1
|
+
# The module that contains all the methods.
|
10
2
|
module Euler
|
3
|
+
VERSION = "1.0.3"
|
11
4
|
extend self
|
12
5
|
attr_reader :primes, :sieve, :fibonaccis
|
13
6
|
# Several methods for the Integer class.
|
@@ -25,10 +18,10 @@ module Euler
|
|
25
18
|
def prime_factors
|
26
19
|
return [self] if self.prime?
|
27
20
|
current, to_factor, factors, max = 2, self, [], 0
|
28
|
-
# current is the value currently being tested
|
29
|
-
#
|
30
|
-
# factors is the
|
31
|
-
#
|
21
|
+
# current is the value currently being tested to_factor is the value
|
22
|
+
# being factored (slowly moves to a prime value) factors is the array of
|
23
|
+
# prime factors max is the maximum value of any factor set after the
|
24
|
+
# next loop
|
32
25
|
while to_factor % current == 0
|
33
26
|
factors << 2
|
34
27
|
to_factor /= current
|
@@ -47,8 +40,7 @@ module Euler
|
|
47
40
|
return factors
|
48
41
|
end
|
49
42
|
|
50
|
-
# Returns
|
51
|
-
# +self+
|
43
|
+
# Returns a boolean which gives the primality of +self+
|
52
44
|
# 2.prime? # => true
|
53
45
|
# 4.prime? # => false
|
54
46
|
def prime?
|
@@ -65,9 +57,8 @@ module Euler
|
|
65
57
|
return true
|
66
58
|
end
|
67
59
|
|
68
|
-
# Compares +self+ and +x+ and returns +true+
|
69
|
-
#
|
70
|
-
# otherwise.
|
60
|
+
# Compares +self+ and +x+ and returns +true+ if the values are
|
61
|
+
# permutations, and +false+ otherwise.
|
71
62
|
# 123.is_permutation?(312) # => true
|
72
63
|
# 312.is_permutation?(281) # => false
|
73
64
|
def is_permutation?(x)
|
@@ -88,8 +79,7 @@ module Euler
|
|
88
79
|
return s_total == x_total
|
89
80
|
end
|
90
81
|
|
91
|
-
# Checks to see if +self+ is a pandigital with the range
|
92
|
-
# of x
|
82
|
+
# Checks to see if +self+ is a pandigital with the range of x
|
93
83
|
# 0192837465.is_pandigital?(0..9) # => true
|
94
84
|
# 0192837465.is_pandigital?(1..9) # => false
|
95
85
|
def is_pandigital?(x)
|
@@ -100,8 +90,7 @@ module Euler
|
|
100
90
|
return true
|
101
91
|
end
|
102
92
|
|
103
|
-
# Checks to see if +self+ is contained in the fibonacci
|
104
|
-
# sequence.
|
93
|
+
# Checks to see if +self+ is contained in the fibonacci sequence.
|
105
94
|
# 1.is_fibonacci? # => true
|
106
95
|
# 4.is_fibonacci? # => true
|
107
96
|
def is_fibonacci?
|
@@ -123,9 +112,8 @@ module Euler
|
|
123
112
|
end
|
124
113
|
end
|
125
114
|
|
126
|
-
# A basic prime sieve. Sets Euler.sieve to an array of
|
127
|
-
#
|
128
|
-
# or not.
|
115
|
+
# A basic prime sieve. Sets Euler.sieve to an array of boolean values that
|
116
|
+
# indicate if that index if prime or not.
|
129
117
|
# Euler.generate_sieve(50)
|
130
118
|
# Euler::sieve[2] # => true
|
131
119
|
# Euler::sieve[4] # => false
|
@@ -140,8 +128,7 @@ module Euler
|
|
140
128
|
}
|
141
129
|
end
|
142
130
|
|
143
|
-
# A more advanced prime sieve. Generates an n amount of
|
144
|
-
# prime values.
|
131
|
+
# A more advanced prime sieve. Generates an n amount of prime values.
|
145
132
|
# Euler.get_primes(50)
|
146
133
|
# Euler::primes[0] # => 2
|
147
134
|
# Euler::primes[2] # => 5
|
@@ -176,8 +163,8 @@ module Euler
|
|
176
163
|
a**2 + b**2 == c**2
|
177
164
|
end
|
178
165
|
|
179
|
-
# Given two values of a pythagorean triplet, and nil for the missing
|
180
|
-
#
|
166
|
+
# Given two values of a pythagorean triplet, and nil for the missing value,
|
167
|
+
# it returns the missing value.
|
181
168
|
# Euler.find_missing_pyth_value(nil, 4, 5) # => 3
|
182
169
|
def find_missing_pyth_value(a, b, c)
|
183
170
|
return Math.sqrt(c**2 - b**2) if a.nil?
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
require "euler"
|
4
|
+
|
5
|
+
class TestIntegerMethods < Test::Unit::TestCase
|
6
|
+
def test_factorial
|
7
|
+
assert_equal(0, 0.factorial, "0! == 0")
|
8
|
+
assert_equal(1, 1.factorial, "1! == 1")
|
9
|
+
assert_equal(120, 5.factorial, "5! == 120")
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_prime_factors
|
13
|
+
assert_equal([2, 5], 10.prime_factors, "10 factors into 2 and 5.")
|
14
|
+
assert_equal([2, 2, 5], 20.prime_factors, "20 factors into 2, 2, and 5.")
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_primality
|
18
|
+
assert(!1.prime?, "1 is not a prime.")
|
19
|
+
assert(2.prime?, "2 is the first prime.")
|
20
|
+
assert(3.prime?, "3 is the second prime.")
|
21
|
+
assert(29.prime?, "29 is the tenth prime.")
|
22
|
+
assert(!77.prime?, "77 is not a prime.")
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_permutations
|
26
|
+
assert(132.is_permutation?(123), "132 is a permutation of 123.")
|
27
|
+
assert(!3344.is_permutation?(3444), "3344 is not a permutation of 3444.")
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_pandigitial
|
31
|
+
assert(1234567890.is_pandigital?(0..9), "124567890 is a 0 to 9 pandigital.")
|
32
|
+
assert(!123456789.is_pandigital?(0..9), "12346789 is not a 0 to 9 pandigital.");
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_fibonacci
|
36
|
+
assert(1.is_fibonacci?, "1 is the first & second fibonacci numbers")
|
37
|
+
assert(8.is_fibonacci?, "8 is the sixth fibonacci number")
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_length
|
41
|
+
assert_equal(2, 10.length, "10 has a length of 2")
|
42
|
+
assert_equal(4, 4563.length, "4563 has a length of 4")
|
43
|
+
end
|
44
|
+
end
|
data/test/tc_module.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
|
3
|
+
require "euler"
|
4
|
+
|
5
|
+
class TestModule < Test::Unit::TestCase
|
6
|
+
def test_sieve
|
7
|
+
Euler.generate_sieve(50)
|
8
|
+
assert_equal(50, Euler::sieve.length, "Euler.sieve(50) should make Euler::sieve an array of length 50.")
|
9
|
+
assert(!Euler::sieve[0], "O is not prime.")
|
10
|
+
assert(!Euler::sieve[1], "1 is not prime.")
|
11
|
+
assert(Euler::sieve[2], "2 is the first prime.")
|
12
|
+
assert(Euler::sieve[3], "3 is the second prime.")
|
13
|
+
assert(Euler::sieve[29], "29 is the tenth prime.")
|
14
|
+
assert(!Euler::sieve[50], "50 is not prime.")
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_get_primes
|
18
|
+
Euler.get_primes(50)
|
19
|
+
assert_equal(50, Euler::primes.length, "Euler.get_primes(50) should make Euler::primes an array of length 50")
|
20
|
+
assert_equal(2, Euler::primes[0], "2 is the first prime.")
|
21
|
+
assert_equal(29, Euler::primes[9], "29 is the tenth prime.")
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_get_fibonaccis
|
25
|
+
Euler.get_fibonaccis(50)
|
26
|
+
assert_equal(2, Euler::fibonaccis[2], "2 is the third fibonaccis number.")
|
27
|
+
assert_equal(39088169, Euler::fibonaccis[37], "39088169 is the thirty eighth fibonacci number.")
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_pythagorean_triplet
|
31
|
+
assert(Euler.is_pythagorean_triplet?(3, 4, 5), "3, 4, 5 is the smallest pythagorean triplet.")
|
32
|
+
assert(!Euler.is_pythagorean_triplet?(1, 2, 3), "1, 2, 3 is not a pythagorean triplet.")
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_find_missing_pyth_value
|
36
|
+
assert_equal(3, Euler.find_missing_pyth_value(nil, 4, 5))
|
37
|
+
assert_equal(4, Euler.find_missing_pyth_value(3, nil, 5))
|
38
|
+
assert_equal(5, Euler.find_missing_pyth_value(3, 4, nil))
|
39
|
+
end
|
40
|
+
end
|
data/test/test_euler.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: Oompa-euler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Skalnik
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2008-08-
|
12
|
+
date: 2008-08-26 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -20,14 +20,15 @@ executables: []
|
|
20
20
|
extensions: []
|
21
21
|
|
22
22
|
extra_rdoc_files:
|
23
|
-
- README.
|
24
|
-
-
|
23
|
+
- README.txt
|
24
|
+
- History.txt
|
25
25
|
files:
|
26
|
-
-
|
27
|
-
-
|
26
|
+
- README.txt
|
27
|
+
- History.txt
|
28
|
+
- Manifest.txt
|
28
29
|
- lib/euler.rb
|
29
30
|
has_rdoc: true
|
30
|
-
homepage: http://
|
31
|
+
homepage: http://www.github.com/Oompa/euler
|
31
32
|
post_install_message:
|
32
33
|
rdoc_options: []
|
33
34
|
|
@@ -47,10 +48,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
48
|
version:
|
48
49
|
requirements: []
|
49
50
|
|
50
|
-
rubyforge_project:
|
51
|
+
rubyforge_project: euler
|
51
52
|
rubygems_version: 1.2.0
|
52
53
|
signing_key:
|
53
54
|
specification_version: 2
|
54
|
-
summary: A
|
55
|
-
test_files:
|
56
|
-
|
55
|
+
summary: A small library to help solve Projet Euler problems.
|
56
|
+
test_files:
|
57
|
+
- test/tc_integer_methods.rb
|
58
|
+
- test/tc_module.rb
|
59
|
+
- test/test_euler.rb
|