# 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

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

• The number of size k unordered subsets of a set of size n.

• Calculates a series of Fibonacci numbers of a specified length.

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

• The number of size k ordered subsets of a set of size n.

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

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

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

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 not be modified unless this is being cached carefully)

Returns:

• (Integer)

the number of steps it takes to get from integer n to 1 using the Collatz conjecture (the depth)

Raises:

• (ArgumentError)

if n < 1

 ``` 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)!).

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

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

Parameters:

• length (Integer)

the length of the Fibonacci series that should be returned

Returns:

• (Array)

a Fibonacci series of Integers with the specified length (ordered)

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.

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

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)!.

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

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.

Parameters:

• limit (Integer)

the upper limit of all primes to find (inclusive)

Returns:

• (Array)

an array of integers containing all discovered primes (in increasing order)

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

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

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.

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

 ``` 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```