class:: Integer
summary:: Integer number
categories:: Math

description::
A 32 bit integer. Integer inherits most of its behaviour from its superclass.

note::
A 32 bit signed integer can represent values in the range -2147483648 to 2147483647. Adding up further returns in a wrapped result, so that and 2147483647+1= -2147483648. For a larger range, one can use link::Classes/Float::, which is 64 bit and supports many (but not all) numerical methods that int does.
::


instancemethods::

subsection:: Iteration

method:: do
Executes strong::function:: for all integers from zero to this minus one.
argument:: function
a link::Classes/Function:: which is passed two arguments, both of which are the same
integer from zero to this minus one. The reason two arguments are passed is for
symmetry with the implementations of do in link::Classes/Collection::.

method:: reverseDo
Executes strong::function:: for all integers from  this minus one to zero.

method:: for
Executes strong::function:: for all integers from this to strong::endval::, inclusive.
argument:: endval
an link::Classes/Integer::.
argument:: function
a link::Classes/Function:: which is passed two arguments, the first which is an integer from this to
endval, and the second which is a number from zero to the number of iterations minus one.

method:: forBy
Executes strong::function:: for all integers from this to strong::endval::, inclusive, stepping each time by strong::stepval::.
argument:: endval
an link::Classes/Integer::.
argument:: stepval
an link::Classes/Integer::.
argument:: function
a link::Classes/Function:: which is passed two arguments, the first which is an integer from this to
endval, and the second which is a number from zero to the number of iterations minus one.

method:: collect
Returns:: an link::Classes/Array:: of this size filled by objects generated from evaluating the strong::function::.

method:: collectAs
Returns:: a link::Classes/Collection:: of strong::class:: of this size filled by objects generated from evaluating the strong::function::.

method:: to
returns:: an link::Classes/Interval:: from this to strong::hi::.

method:: geom
returns:: an array with a geometric series of this size from start.

method:: fib
returns:: an array with a fibonacci series of this size beginning with strong::a:: and strong::b::.

method:: factors
returns:: the prime factors as array.

method:: factorial
returns:: the factorial of this.


subsection:: Random Numbers
See also: link::Guides/Randomness::

method:: xrand
argument:: exclude
an link::Classes/Integer::.
returns:: a random value from zero to this, excluding the value exclude.

method:: xrand2
argument:: exclude
an link::Classes/Integer::.
returns:: a random value from this.neg to this, excluding the value exclude.

subsection:: Conversion

method:: asAscii
returns:: a link::Classes/Char:: which has the ASCII value of the receiver.

method:: asDigit
returns:: a link::Classes/Char:: which represents the receiver as an ASCII digit.
discussion:: For example code::5.asDigit:: returns code::$5::.

method:: asBinaryDigits
returns:: an array with the binary digits (integer 0 or 1).

method:: asDigits
returns:: an array with the n-ary digits.
discussion::
See also the complementary method link::Classes/SequenceableCollection#-convertDigits::.
code::
2007.asDigits;
2007.asDigits(2);
::

method:: asBinaryString
returns:: a string with the binary digits (0 or 1).

method:: asHexString
returns:: a string with the hexadecimal digits (integer 0 to F).

method:: asIPString
returns:: a string in IP format.

method:: degreeToKey
Interpret this as index into a scale with a given number of steps per ocatve.
discussion::
code::
2.degreeToKey([0, 2, 5, 7, 11]);
::

method:: grayCode
Returns:: the gray code for the number.
discussion::
code::
2.grayCode
::

subsection:: Binary Representation

method:: setBit
set nth bit to zero (bool = false) or one (bool = true)

method::leadingZeroes
code:: { _CLZ } ::

method:: trailingZeroes
code:: { _CTZ } ::

method:: numBits
returns:: number of required bits


subsection:: Properties

method:: even
returns:: true if dividable by 2 with no rest

method:: odd
returns:: true if not dividable by 2 with no rest


subsection:: Powers Of Two

method:: nextPowerOfTwo
returns:: the next power of two greater than or equal to the receiver.
discussion::
code::
13.nextPowerOfTwo.postln;
64.nextPowerOfTwo.postln;
::

method:: isPowerOfTwo
returns:: the whether the receiver is a power of two.
discussion::
code::
13.isPowerOfTwo.postln;
64.isPowerOfTwo.postln;
::


subsection:: Prime Numbers

method:: nthPrime
returns:: the nth prime number. The receiver must be from 0 to 6541.
discussion::
code::
[0,1,2,3,4,5].collect({ arg i; i.nthPrime; }).postln;
::

method:: prevPrime
returns:: the next prime less than or equal to the receiver up to 65521.
discussion::
code::
25.prevPrime.postln;
::

method:: nextPrime
returns:: the next prime less than or equal to the receiver up to 65521.
discussion::
code::
25.nextPrime.postln;
::

method:: isPrime
returns:: whether the receiver is prime.
discussion::
code::
25.isPrime.postln;
13.isPrime.postln;
::

method:: indexOfPrime
returns:: the index of a prime number less than or equal to the receiver up to 65521.
If the receiver is not a prime, the answer is nil.
discussion::
code::
23.indexOfPrime;
25.indexOfPrime;
::


subsection:: Misc

method:: pidRunning
returns:: a Boolean for whether or not the specified pid is running.
discussion::
code::
p = "cat".unixCmd;
p.pidRunning; // cat will stay alive
("kill" + p).unixCmd
p.pidRunning;
::
