class Integer

Public Class Methods

square?(n) click to toggle source

Integer.square? -> boolean

Returns whether the number is a square number

Integer.square?(1)          #=> true
Integer.square?(8)          #=> false
Integer.square?(36)         #=> true
# File lib/AnnieNumbers/IntegerMonkeyPatch.rb, line 74
def self.square?(n)
  n = n.to_i
  i = 1 # create starting number
  while true
    val = i*i  # create the square number of i
    if val == n
      return true
    end
    if val > n
      return false
    end
    i += 1
  end
end
triangular?(n) click to toggle source

Integer.triangular? -> boolean

Returns whether the number is triangular

Integer.triangular?(1)       #=> true
Integer.triangular?(2)       #=> false
Integer.triangular?(24)      #=> true
# File lib/AnnieNumbers/IntegerMonkeyPatch.rb, line 31
def self.triangular?(n)
  n = n.to_i
  i = 1 # create starting number
  while true
    val = (i*(i+1))/2  # create the triangular number to base of i
    if val == n
      return true
    end
    if val > n
      return false
    end
    i += 1
  end
end

Public Instance Methods

square?() click to toggle source

Integer.square? -> boolean

Returns whether the number is a square number

1.square?          #=> true
8.square?          #=> false
36.square?         #=> true
# File lib/AnnieNumbers/IntegerMonkeyPatch.rb, line 53
def square?
  i = 1 # create starting number
  while true
    val = i*i  # create the square number of i
    if val == self
      return true
    end
    if val > self
      return false
    end
    i += 1
  end
end
triangular?() click to toggle source

triangular? -> boolean

Returns whether the number is triangular

1.triangular?          #=> true
4.triangular?          #=> false
24.triangular?         #=> true
# File lib/AnnieNumbers/IntegerMonkeyPatch.rb, line 10
def triangular?
  i = 1 # create starting number
  while true
    val = (i*(i+1))/2  # create the triangular number to base of i
    if val == self
      return true
    end
    if val > self
      return false
    end
    i += 1
  end
end