Module: Math
Overview
Magician’s extensions to the Math module.
Instance Method Summary collapse
-
#collatz(n, depth = 0) ⇒ Integer
Get the number of steps it takes to get from integer n to 1 using the Collatz conjecture (set en.wikipedia.org/wiki/Collatz_conjecture).
-
#combinations(n, k) ⇒ Integer
The number of size k unordered subsets of a set of size n.
-
#fibs(length) ⇒ Array
Calculates a series of Fibonacci numbers of a specified length.
-
#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.
-
#permutations(n, k) ⇒ Integer
The number of size k ordered subsets of a set of size n.
-
#quadratic(a, b, c) ⇒ Array
Solves a quadratic formula of the form “ax^2+bx+c=0” for x, where a is not 0.
-
#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).
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 (set en.wikipedia.org/wiki/Collatz_conjecture). Returns nil if n < 1.
not be modified unless this is being cached carefully)
using the Collatz conjecture (the depth)
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/magician/math.rb', line 58 def collatz(n, depth=0) return nil if n < 1 if n == 1 depth elsif n % 2 == 0 depth += 1 collatz(n/2, depth) else depth += 1 collatz(3*n + 1, depth) 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)!). Returns nil if either is negative, or if n < k.
43 44 45 46 |
# File 'lib/magician/math.rb', line 43 def combinations(n, k) return nil if n < 0 or k < 0 or n < k n.factorial / (k.factorial * (n-k).factorial) end |
#fibs(length) ⇒ Array
Calculates a series of Fibonacci numbers of a specified length. Returns nil if a negative length is given.
returned
(ordered)
111 112 113 114 115 116 117 118 119 |
# File 'lib/magician/math.rb', line 111 def fibs length return nil if length < 0 terms = [] until terms.length == length do at_beginning = [0,1].include? terms.length terms << ( at_beginning ? 1 : terms[-2] + terms[-1] ) end terms 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. Returns nil if either a or b is negative.
79 80 81 82 83 84 |
# File 'lib/magician/math.rb', line 79 def hypotenuse(a, b) [a,b].each do |n| return nil if n < 0 end 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)!. Returns nil if either is negative, or if n < k.
31 32 33 34 |
# File 'lib/magician/math.rb', line 31 def permutations(n, k) return nil if n < 0 or k < 0 or n < k n.factorial / (n-k).factorial end |
#quadratic(a, b, c) ⇒ Array
Solves a quadratic formula of the form “ax^2+bx+c=0” for x, where a is not
-
It asks for the three coefficients of the function (a, b, and c), and
returns the two possible values for x. Returns nil if a is 0.
16 17 18 19 20 21 22 |
# File 'lib/magician/math.rb', line 16 def quadratic(a, b, c) return nil 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.
94 95 96 97 98 99 100 101 |
# File 'lib/magician/math.rb', line 94 def triplet?(a, b, c) inputs_are_valid = true [a,b,c].each do |n| inputs_are_valid = false if n < 1 or not n.class <= Integer end return false unless inputs_are_valid a**2 + b**2 == c**2 end |