Module: MissingMath

Included in:
Integer
Defined in:
lib/missing_math.rb,
lib/missing_math/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Instance Method Details

#factorialObject



20
21
22
23
# File 'lib/missing_math.rb', line 20

def factorial
  throw "Not an Integer" if !self.is_i?
  self.downto(1).reduce(:*)
end

#factors(include_one = true) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/missing_math.rb', line 25

def factors(include_one=true)
  throw "Not an Integer" if !self.is_i?
  last = self
  i = include_one ? 1 : 2
  a = []
  while i < last
    if self % i == 0
      last = self / i
      a << i
      a << last
    end
    i += 1
  end
  return a.sort
end

#prime?Boolean

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/missing_math.rb', line 3

def prime?
  throw "Not an Integer" if !self.is_i?
  begin
    last = Math.sqrt(self).floor
    i = 2
    while i <= last
      if self % i == 0
        return false
      end
      i += 1
    end
    return true
  rescue
    false
  end
end