Mathematics with Python and Ruby/Whole numbers in Ruby< Mathematics with Python and 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 RubyEdit
From a stringEdit
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
a="7" b=a.to_i puts(b+2)
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:
t="Supercalifragilisticexpialidocious" n=t.length puts(n)
From a real numberEdit
A real number too has a to_i method which converts it to an integer. This is sometimes useful because for Ruby 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
a=3.9999999 b=a.to_i puts(b)
a=3.9999999 b=a.floor puts(b)
On the converse, a real number has also a ceil method:
a=3.9999999 b=a.ceil puts(b)
But now an other problem arouses:
a=3.0000001 b=a.ceil puts(b)
Maybe 4 was not the expected answer!
Finally the best method to get an integer approximation is round:
a=3.9999999 b=a.round puts(b)
From an other integerEdit
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.
To know if an integer is prime requires an other module called mathn, which offers a primality test prime? (a boolean):
require 'mathn' a=2**32+1 puts(a.prime?)
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!)!
Addition, subtraction and multiplicationEdit
For Ruby, the arithmetic operations are denoted by +, - et * without surprise. These operations work also on negative integers:
a=5 b=-8 puts(a+b) puts(a-b) puts(a*b)
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:
num=3 den=2 q=num/den puts(q)
yields 1 instead of 1.5 because the integer quotient is 1 (with a remainder), while any if the following variants yields 1.5:
puts(3.0/2) puts(3/2.0) puts(3.0/2.0) puts(3.to_f/2)
To compute the exact value of a non integer quotient, one needs to use fractions.
Sometimes (often?) the quotient is not as important as the remainder (RSA kryptography for example, or even the hours, that are counted modulo 12). The remainder is given by the modulo operator denoted as %:
a=13 b=8 r=a%b puts(r)
It is then quite possible to compute in modular arithmetic with Ruby.
a=13572468 b=12345678 g=a.gcd(b) puts(g)
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:
require 'prime' n=2012 puts(n.prime?) puts(n.prime_division)
The above example shows that Ruby can even find the prime divisors of an integer!
a=4 b=2 puts(a**b) puts(b**a)
This shows that .
- If the exponent is negative, the result of the exponentiation is a fraction.
- If the exponent is a float, the result is a float even if it is actually an integer (like in 256**0.25).
In Ruby like in algebra, the computings are made in this order:
- First, the parentheses;
- then the functions (like the exponentiation for example);
" then only the multiplications and divisions;
- At last, the additions and subtractions.
displays 17 and not 25, because in this case the operations are not computed from left to right, but the multiplication first.