(366 lines)
4 Calculates the absolute value of an integer.8 The absolute value of `x`.13 The integer whose absolute value is to be calculated.15 assert (-10).abs() == 1020 Computes the binomial coefficient of the given numbers (the equivalent of `n`21 choose `k` in combinatorics). This is equal to `n.factorial()/(k.factorial() *22 (n-k).factorial())`.26 The binomial coefficient, equivalent to the number of ways to uniquely choose27 `k` objects from among `n` objects, ignoring order.32 The number of things to choose from.36 The number of things to be chosen.38 assert 4.choose(2) == 643 Returns the given number clamped between two values so that it is within44 that range.48 The first argument clamped between the other two arguments.53 The integer to clamp.57 The lowest value the result can take.61 The highest value the result can take.63 assert 2.clamped(5, 10) == 568 Computes the factorial of an integer.72 The factorial of the given integer.77 The integer to compute the factorial of.79 assert 10.factorial() == 362880084 In the binary representation of an integer, check whether a given bit index85 is set to 1 or not.87 For fixed-size integers, the bit index must be between 1 and the number of88 bits in that integer (i.e. 1-64 for `Int64`). For `Int`, the bit index must89 be between 1 and `Int64.max`. Values outside this range will produce a90 runtime error.94 Whether or not the given bit index is set to 1 in the binary95 representation of the integer.100 The integer whose bits are being inspected.104 The index of the bit to check (1-indexed).106 assert 6.get_bit(1) == no107 assert 6.get_bit(2) == yes108 assert 6.get_bit(3) == yes109 assert 6.get_bit(4) == no114 Converts an integer to its hexadecimal representation.118 The hexadecimal string representation of the integer.123 The integer to be converted.128 The minimum number of digits in the output string.133 Whether to use uppercase letters for hexadecimal digits.138 Whether to include a "0x" prefix.140 assert 255.hex(digits=4, uppercase=yes, prefix=yes) == "0x00FF"145 Determines if an integer is between two numbers (inclusive).149 `yes` if `a <= x and x <= b` or `a >= x and x >= b`, otherwise `no`154 The integer to be checked.158 One end of the range to check (inclusive).162 The other end of the range to check (inclusive).164 assert 7.is_between(1, 10) == yes165 assert 7.is_between(10, 1) == yes166 assert 7.is_between(100, 200) == no167 assert 7.is_between(1, 7) == yes172 Determines if an integer is a prime number.174 This function is _probabilistic_. With the default arguments, the chances of175 getting an incorrect answer are astronomically small (on the order of 10^(-30)).176 See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp)177 for more details.181 `yes` if `x` is a prime number, `no` otherwise.186 The integer to be checked.191 The number of repetitions for primality tests.193 assert 7.is_prime() == yes194 assert 6.is_prime() == no199 Finds the next prime number greater than the given integer.201 This function is _probabilistic_, but the chances of getting an incorrect202 answer are astronomically small (on the order of 10^(-30)).203 See [the GNU MP docs](https://gmplib.org/manual/Number-Theoretic-Functions#index-mpz_005fprobab_005fprime_005fp)204 for more details.208 The next prime number greater than `x`.213 The integer after which to find the next prime.215 assert 11.next_prime() == 13220 Converts an integer to its octal representation.224 The octal string representation of the integer.229 The integer to be converted.234 The minimum number of digits in the output string.239 Whether to include a "0o" prefix.241 assert 64.octal(digits=4, prefix=yes) == "0o0100"246 Return an iterator that counts infinitely from the starting integer (with an247 optional step size).251 An iterator function that counts onward from the starting integer.256 The starting integer.261 The increment step size.263 nums : &[Int] = &[]264 for i in 5.onward()265 nums.insert(i)266 stop if i == 10267 assert nums[] == [5, 6, 7, 8, 9, 10]272 Converts a text representation of an integer into an integer.276 The integer represented by the text. If the given text contains a value outside277 of the representable range or if the entire text can't be parsed as an integer,278 `none` will be returned.283 The text containing the integer.288 The numeric base to use when parsing the integer. If unspecified, the289 integer's base will be inferred from the text prefix. After any "+" or290 "-" sign, if the text begins with "0x", the base will be assumed to be291 16, "0o" will assume base 8, "0b" will assume base 2, otherwise the292 base will be assumed to be 10.297 If non-none, this argument will be set to the remainder of the text after the matching part.298 If none, parsing will only succeed if the entire text matches.300 assert Int.parse("123") == 123301 assert Int.parse("0xFF") == 255302 assert Int.parse("123xyz") == none303 remainder : Text304 assert Int.parse("123xyz", remainder=&remainder) == 123305 assert remainder == "xyz"307 # Can't parse:308 assert Int.parse("asdf") == none310 # Outside valid range:311 assert Int8.parse("9999999") == none313 # Explicitly specifying base:314 assert Int.parse("10", base=16) == 16319 Calculates the nearest square root of an integer.323 The integer part of the square root of `x`.328 The integer whose square root is to be calculated.330 assert 16.sqrt() == 4331 assert 17.sqrt() == 4336 Returns an iterator function that iterates over the range of numbers specified.340 An iterator function that returns each integer in the given range (inclusive).345 The starting value of the range.349 The ending value of the range.354 An optional step size to use. If unspecified or `none`, the step will be inferred to be `+1` if `last >= first`, otherwise `-1`.356 iter := 2.to(5)357 assert iter() == 2358 assert iter() == 3359 assert iter() == 4360 assert iter() == 5361 assert iter() == none363 assert [x for x in 2.to(5)] == [2, 3, 4, 5]364 assert [x for x in 5.to(2)] == [5, 4, 3, 2]365 assert [x for x in 2.to(5, step=2)] == [2, 4]