Int

struct Int : SignedInteger, Comparable, Equatable
  • Returns the greatest common divisor (always positive). 0.gcd(x) and x.gcd(0) return abs(x).

    2.gcd(2)        #=> 2
    1.gcd(-7)       #=> 1
    0.gcd(2)        #=> 2
    0.gcd(-2)       #=> 2
    (-2).gcd(0)     #=> 2
    ((1<<31)-1).gcd((1<<61)-1)  #=> 1
    

    Declaration

    Swift

    func gcd(_ another: Int) -> Int

    Parameters

    another

    Another integer

    Return Value

    The greatest common divisor

  • Returns the least common multiple (always positive).

    2.lcm(2)        #=> 2
    3.lcm(-7)       #=> 21
    

    0.lcm(x) and x.lcm(0) return zero.

    0.lcm(10)       #=> 0
    10.lcm(0)       #=> 0
    

    Declaration

    Swift

    func lcm(_ another: Int) -> Int

    Parameters

    another

    Another integer

    Return Value

    The least common mutiple

  • Returns an tuple (Number#gcd(another:), Number#lcm(another:)).

    2.gcdlcm(2)         #=> (2, 2)
    3.gcdlcm(-7)        #=> (1, 21)
    

    Declaration

    Swift

    func gcdlcm(_ another: Int) -> (Int, Int)

    Parameters

    another

    Another integer

    Return Value

    A tuple contains the gcd and lcm

  • Returns the number of bits of the value of Integer. the number of bits means that the bit position of the highest bit which is different to the sign bit. (The bit position of the bit 2**n is n+1.) If there is no such bit (zero or minus one), zero is returned.

    (-(2 ** 12) - 1).bitLength      #=> 13
    (-(2 ** 12)).bitLength          #=> 12
    (-(2 ** 12) + 1).bitLength      #=> 12
    (-0x101).bitLength              #=> 9
    (-0x100).bitLength              #=> 8
    (-0xff).bitLength               #=> 8
    (-2).bitLength                  #=> 1
    (-1).bitLength                  #=> 0
    0.bitLength                     #=> 0
    1.bitLength                     #=> 1
    0xff.bitLength                  #=> 8
    0x100.bitLength                 #=> 9
    (2 ** 12 - 1).bitLength         #=> 12
    (2 ** 12).bitLength             #=> 13
    (2 ** 12 + 1).bitLength         #=> 13
    

    Declaration

    Swift

    var bitLength: Int
  • Returns the Integer equal to self + 1.

    1.next      #=> 2
    (-1).next       #=> 0
    

    Declaration

    Swift

    var next: Int
  • Returns the Integer equal to self + 1.

    1.succ      #=> 2
    (-1).succ       #=> 0
    

    Declaration

    Swift

    var succ: Int
  • Returns the Integer equal to self - 1.

    1.pred      #=> 0
    (-1).pred       #=> -2
    

    Declaration

    Swift

    var pred: Int
  • Returns the largest number less than or equal to int in decimal digits.

    1.ceil          #=> 1
    1.5.ceil        #=> 2
    

    Declaration

    Swift

    var ceil: Int
  • Returns the smallest number less than or equal to int in decimal digits.

    1.floor         #=> 1
    1.5.floor       #=> 1
    

    Declaration

    Swift

    var floor: Int
  • Rounds int to a given precision in decimal digits.

    1.round         #=> 1
    1.5.round       #=> 2
    1.4.round       #=> 1
    

    Declaration

    Swift

    var round: Int
  • Returns the array including the digits extracted by place-value notation with radix base of int.

    12345.digits            #=> [5, 4, 3, 2, 1]
    12345.digits(7)         #=> [4, 6, 6, 0, 5]
    12345.digits(100)       #=> [45, 23, 1]
    (-12345).digits(7)      #=> []
    

    Declaration

    Swift

    var digits: [Int]
  • Returns the array including the digits extracted by place-value notation with radix base of int.

    12345.digits            #=> [5, 4, 3, 2, 1]
    12345.digits(7)         #=> [4, 6, 6, 0, 5]
    12345.digits(100)       #=> [45, 23, 1]
    (-12345).digits(7)      #=> []
    

    Declaration

    Swift

    func digits(_ base: Int = 10) -> [Int]

    Parameters

    base

    A radix indicates the base of integer

    Return Value

    An array of digits

  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • Undocumented

    Declaration

    Swift

    struct Int : SignedInteger, Comparable, Equatable
  • chr

    Returns a string containing the character represented by the int’s value according to encoding.

    97.chr      #=> "a"
    98.chr      #=> "b"
    

    Declaration

    Swift

    var chr: String
  • Returns an array of int from self down to and including limit.

    3.downto(1)         #=> [3, 2, 1]
    3.downto(-2)        #=> [3, 2, 1, 0, -1, -2]
    3.downto(3)         #=> [3]
    3.downto(4)         #=> []
    
    3.downto(-2, step: 2)       #=> [3, 1, -1]
    

    Declaration

    Swift

    func downto(_ limit: Int, step: Int = 1) -> [Int]

    Parameters

    limit

    A integer specifies the last int to enumerate

    step

    A int used to step the array

    Return Value

    An array of int self…limit

  • Iterates the given block, passing decreasing values from int down to and including limit.

    var result: [Int] = []
    3.downto(1) { 
        result.append($0) 
    }
    result      #=> [3, 2, 1]
    

    Declaration

    Swift

    func downto(_ limit: Int, step: Int = 1, closure: (Int) throws -> Void) rethrows

    Parameters

    limit

    A integer specifies the last int to enumerate

    step

    A int used to step the array

    closure

    A closure accepts an integer

  • Returns an array of int from self up to and including limit.

    1.upto(3)       #=> [1, 2, 3]
    (-2).upto(3)    #=> [-2, -1, 0, 1, 2, 3]
    3.upto(3)       #=> [3]
    3.upto(4)       #=> [3, 4]
    (-2).upto(3, step: 2)       #=> [-2, 0, 2]
    

    Declaration

    Swift

    func upto(_ limit: Int, step: Int = 1) -> [Int]

    Parameters

    limit

    A integer specifies the last int to enumerate

    step

    A int used to step the array

    Return Value

    An array of int limit…self

  • Iterates the given block, passing increasing values from int up to and including limit.

    var result: [Int] = []
    1.upto(3) {
        result.append($0)
    }
    result      #=> [1, 2, 3]
    

    Declaration

    Swift

    func upto(_ limit: Int, step: Int = 1, closure: (Int) throws -> Void) rethrows

    Parameters

    limit

    A integer specifies the last int to enumerate

    step

    A int used to step the array

    closure

    A closure accepts an integer

  • Execute the block for self times

    3.times { i in
        print(i)    #=> 0, 1, 2
    }
    
    3.times {
        print(1)    #=> 1, 1, 1
    }
    

    Declaration

    Swift

    func times(_ closure: @escaping (Void) throws -> Void) rethrows

    Parameters

    closure

    A closure accepts Void and returns Void

  • Execute the block for self times

    let arr: [Int] = 3.times {
        return 1
    }
    arr     #=> [1, 1, 1]
    

    Declaration

    Swift

    func times<T>(_ closure: @escaping (Void) throws -> T) rethrows -> [T]

    Parameters

    closure

    A closure accepts Void and returns Void

    Return Value

    An array

  • Execute the block with counter for self times

    3.times { i in
        print(i)    #=> 0, 1, 2
    }
    
    3.times {
        print(1)    #=> 1, 1, 1
    }
    

    Declaration

    Swift

    func times(_ closure: (Int) throws -> Void) rethrows

    Parameters

    closure

    A closure accepts an int counter and returns Void

  • Execute the block for self times

    let arr: [Int] = 3.times {
        return $0
    }
    arr     #=> [0, 1, 2]
    

    Declaration

    Swift

    func times<T>(_ closure: @escaping (Int) throws -> T) rethrows -> [T]

    Parameters

    closure

    A closure accepts Void and returns Void

    Return Value

    An array