Module: Euler

Extended by:
Euler
Included in:
Euler
Defined in:
lib/euler.rb

Overview

The module that contains all the methods.

Defined Under Namespace

Modules: IntegerMethods

Constant Summary collapse

VERSION =
"1.0.7"

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fibonaccisObject (readonly)

Returns the value of attribute fibonaccis.



5
6
7
# File 'lib/euler.rb', line 5

def fibonaccis
  @fibonaccis
end

#primesObject (readonly)

Returns the value of attribute primes.



5
6
7
# File 'lib/euler.rb', line 5

def primes
  @primes
end

#sieveObject (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

A basic prime sieve. Sets Euler.sieve to an array of boolean values that indicate if that index if prime or not.

Euler.generate_sieve(50)
Euler::sieve[2] # => true
Euler::sieve[4] # => false


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

A more advanced prime sieve. Generates an n amount of prime values.

Euler.get_primes(50)
Euler::primes[0] # => 2
Euler::primes[2] # => 5


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

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

Returns:

  • (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