# Divisors

*divisor*of a given number "a" is the number by which "a" is divisible without a remainder.

How do we find all divisors of a given number. Let's use Maxima, WolframAlpha and Python. Maxima is a free computer algebra system that can be installed in Windows, macOS, and Linux. It is very powerful, fast, and very easy to use, ideal for students, teachers, and researchers alike.

You just type in an expression and press Shift and Enter, e.g. the query *divisor(24);* gives back the divisor of 24.You will get 1, 2, 3, 4, 6, 8, 12, 24.

You can also perform basic calculations, such as *3*7* (21), *6**2* (6^{2}=36), *sqrt(16);* (=4).

# Let's do it with Python.

Python is available on a wide variety of platforms (Linux, Windows, and macOS) and comes preinstalled on most Linux distributions.

user@pc:~$ *python*

Python 3.8.6 (default, May 27 2021, 13:28:02)

[GCC 10.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> from sympy import divisors # We need to import the sympy library

>>> **print(divisors(24))** # Return all divisors of n: [1, 2, 3, 4, 6, 8, 12, 24]
>>> print(divisors(124)) # [1, 2, 4, 31, 62, 124]

# Primes

*prime number*is one with exactly two positive divisors or factors, itself and one.

Maxima's command primep() is useful for testing whether or not an integer is prime or not, e.g. 7 (*primep(7);* return true) is a prime number, but 36 is not (primep(36); return false). The command *next_prime(n)* returns the next prime number greater than or equal to n

WolframAlpha uses natural language processing to understand queries, such as is 7 a prime number?, primes between 1 and 100, divisors of 48, etc.

user@pc:~$ *python*

Python 3.8.6 (default, May 27 2021, 13:28:02)

[GCC 10.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> from sympy import primerange, prime, isprime # We need to import the primerange, prime, and isprime functions from the sympy library

>>> **print(list(primerange(0, 36)))** # Return all prime numbers in the range [0, 36): [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31]

1 is not a prime number. A prime number is a positive integer that has exactly two positive divisors. However, 1 only has one positive divisor (1 itself), so it is not prime.

>>> **prime(5)** # prime(n) return the nth prime, with the primes indexed as prime(1) = 2, prime(2) = 3, etc., prime(5) = 11

>>> **isprime(17)** # isprime tests if n is prime, isprime(17) = True, isprime(32) = False

# Prime factorization

Observe that *factor(180); factor(420); or factor(54);* give back the prime factorization of 180, 420, and 54 respectively in Maxima and WolframAlpha, the only difference is the use of a semicolon (;) in Maxima.

user@pc:~$ *python*

Python 3.8.6 (default, May 27 2021, 13:28:02)

[GCC 10.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> from sympy import factorint, pprint

>>> **pprint(factorint(420, visual=True))**

2^{2} ⋅3 ^{1}⋅5 ^{1}⋅7 ^{1}

>>> **pprint(factorint(180, visual=True))**

2^{2} ⋅3 ^{2}⋅5 ^{1}

# Greatest common divisor (gcd) and least common multiple (lcm)

*greatest common divisor*(gcd) of two numbers (420, 54) is the largest product of primes that the two numbers share or the largest number that divides both of them without remainder. The

*least common multiple*(lcm) of two numbers is the smallest number that is a multiple or is evenly divisible by both of them.

They both can be determined using prime factorization, 420 = 2^{2} * 3 * 5 * 7, and 54 = 2 * 3^{3}. The lcm is the product of multiplying the highest power of each prime number (all factors found in either of the original number), lcm(420, 54) = 2^{2} * 3^{3} * 5 * 7 = 3780. The gcd is the product of multiplying the lowest power of common factors, gcd(420, 54) = 2 * 3 = 6.

The least common multiple of two numbers is the product of the two numbers divided by their greatest common divisor: 3780 = (420 * 54) / 6.

user@pc:~$ *python*

Python 3.8.6 (default, May 27 2021, 13:28:02)

[GCC 10.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import math

>>> **math.gcd(420, 54)**

6

>>> **420*54 // math.gcd(420, 54)**

3780

# Powers and roots

**power of a number**is the

*amount of times that a number is to be multiplied by itself*. For example, 7 to the power of two is 49, 7

^{2}= 7 x 7 = 49; 4 to the power of three is 64, 4

^{3}= 4 x 4 x 4 = 64. The

**square root**of a number is

*a number that, when multiplied by itself, equals the original number*. It is the inverse operation of squaring than number. For example, the square root of √49 is 7, the square root of √64 is 8, etc.

If you want to raise a number n to a power p in Maxima, simply type *n^p*, such as *4^3;* (= 64), *7^2;* (= 49) or *2^4;* (= 16). If you want to calculate the square root of a number (64) in Maxima, then your query would look like *sqrt(49);* You can use Qalculate, Wolfram Alpha, and Google using the same syntax but without the semicolon ";".

The Google Calculator is a pretty simple calculator, but it's nicely designed, minimalistic, and quite easy to use.

**inrt(x, n)** returns the n’th root of x, e.g. inrt(64, 3); = 4 (*4^3;* = 64), l: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; powerl: map (lambda ([a], a^3), l); = [1, 8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728]. *map (lambda ([a], inrt(a, 3)), powerl);* = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

user@pc:~$ *python*

Python 3.8.6 (default, May 27 2021, 13:28:02)

[GCC 10.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import math

>>> **math.sqrt(49)** # sqrt() function returns the square root of any number.

7

>>> **pow(4, 3)** # pow() returns the power of a number.

64

# Roman, Greek, Mayan, and Babylonian numeric systems

**Roman numerals is the numeric system used in ancient Rome**. They are formed by combining symbols (I, V, X, L, C, D, and M) together and adding their values (I is 1, V is 5, X is 10, and so on). For instance, the numbers 1 to 15 are expressed in Roman numerals as follows: I, II, III, IV, V, VI, VII, VIII, IX, X, XI, XII, XIII, XIV, XV.

However, it is a little more complicated than that. When a symbol of lesser value (I = 1) comes before a symbol of greater value (V = 5), we need to subtract the lesser value from the greater value (5 - 1), and add the result to the total, so XIV is 10 + (5 - 1) = 10 + 4 = 14.

Wolfram Alpha converts decimal numbers to Roman (*123 to Roman numerals*), Greek (*234 to Greek numerals*), Mayan (573 to Mayan), and Babylonian numerals (457 to Babylonian). It can also convert from one numeral system to another (*XVI to Mayan*), do arithmetic operations with Roman numerals (*XLVI + MMIV*), etc.

Qalculate can handle conversions between Roman numerals and decimal numbers. The result of calculations is displayed in the open area below the expression entry: the result display. Select **Mode, Number Base, Roman Numerals** to display the result using roman numerals. Besides, it can convert Roman numerals to decimal numbers by navigating through Functions, Miscellaneous, Roman Number, and typing the Roman numerals as CXXV (roman(CXXV)).

user@pc:~$ *python*

Python 3.8.6 (default, May 27 2021, 13:28:02)

[GCC 10.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> import roman # Small helper library to convert arabic to roman numerals.

>>> print(**roman.toRoman(124)**)

CXXIV

>>> print(roman.toRoman(87))

LXXXVII

>>> print(**roman.fromRoman("MML")**)

2050

>>> print(roman.fromRoman("XCVIII"))

98

>>>

# Other number systems: binary, octal, and hexadecimal

**number system**is

*a writing system for expressing or representing numbers*. The most common one used today is called the decimal system. It has ten symbols (0-9) and it has been so widely adopted throughout the world that you may not know that other number systems do indeed exist.

The **binary system** has a base of two and uses only two symbols: 0 and 1. Therefore, when you count in binary, it goes like this: 0 (0), 1(1), 10(2), 11(3), 100(4), 101(5), 110(6), 111(7)..., and thus each place value is a power of 2 instead of a power of 10. 1010_{2} = 1*2^{3} + 1*2^{1} = 10. Computers see everything in terms of binary.

The **octal numeral system** is the base-8 number system and uses eight symbols starting from 0 to 7. This system is also *a positional number system*, which means that the value of each digit depends on the position of the digit in the number and it is called the *place value* of that digit. In base 8, each digit represents a power of 8. For example: 275_{8} = 2*8^{2} + 7*8^{1}+ 5*8^{0} = 189. The place value of digit 7 is 7*8^{1} = 56.

The **hexadecimal system** has a base of sixteen and it uses sixteen different symbols: 0, 1, 2, ... 9, A(10), B(11), C(12), D(13), E(14), F(15). Under this system, each symbol's place value is worth 16 times more than the same symbol in the place value to its right. For instance: 1F2_{16} = 1 * 16^{2} + 15*16^{1} + 2 * 16^{0} = 498

Qalculate can handle conversions between different number systems. Select **Mode, Number Base, Binary, Octal, Decimal or Hexadecimal** to display the result in binary, octal, decimal or hexadecimal. It allows us to write numbers in binary, octal or hexadecimal form by preceding them with the appropriate prefix: 0b, 0o (zero "0" and letter "o"), or 0x: 0b1101102 (= 54), 0o16 (= 14), 0x2D16 (= 11542).

Wolfram Alpha and Google can convert numbers between different bases. To convert a number to binary, octal, hexadecimal, or decimal, type the number followed by the words to binary, to octal, to hex, or to decimal. Besides, Wolfram Alpha can perform computations with numbers on different bases.

user@pc:~$ *python*

Python 3.8.6 (default, May 27 2021, 13:28:02)

[GCC 10.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> print(**bin(79)**)

0b1001111

>>> print(**oct(79)**)

0o117

>>> print(**hex(79)**)

0x4f

>>> print(**int("0b1001111", 2)**)

79

>>> print(int("0o117", 8))

79

>>> print(int("0x4f", 16))

79

# Integers

"If a person is engaged in trade, his capital will be increased by his gains, and diminished by his losses. Increase in temperature is measured by the number of degrees the mercury rises in a thermometer, and decrease in temperature by the number of degrees the mercury falls. In considering any quantity whatever, a quantity that increases the quantity considered is called a positive quantity; and a quantity that decreases the quantity considered is called a negative quantity," The First Steps in Algebra, G. A. Wentworth.

The **absolute value** of a number *is its distance from zero on the number line*: |-4| = 4, |7| = 7. The absolute value of a positive number is just the number itself, abs(9) = 9. The absolute value of zero is zero, abs(0) = 0; and the absolute value of a negative number is its opposite, abs(-7) = 7.

The **opposite of a number** is the number that is situated at the same distance from 0 but in the opposite direction on the number line: -(-7)=7.

Operations with integers are a little tricky. Here is what we need to do when we add a negative and a positive number (-8 + 7). Use the sign of the larger number (-1) and subtract the smaller absolute value from the greater absolute value (8 - 7 = 1): -1.

You also need to remember: (R1) The product of a positive integer and a negative integer is always a negative integer: 4 * -3 = 12, -5 * 3 = 15. (R2) The product of two negative integers (-3 * -3 = 9, -4 * -5 = 20) or two positive integers (2 * 4 = 8, 4 * 7 = 28) is always a positive integer. The same rules apply to division: R1 (8 : -4 = -2, -4 : 2 = -2), R2 (8 : 4 = 2, -9 : -3 = 3).

You can see the representation of an integer on the Number Line with WolframAlpha. Just type a number.

user@pc:~$ *python*

Python 3.8.6 (default, May 27 2021, 13:28:02)

[GCC 10.2.0] on linux

Type "help", "copyright", "credits" or "license" for more information.

>>> 4-5

-1

>>> -5*(-4)

20

>>> (-8)/(-4)

2.0

>>> (-4*7)-(-3*(-2))

-34

>>> abs(-8)

8

# Implementing a Natural class in Python

# It is necessary to overload / from __future__ import division import math import roman from sympy import divisors, primerange, isprime, factorint, pprint class myNumber: ## Contructs a number # @param number, the number itself # @param base, the base in which the number is written (default is 10) def __init__(self, number, base = 10): if base>= 2 and base<= 36: self.number = int(number, base) else: raise TypeError("The base must be a value between 2 and 36.") @classmethod def roman(cls, romanNumber): return cls(str(roman.fromRoman(romanNumber))) # Sets need two methods to make an object hashable: __hash__ and __eq__ def __eq__(self, other): return self.number == other.number # An object hash is an integer number representing the value of the object def __hash__(self): return hash(self.number) # It is called when print() or str() and returns the string representation of the object def __str__(self): return str(self.number) # It is called when repr() and returns the object representation in string format def __repr__(self): return str(self.number) def __add__(self, other): return myNumber(str(self.number + other.number)) def __sub__(self, other): return myNumber(str(self.number - other.number)) def __mul__(self, other): return myNumber(str(self.number * other.number)) def __truediv__(self, other): return myNumber(str(self.number // other.number)) def __mod__(self, other): return myNumber(str(self.number % other.number)) # Return all divisors of our number def divisors(self): print("divisors(" + str(self.number) + ") = ", divisors(self.number)) # It generates a list of all prime number in the range [0, number). def primes(self): return list(primerange(0, self.number) ) # It checks if the number is prime def isPrime(self): if isprime(self.number): print(self.number, "is prime.") else: print(self.number, "is not prime.") # It returns the factorization of the number def factorization(self): pprint(factorint(self.number, visual=True)) # It returns the gcd of itself and other.number def gcd(self, other): print("gcd(" + str(self.number) + ", " + str(other.number) + ") =", math.gcd(self.number, other.number)) # It returns the lcm of itself and other.number def lcm(self, other): lcmResult = abs(self.number * other.number) // math.gcd(self.number, other.number) print("lcm(" + str(self.number) + ", " + str(other.number) + ") =", lcmResult) # It returns the square root of the number def sqrt(self): print("sqrt(" + str(self.number) + ") =", math.sqrt(self.number)) def toRoman(self): print(str(self.number) + " =", roman.toRoman(self.number)) def toBinary(self): print(str(self.number) + "=" + str(bin(self.number)[2:]) + "\u2082") def toOctal(self): print(str(self.number) + "=" + str(oct(self.number)[2:]) + "\u2088") def toHexadecimal(self): print(str(self.number) + "=" + str(hex(self.number)[2:]) + "\u2081\u2086") def abs(self): print("|" + str(self.number) + "| = " + str(abs(self.number)) ) def fact(self): print(str(self.number) + "! = " + str(math.factorial(self.number)) ) def elementaryMaths(): myFirstNumber = myNumber("24") myFirstNumber.divisors() # divisors(24) = [1, 2, 3, 4, 6, 8, 12, 24] print(myFirstNumber.primes()) # [2, 3, 5, 7, 11, 13, 17, 19, 23] myFirstNumber.isPrime() # 24 is not prime. myFirstNumber.factorization() # 2^{3}*3^{1}mySecondNumber = myNumber("4200") mySecondNumber.factorization() # 2^{3}*3^{1}*5^{2}*7^{1}myNumber("60").gcd(myNumber("48")) # gcd(60, 48) = 12 myNumber("60").lcm(myNumber("48")) # lcm(60, 48) = 240 myNumber("16").sqrt() # sqrt(16) = 4.0 mySecondNumber.toRoman() # 4200 = MMMMCC myNumber("18").toBinary() # 18=10010₂ myNumber("19").toOctal() # 19=23₈ myNumber("19").toHexadecimal() # 19=13₁₆ myNumber("-19").abs() # |-19| = 19 myThirdNumber = myNumber.roman("XCVIII") print(myThirdNumber) # 98 print(myNumber("4F", 16)) # 79 print(myNumber("7").fact()) # 7! = 5040 print(myNumber.roman("XVIII") + myNumber("1F", 16)) # 49(=18+31) print(myNumber("1F", 16) - myNumber.roman("XVIII")) # 13(=31-18) print(myNumber("1F", 16) * myNumber.roman("XVIII")) # 558(=31*18) print(myNumber("1F", 16) / myNumber.roman("XVIII")) # 1(=31/18) print(myNumber("1F", 16) % myNumber.roman("XVIII")) # 13(=31%18) if __name__ == '__main__': elementaryMaths()