Skip to content
Rugen Heidbuchel edited this page Jun 20, 2015 · 8 revisions

The functions file defines basic math functions. Most of the functions are completely generic, the non-generic functions will be converted in the future.

Basic Functions

sign(x: X) -> Int

Returns an Int representing the sign of the given number, this is -1, 0 or +1.

protocols to adopt:

  • Equatable
  • Comparable
  • IntegerLiteralConvertible

factorial(x: X) -> X

Returns the factorial of the given number, this is x! .

protocols to adopt:

factorialsUpTo(x: X) -> [X]

Returns the factorial values up to (and including) the x'th value. The index corresponds to the parameter for factorial, so factorialsUpTo(10)[5] == factorial(5).

factorialsUpTo(5) == [1, 1, 2, 6, 24, 120]

protocols to adopt:

fib(x: X) -> X

Returns the x'th Fibonacci number F(x) where F(0) = 0 and F(1) = 1.

protocols to adopt:

digits(number: Int) -> [Int]

Returns the digits of the given number, starting with the most significant. This function will be made generic as soon as possible

sum(seq: SequenceType) -> Element

Returns the sum of all elements in the given sequence.

The element type of the sequence must adopt the following protocols:

  • Addable
  • IntegerLiteralConvertible

product(seq: SequenceType) -> Element

Returns the product of all elements in the given sequence.

The element type of the sequence must adopt the following protocols:

Divisor Functions

divisors(x: X) -> [X]

Returns a list of all divisors of the given number, this includes x and 1. The list will be sorted ascending.

protocols to adopt:

  • Equatable
  • Comparable
  • Modulable
  • Dividable
  • RealPowerable, RealPowerType has to adopt Comparable, Addable and IntegerLiteralConvertible
  • IntegerLiteralConvertible

properDivisors(x: X) -> [X]

Returns a list of all proper divisors of the given number, this does not include x, but it does include 1. The list will be sorted ascending.

protocols to adopt:

  • Equatable
  • Comparable
  • Modulable
  • Dividable
  • RealPowerable, RealPowerType has to adopt Comparable, Addable and IntegerLiteralConvertible
  • IntegerLiteralConvertible

isPerfect(x: X) -> Bool

Returns whether the given number is perfect. This means the sum of its proper divisors equals the number itself. 6 is an example.

protocols to adopt:

isAbundant(x: X) -> Bool

Returns whether the given number is abundant. This means the sum of it's proper divisors is bigger than the number itself.

protocols to adopt:

abundantsUpTo(x: X) -> Set

Returns a set containing the abundance up to (and maybe including) the given value.

protocols to adopt:

isDeficient(x: X) -> Bool

Returns whether the given number is deficient. This means the sum of it's proper divisors is smaller than the number itself.

protocols to adopt:

gcd(a: X, b: X) -> X

Returns the greatest common divisor of the two given numbers.

protocols to adopt:

  • Equatable
  • Modulable
  • IntegerLiteralConvertible

lcm(a: X, b: X) -> X

Returns the greatest common divisor of the two given numbers.

protocols to adopt:

Clone this wiki locally