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

Parameters:

  • n (Integer)

    the number to put into the Collatz conjecture initially

  • depth (Integer) (defaults to: 0)

    the number of steps that have passed so far (should

Returns:

  • (Integer)

    the number of steps it takes to get from integer n to 1



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.

Parameters:

  • n (Integer)

    the size of the set to pick from

  • k (Integer)

    the size of the unordered subsets

Returns:

  • (Integer)

    the number of combinations



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)

Parameters:

  • length (Integer)

    the length of the Fibonacci series that should be

Returns:

  • (Array)

    a Fibonacci series of Integers with the specified length



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.

Parameters:

  • a (Numeric)

    the length of the first side of the triangle

  • b (Numeric)

    the length of the second side of the triangle

Returns:

  • (Float)

    the length of the hypotenuse of the triangle



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.

Parameters:

  • n (Integer)

    the size of the set to pick from

  • k (Integer)

    the size of the ordered subsets

Returns:

  • (Integer)

    the number of permutations



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

  1. 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.

Parameters:

  • a (Numeric)

    the first coefficient (must not be 0)

  • b (Numeric)

    the second coefficient

  • c (Numeric)

    the third coefficient

Returns:

  • (Array)

    a sorted array of two Floats, the two possible values for x



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.

Parameters:

  • a (Integer)

    the length of the first side of the triangle

  • b (Integer)

    the length of the second side of the triangle

  • c (Integer)

    the length of the hypotenuse of the triangle

Returns:

  • (Boolean)

    true if the three numbers form a Pythagorean triplet



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