Module: Math

Extended by:
Math
Included in:
Math
Defined in:
lib/magician/math.rb

Overview

Magician's extensions to the Math module.

Instance Method Summary collapse

Instance Method Details

#collatz(n, depth = 0) ⇒ Integer

Get the number of steps it takes to get from integer n to 1 using the Collatz conjecture.

Raises:

  • (ArgumentError)

    if n < 1

See Also:


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/magician/math.rb', line 76

def collatz(n, depth=0)
  raise ArgumentError, 'n must be at least 1' if n < 1

  if n == 1
    depth
  elsif n.divisible? 2
    collatz(n/2, depth + 1)
  else
    collatz(3*n + 1, depth + 1)
  end
end

#combinations(n, k) ⇒ Integer

The number of size k unordered subsets of a set of size n. Equivalent to n!/(k!(n-k)!).

Raises:

  • (ArgumentError)

    if either argument is negative, or if n < k


55
56
57
58
59
60
61
# File 'lib/magician/math.rb', line 55

def combinations(n, k)
  raise ArgumentError, 'n cannot be negative' if n < 0
  raise ArgumentError, 'k cannot be negative' if k < 0
  raise ArgumentError, 'n must be at least as large as k' if n < k

  n.factorial / (k.factorial * (n-k).factorial)
end

#fibs(length, terms = [1, 1]) ⇒ Array

Calculates a series of Fibonacci numbers of a specified length. Note that if terms are not passed to this method, it will start generating numbers with the terms [1, 1].

Raises:

  • (ArgumentError)

    if a negative length is given, or if less than two terms are given


130
131
132
133
134
135
136
137
# File 'lib/magician/math.rb', line 130

def fibs length, terms = [1, 1]
  raise ArgumentError, 'Length must be at least 0' if length < 0
  raise ArgumentError, 'At least two terms must be given' if terms.length < 2

  terms << (terms[-2] + terms[-1]) while terms.length < length

  terms.first length
end

#hypotenuse(a, b) ⇒ Float

Using the Pythagorean theorem, gets c (the length of the hypotenuse) when a and b (the lengths of the other sides of a triangle) are given.

Raises:

  • (ArgumentError)

    if either argument is negative


97
98
99
100
101
102
# File 'lib/magician/math.rb', line 97

def hypotenuse(a, b)
  raise ArgumentError, 'a cannot be negative' if a < 0
  raise ArgumentError, 'b cannot be negative' if b < 0

  Math.sqrt(a**2 + b**2)
end

#permutations(n, k) ⇒ Integer

The number of size k ordered subsets of a set of size n. Equivalent to n!/(n-k)!.

Raises:

  • (ArgumentError)

    if either argument is negative, or if n < k


38
39
40
41
42
43
44
# File 'lib/magician/math.rb', line 38

def permutations(n, k)
  raise ArgumentError, 'n cannot be negative' if n < 0
  raise ArgumentError, 'k cannot be negative' if k < 0
  raise ArgumentError, 'n must be at least as large as k' if n < k

  n.factorial / (n-k).factorial
end

#primes(limit) ⇒ Array

Finds all prime numbers from 1 to a given number n (inclusive) using the Sieve of Eratosthenes.


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/magician/math.rb', line 148

def primes limit
  # Initialize the array of booleans
  is_prime = [true] * (limit+1)
  is_prime[0] = false
  is_prime[1] = false

  # Check for composite numbers and update the array with results
  2.upto(Math.sqrt limit).each do |i|
    if is_prime[i]
      # Mark all multiples of i as composite
      2.upto(limit).each do |factor|
        multiple = i * factor
        break if multiple > limit
        is_prime[multiple] = false
      end
    end
  end

  # Create an array of prime integers
  1.upto(limit).find_all { |i| is_prime[i] }
end

#quadratic(a, b, c) ⇒ Array

Solves a quadratic formula of the form “ax^2+bx+c=0” for x, where a is not

  1. It asks for the three coefficients of the function (a, b, and c), and

returns the two possible values for x. Complex number results are not supported yet.

Raises:

  • (ArgumentError)

    if a is 0


19
20
21
22
23
24
25
26
27
# File 'lib/magician/math.rb', line 19

def quadratic(a, b, c)
  raise ArgumentError, 'a cannot be zero' if a.zero?

  left = -b
  right = Math.sqrt(b**2 - 4*a*c)
  bottom = 2*a

  [ (left+right)/bottom, (left-right)/bottom ].sort
end

#triplet?(a, b, c) ⇒ Boolean

Returns true if the three given numbers are positive integers that form a Pythagorean triplet (that is, if a^2+b^2=c^2). C must be the last parameter.


112
113
114
115
116
# File 'lib/magician/math.rb', line 112

def triplet?(a, b, c)
  return false if [a, b, c].any? { |n| n < 1 or not n.is_a? Integer }

  a**2 + b**2 == c**2
end