]>
Book Contents Previous Section 9.33 HexadecimalExpansion Next Section 9.35 IntegerLinearDependence Book Index

## 9.34 Integer

Axiom provides many operations for manipulating arbitrary precision integers. In this section we will show some of those that come from Integer itself plus some that are implemented in other packages. More examples of using integers are in the following sections: ugIntroNumbersPage in section ugIntroNumbersNumber IntegerNumberTheoryFunctionsXmpPage , DecimalExpansionXmpPage , BinaryExpansionXmpPage , HexadecimalExpansionXmpPage , and RadixExpansionXmpPage .

### 9.34.1 Basic Functions

The size of an integer in Axiom is only limited by the amount of computer storage you have available. The usual arithmetic operations are available.

2**(5678 - 4856 + 2 * 17)

48048107704350081471815409251259243912395261398716822634738556100 88084200076308293086342527091412083743074572278211496076276922026 43343568752733498024953930242542523045817764949544214392905306388 478705146745768073877141698859815495632935288783334250628775936

Type: PositiveInteger

There are a number of ways of working with the sign of an integer. Let's use this x as an example.

x := -101

-101

Type: Integer

First of all, there is the absolute value function.

abs(x)

101

Type: PositiveInteger

The sign sign Integer operation returns -1 if its argument is negative, 0 if zero and 1 if positive.

sign(x)

-1

Type: Integer

You can determine if an integer is negative in several other ways.

x < 0

true

Type: Boolean
x <= -1

true

Type: Boolean
negative?(x)

true

Type: Boolean

Similarly, you can find out if it is positive.

x > 0

false

Type: Boolean
x >= 1

false

Type: Boolean
positive?(x)

false

Type: Boolean

This is the recommended way of determining whether an integer is zero.

zero?(x)

false

Type: Boolean

Use the zero?zero?Integer operation whenever you are testing any mathematical object for equality with zero. This is usually more efficient that using = (think of matrices: it is easier to tell if a matrix is zero by just checking term by term than constructing another ``zero'' matrix and comparing the two matrices term by term) and also avoids the problem that = is usually used for creating equations.

This is the recommended way of determining whether an integer is equal to one.

one?(x)

false

Type: Boolean

This syntax is used to test equality using =. It says that you want a Boolean (true or false) answer rather than an equation.

(x = -101)@Boolean

true

Type: Boolean

The operations odd?odd?Integer and even?even?Integer determine whether an integer is odd or even, respectively. They each return a Boolean object.

odd?(x)

true

Type: Boolean
even?(x)

false

Type: Boolean

The operation gcd gcd Integer computes the greatest common divisor of two integers.

gcd(56788,43688)

4

Type: PositiveInteger

The operation lcm lcm Integer computes their least common multiple.

lcm(56788,43688)

620238536

Type: PositiveInteger

To determine the maximum of two integers, use max max Integer.

max(678,567)

678

Type: PositiveInteger

To determine the minimum, use min min Integer.

min(678,567)

567

Type: PositiveInteger

The reduce operation is used to extend binary operations to more than two arguments. For example, you can use reduce to find the maximum integer in a list or compute the least common multiple of all integers in the list.

reduce(max,[2,45,-89,78,100,-45])

100

Type: PositiveInteger
reduce(min,[2,45,-89,78,100,-45])

-89

Type: Integer
reduce(gcd,[2,45,-89,78,100,-45])

1

Type: PositiveInteger
reduce(lcm,[2,45,-89,78,100,-45])

1041300

Type: PositiveInteger

The infix operator ``/'' is not used to compute the quotient of integers. Rather, it is used to create rational numbers as described in FractionXmpPage .

13 / 4

13 4

Type: Fraction Integer

The infix operation quo quo Integer computes the integer quotient.

13 quo 4

3

Type: PositiveInteger

The infix operation rem rem Integer computes the integer remainder.

13 rem 4

1

Type: PositiveInteger

One integer is evenly divisible by another if the remainder is zero. The operation exquo exquo Integer can also be used. See ugTypesUnionsPage in Section ugTypesUnionsNumber for an example.

zero?(167604736446952 rem 2003644)

true

Type: Boolean

The operation divide divide Integer returns a record of the quotient and remainder and thus is more efficient when both are needed.

d := divide(13,4)

[quotient=3,remainder=1]

Type: Record(quotient: Integer,remainder: Integer)
d.quotient

3

Type: PositiveInteger

Records are discussed in detail in Section ugTypesRecords .

d.remainder

1

Type: PositiveInteger

### 9.34.2 Primes and Factorization

Use the operation factor factor Integer to factor integers. It returns an object of type Factored Integer. See FactoredXmpPage for a discussion of the manipulation of factored objects.

factor 102400

2 12 5 2

Type: Factored Integer

The operation prime?prime?Integer returns true or false depending on whether its argument is a prime.

prime? 7

true

Type: Boolean
prime? 8

false

Type: Boolean

The operation nextPrime nextPrime IntegerPrimesPackage returns the least prime number greater than its argument.

nextPrime 100

101

Type: PositiveInteger

The operation prevPrime prevPrime IntegerPrimesPackage returns the greatest prime number less than its argument.

prevPrime 100

97

Type: PositiveInteger

To compute all primes between two integers (inclusively), use the operation primes primes IntegerPrimesPackage.

primes(100,175)

[173,167,163,157,151,149,139,137,131,127,113,109,107,103,101]

Type: List Integer

You might sometimes want to see the factorization of an integer when it is considered a Gaussian integer. See ComplexXmpPage for more details.

factor(2 :: Complex Integer)

-i(1+i)2

Type: Factored Complex Integer

### 9.34.3 Some Number Theoretic Functions

Axiom provides several number theoretic operations for integers. More examples are in IntegerNumberTheoryFunctionsXmpPage .

The operation fibonacci fibonacci IntegerNumberTheoryFunctions computes the Fibonacci numbers. The algorithm has running time for argument n.

[fibonacci(k) for k in 0..]

[0,1,1,2,3,5,8,13,21,34,…]

Type: Stream Integer

The operation legendre legendre IntegerNumberTheoryFunctions computes the Legendre symbol for its two integer arguments where the second one is prime. If you know the second argument to be prime, use jacobi jacobi IntegerNumberTheoryFunctions instead where no check is made.

[legendre(i,11) for i in 0..10]

[0,1,-1,1,1,1,-1,-1,-1,1,-1]

Type: List Integer

The operation jacobi jacobi IntegerNumberTheoryFunctions computes the Jacobi symbol for its two integer arguments. By convention, 0 is returned if the greatest common divisor of the numerator and denominator is not 1.

[jacobi(i,15) for i in 0..9]

[0,1,1,0,1,0,0,-1,1,0]

Type: List Integer

The operation eulerPhi eulerPhi IntegerNumberTheoryFunctions computes the values of Euler's -function where equals the number of positive integers less than or equal to n that are relatively prime to the positive integer n.

[eulerPhi i for i in 1..]

[1,1,2,2,4,2,6,4,6,4,…]

Type: Stream Integer

The operation moebiusMu moebiusMu IntegerNumberTheoryFunctions computes the Möbius λ function.

[moebiusMu i for i in 1..]

[1,-1,-1,0,-1,1,-1,0,0,1,…]

Type: Stream Integer

Although they have somewhat limited utility, Axiom provides Roman numerals.

a := roman(78)

LXXVIII

Type: RomanNumeral
b := roman(87)

LXXXVII

Type: RomanNumeral
a + b

CLXV

Type: RomanNumeral
a * b

MMMMMMDCCLXXXVI

Type: RomanNumeral
b rem a

IX

Type: RomanNumeral
Book Contents Previous Section 9.33 HexadecimalExpansion Next Section 9.35 IntegerLinearDependence Book Index