Mathematics with Python and Ruby/Whole numbers in Ruby



A peculiarity of the integers is that any one of them possesses a predecessor and a successor. Although it is very easy to compute them (subtract or add one to an integer), Ruby has methods coined pred and succ to handle them!

=How to get an integer in Ruby=

From a string
This script

yields exactly the same result than

at least at first sight. But if one tries to add 2,

yields 9 as expected, but

yields an error message, as one can not add a number with a string.

So a string representing an integer number has to be converted into a number with the to_i method (to integer). Thus

yields 9 this time.

An other way to get an integer number from a string is to count its letters. Which is made with its length property:

From a real number
A real number too has a to_i method which converts it to an integer. This is sometimes useful because for Ruby $$\sqrt{100}$$ is not considered as an integer number:

Ruby answers false because the number has been computed as 10.0 and not 10. So

yields the expected true. But

may not have the expected result: to_i uses a truncature instead of an approximation. Actually, floor has the same effect:

On the converse, a real number has also a ceil method:

But now an other problem arouses:

Maybe 4 was not the expected answer!

Finally the best method to get an integer approximation is round:

From an other integer
So, to get the successor of an integer, one asks to it to tell its successor, with its method succ:

tells that 7+1=8 (you bet!), whereas

shows that 7-1=6. But contrary to Peano's axiom number 7, zero has a predecessor (which is -1) because for Ruby the integers are rational integers and not only natural ones. Then, any integer has an additive inverse, which one can obtain by just preceding its name by a minus sign:

This yields 5 because the additive inverse of -5 is 5.

Primality
To know if an integer is prime requires an other module called mathn, which offers a primality test prime? (a boolean):

This shows at a glance that 4 294 967 297 is not prime, shame on Fermat (he has excuses, he didnt't own a Ruby console!)!

=Operations=

Addition, subtraction and multiplication
For Ruby, the arithmetic operations are denoted by +, - et * without surprise. These operations work also on negative integers:

Quotient
Unless otherwise specified, the division denoted by the slash operator is the integer one. To specify otherwise one needs to write one of the operands as a float, with a period:

yields 1 instead of 1.5 because the integer quotient is 1 (with a remainder), while any if the following variants yields 1.5:

To compute the exact value of a non integer quotient, one needs to use fractions.

Remainder
Sometimes (often?) the quotient is not as important as the remainder (RSA cryptography for example, or even the hours, that are counted modulo 12). The remainder is given by the modulo operator denoted as %:

It is then quite possible to compute in modular arithmetic with Ruby.

Divisors
In Ruby, the gcd is an infix operation:

Of course, a.gcd(b) and b.gcd(a) yield the same result.

Likewise, the lcm of two integers a and b can be computed as a.lcm(b).

An integer which has no divisor (except 1 and itself) is a prime number. Ruby can test the primality of an (not to big) integer:

The above example shows that Ruby can even find the prime divisors of an integer!

Exponentiation
In Ruby, the exponentiation operator is denoted with the same asterisk than the multiplication, but written twice:

This shows that $$2^4=4^2$$.

Remarks:
 * 1) If the exponent is negative, the result of the exponentiation is a fraction.
 * 2) If the exponent is a float, the result is a float even if it is actually an integer (like in 256**0.25).

Priority
In Ruby like in algebra, the computings are made in this order:

" then only the multiplications and divisions;
 * 1) First, the parentheses;
 * 2) then the functions (like the exponentiation for example);
 * 1) At last, the additions and subtractions.

Thus

displays 17 and not 25, because in this case the operations are not computed from left to right, but the multiplication first.