Sha256: e46aba515b3012ea78798cd1c25e15453b299c5f0a939742cb84e7e113bc69c0

Contents?: true

Size: 792 Bytes

Versions: 3

Compression:

Stored size: 792 Bytes

Contents

# armstrong_numbers.fy
# Calculates & outputs all Armstrong Numbers between 0 and 10000.
# See http://en.wikipedia.org/wiki/Narcissistic_number for more
# information.

class Fixnum {
  def decimals {
    """
    Returns all decimals of a Number as an Array.
    E.g. 123 decimals # => [1,2,3]
    """

    decimals = []
    tmp = self
    while: { tmp >= 10 } do: {
      decimals << (tmp modulo: 10)
      tmp = tmp div: 10
    }
    decimals << tmp
    decimals
  }

  def armstrong? {
    """
    Indicates, if a Number is a Armstrong Number.
    """

    decimals = self decimals
    n_decimals = decimals size
    decimals map: |x| { x ** n_decimals } . sum == self
  }
}

# output alls Armstrong Numbers between 0 and 10000
0 upto: 10000 do: |i| {
  { i println } if: $ i armstrong?
}

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
fancy-0.10.0 examples/armstrong_numbers.fy
fancy-0.9.0 examples/armstrong_numbers.fy
fancy-0.8.0 examples/armstrong_numbers.fy