Skip to content
Rugen Heidbuchel edited this page Apr 30, 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.

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

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
  • Powerable, PowerType 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
  • Powerable, PowerType 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:

  • Equatable
  • Comparable
  • Addable
  • Modulable
  • Dividable
  • Powerable, PowerType has to adopt Comparable, Addable and IntegerLiteralConvertible
  • IntegerLiteralConvertible

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

Returns the greatest common divisor of the two given numbers.

protocols to adopt:

  • Equatable
  • Modulable
  • IntegerLiteralConvertible

factorial(x: X) -> X

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

protocols to adopt:

  • Comparable
  • Substractable
  • Multiplicable
  • SetCompliant
  • IntegerLiteralConvertible

fib(x: X) -> X

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

protocols to adopt:

  • Hashable
  • Addable
  • Substractable
  • IntegerLiteralConvertible

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:

  • Multiplicable
  • IntegerLiteralConvertible
Clone this wiki locally