Module: Euler
Overview
The module that contains all the methods.
Defined Under Namespace
Modules: IntegerMethods
Constant Summary collapse
- VERSION =
"1.0.7"
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
nEuler.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.
-
#solve_sudoku(puzzle) ⇒ Object
Solves a Sudoku puzzle passed in as a 2D array.
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
171 172 173 174 175 |
# File 'lib/euler.rb', line 171 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
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/euler.rb', line 122 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
153 154 155 156 157 158 159 |
# File 'lib/euler.rb', line 153 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
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/euler.rb', line 137 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
164 165 166 |
# File 'lib/euler.rb', line 164 def is_pythagorean_triplet?(a, b, c) a**2 + b**2 == c**2 end |
#solve_sudoku(puzzle) ⇒ Object
Solves a Sudoku puzzle passed in as a 2D array.
Euler.solve_sudoku(
[[0, 0, 3, 0, 2, 0, 6, 0, 0],
[9, 0, 0, 3, 0, 5, 0, 0, 1],
[0, 0, 1, 8, 0, 6, 4, 0, 0],
[0, 0, 8, 1, 0, 2, 9, 0, 0],
[7, 0, 0, 0, 0, 0, 0, 0, 8],
[0, 0, 6, 7, 0, 8, 2, 0, 0],
[0, 0, 2, 6, 0, 9, 5, 0, 0],
[8, 0, 0, 2, 0, 3, 0, 0, 9],
[0, 0, 5, 0, 1, 0, 3, 0, 0]]
) # => [[4, 8, 3, 9, 2, 1, 6, 5, 7], [9, 6, 7, 3, 4, 5, 8, 2, 1], [2, 5, 1, 8, 7, 6, 4, 9, 3], [5, 4, 8, 1, 3, 2, 9, 7, 6], [7, 2, 9, 5, 6, 4, 1, 3, 8], [1, 3, 6, 7, 9, 8, 2, 4, 5], [3, 7, 2, 6, 8, 9, 5, 1, 4], [8, 1, 4, 2, 5, 3, 7, 6, 9], [6, 9, 5, 4, 1, 7, 3, 8, 2]]
189 190 191 192 193 194 |
# File 'lib/euler.rb', line 189 def solve_sudoku(puzzle) require File.dirname(__FILE__) + "/rudoku" rudoku = Rudoku.new(puzzle) rudoku.backtrack_solve rudoku.to_a end |