Module: Euler
Overview
The module that contains all the methods.
Defined Under Namespace
Modules: IntegerMethods
Constant Summary collapse
- VERSION =
"1.0.3"
Instance Attribute Summary collapse
-
#fibonaccis ⇒ Object
readonly
Returns the value of attribute fibonaccis.
-
#primes ⇒ Object
readonly
Returns the value of attribute primes.
-
#sieve ⇒ Object
readonly
Returns the value of attribute sieve.
Instance Method Summary collapse
-
#find_missing_pyth_value(a, b, c) ⇒ Object
Given two values of a pythagorean triplet, and nil for the missing value, it returns the missing value.
-
#generate_sieve(max) ⇒ Object
A basic prime sieve.
-
#get_fibonaccis(n) ⇒ Object
Sets Euler::fibonaccis to an array of integers which are the Fibonacci sequence of length
n
Euler.get_fibonaccis(50) Euler::fibonaccis # => 1 Euler::fibonaccis # => 5. -
#get_primes(n) ⇒ Object
A more advanced prime sieve.
-
#is_pythagorean_triplet?(a, b, c) ⇒ Boolean
Determines if the three values given are a pythagorean triplet Euler.is_pythagorean_triplet?(3, 4, 5) # => true Euler.is_pythagorean_triplet?(1, 2, 3) # => false.
Instance Attribute Details
#fibonaccis ⇒ Object (readonly)
Returns the value of attribute fibonaccis.
5 6 7 |
# File 'lib/euler.rb', line 5 def fibonaccis @fibonaccis end |
#primes ⇒ Object (readonly)
Returns the value of attribute primes.
5 6 7 |
# File 'lib/euler.rb', line 5 def primes @primes end |
#sieve ⇒ Object (readonly)
Returns the value of attribute sieve.
5 6 7 |
# File 'lib/euler.rb', line 5 def sieve @sieve end |
Instance Method Details
#find_missing_pyth_value(a, b, c) ⇒ Object
Given two values of a pythagorean triplet, and nil for the missing value, it returns the missing value.
Euler.find_missing_pyth_value(nil, 4, 5) # => 3
169 170 171 172 173 |
# File 'lib/euler.rb', line 169 def find_missing_pyth_value(a, b, c) return Math.sqrt(c**2 - b**2) if a.nil? return Math.sqrt(c**2 - a**2) if b.nil? return Math.sqrt(a**2 + b**2) if c.nil? end |
#generate_sieve(max) ⇒ Object
120 121 122 123 124 125 126 127 128 129 |
# File 'lib/euler.rb', line 120 def generate_sieve(max) @sieve = Array.new(max, false) @sieve[2] = true 3.step(max, 2) { |x| @sieve[x] = true} 3.step(Math.sqrt(max).to_i, 2) { |i| if @sieve[i] (i**2).step(max, 2*i) { |x| @sieve[x] = false} end } end |
#get_fibonaccis(n) ⇒ Object
Sets Euler::fibonaccis to an array of integers which are the Fibonacci sequence of length n
Euler.get_fibonaccis(50)
Euler::fibonaccis[0] # => 1
Euler::fibonaccis[4] # => 5
151 152 153 154 155 156 157 |
# File 'lib/euler.rb', line 151 def get_fibonaccis(n) @fibonaccis, phi = [], (1+Math.sqrt(5))/2 sqrt_5 = Math.sqrt(5) 0.upto(n) do |x| @fibonaccis << (phi**(x+1)/sqrt_5).round end end |
#get_primes(n) ⇒ Object
135 136 137 138 139 140 141 142 143 144 |
# File 'lib/euler.rb', line 135 def get_primes(n) counter, current_value, @primes = 0, 2, [] while counter < n if current_value.prime? @primes << current_value counter += 1 end current_value += 1 end end |
#is_pythagorean_triplet?(a, b, c) ⇒ Boolean
162 163 164 |
# File 'lib/euler.rb', line 162 def is_pythagorean_triplet?(a, b, c) a**2 + b**2 == c**2 end |