Module: Math

Defined in:
lib/roebe/core/math.rb

Overview

#

Class Method Summary collapse

Class Method Details

.inverse_log(i) ⇒ Object

#

Math.inverse_log

#


46
47
48
# File 'lib/roebe/core/math.rb', line 46

def self.inverse_log(i)
  Math.exp(i)
end

.ln(i) ⇒ Object

#

Math.ln

This should be the natural logarithm (ln).

#


39
40
41
# File 'lib/roebe/core/math.rb', line 39

def self.ln(i)
  return Math.log(i, Math::E)
end

.pythagorean_triplets?(array = [8,15,17]) ⇒ Boolean

#

Math.pythagorean_triplets?

We know that the hypotenuse must be the longest side.

An example for a “pythagorean triplet” is [8,15,17].

#

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/roebe/core/math.rb', line 14

def self.pythagorean_triplets?(
    array = [8,15,17]
  )
  min = array.min
  max = array.max
  # ======================================================================= #
  # The opposite only has to search from 1 until the current hypotenuse,
  # and the adjacent only has to search from 1 to the current opposite.
  # ======================================================================= #
  (min .. max).flat_map { |hypotenuse|
    (1 .. hypotenuse).flat_map { |opposite|
      (1 .. opposite).select { |adjacent|
        adjacent*adjacent + opposite*opposite == hypotenuse*hypotenuse
      }.map { |selected_adjacent|
        [selected_adjacent,opposite,hypotenuse]
      }
    }
  }
end