Class: MathMagic

Inherits:
Object
  • Object
show all
Defined in:
lib/math_magic.rb

Overview

Description: A class that contains methods for performing mathematical operations.

Class Method Summary collapse

Class Method Details

.factors(num) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/math_magic.rb', line 28

def self.factors(num)
  factors = []
  temp = 2
  while (temp * temp) <= num
    if (num % temp).zero?
      factors << temp
      factors << num / temp
    end
    temp += 1
  end

  factors.sort
end

.gcd(num1, num2) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/math_magic.rb', line 6

def self.gcd(num1, num2)
  if num2.zero?
    num1
  else
    gcd(num2, num1 % num2)
  end
end

.prime?(num) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/math_magic.rb', line 14

def self.prime?(num)
  if num < 2
    false
  else
    temp = 2
    while (temp * temp) <= num
      return false if (num % temp).zero?

      temp += 1
    end
    true
  end
end