Class: Fixnum

Inherits:
Object
  • Object
show all
Defined in:
lib/hexa/core_ext/fixnum.rb

Instance Method Summary collapse

Instance Method Details

#factorialFixnum Also known as: !

Allows tou to know the factorial of self

Returns:

  • factorial of self



5
6
7
8
9
10
11
# File 'lib/hexa/core_ext/fixnum.rb', line 5

def factorial
  if self == 1 or self == 0
    return 1
  else
    return self * (self - 1).factorial
  end
end

#inverseFloat Also known as: reverse

Returns inverse of self.

Returns:

  • inverse of self



45
46
47
48
49
50
51
# File 'lib/hexa/core_ext/fixnum.rb', line 45

def inverse
  if self == 0
    return 0
  else
    return 1 / self
  end
end

#oppositeFixnum

Returns the opposite of self.

Returns:

  • the opposite of self



40
41
42
# File 'lib/hexa/core_ext/fixnum.rb', line 40

def opposite
  - self
end

#prime?Boolean

Predicate method which allows you to know if the number is a prime number

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hexa/core_ext/fixnum.rb', line 22

def prime?
  if self < 2
    false
  elsif self == 2
    true
  elsif self % 2 == 0
    false
  else
    i = 3
    while i*i <= self
      return false if self % i == 0
      i += 2
    end
    return true
  end
end

#sqrtFloat

Returns the square root of self.

Returns:

  • the square root of self



16
17
18
# File 'lib/hexa/core_ext/fixnum.rb', line 16

def sqrt
  Math.sqrt(self)
end