Elementary Maths

Divisors

The 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 (62=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

A 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

Prime factorization is the process of discovery or finding which prime numbers (prime factors) you need to multiply together to get a certain number.

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))
22 ⋅3 1⋅5 1⋅7 1
>>> pprint(factorint(180, visual=True))
22 ⋅3 2⋅5 1

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

The 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 = 22 * 3 * 5 * 7, and 54 = 2 * 33. 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) = 22 * 33 * 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

The 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, 72 = 7 x 7 = 49; 4 to the power of three is 64, 43 = 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

A 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. 10102 = 1*23 + 1*21 = 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: 2758 = 2*82 + 7*81+ 5*80 = 189. The place value of digit 7 is 7*81 = 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: 1F216 = 1 * 162 + 15*161 + 2 * 160 = 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

Integers are whole numbers (1, 2, 3, 4,...), negative of whole numbers (-1, -2, -3, -4,...), and the number zero(0). Negative whole numbers are used for expressing temperatures below freezing (0ºC), for measuring altitude below sea level, and when someone withdraws more than the current balance. Qalculate, Maxima, Google, and WolframAlpha can perform computations with 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() # 23*31
    mySecondNumber = myNumber("4200")
    mySecondNumber.factorization() # 23*31*52*71
    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()

Sets and Operations on Sets

Sets and Operations on Sets

What is a set? It is a collection of things, objects, such as animals, plants, vowels (a, e, i, o, u), swearwords, etc. Yes, you've guessed it, I am not going to give examples of these! Basically, it is a list or a group of unordered items which means: The order is irrelevant, {1, 2} is the same set as {2, 1}; and the elements are not repeated.

We can easily work with sets in Maxima. Let's define sets in this free and useful application. It is very easy, just list the elements that form the set. For instance, mySet:{a, b, c, d, f}; Do not forget the semicolon ";" and simultaneously press Ctrl + Enter. Please observe that the elements are grouped in brackets "{ ... }" and separated by commas ",".

The number of elements that make up or are contained in a set is called the cardinality. The cardinality of mySet is five and we use the following command to obtain it from Maxima: cardinality(mySet);

mySet:{a, b, c, d, f}; is a definition by extension, that is, writing down or listing all the elements that belong to the set. Maxima also allows definition by intension. We describe a set by a defining property, a common feature that all of the set's elements share. One example is otherSet: divisors(32); In other words, otherSet is the set of all the divisors of 32: {1, 2, 4, 8, 16, 32}.

Here's another way to define complex sets in Maxima: setify (makelist (fib (n), n, 1, 10)); This gives a set formed from the list (setify converts a list into a set — it removes duplicates) of the Fibonacci series’ first elements (fib(n) returns the n-th Fibonacci number, fib (0) = 0, fib (1) = 1, fib (2) = 1, fib (3) = 2, etc. — fib(n) = fib(n-1) + fib(n-2)).

Observe the result: {1, 2, 3, 5, 8, 13, 21, 34, 55}. There is something funny going on. Can you guess what it is?

We have asked for the first 10 elements of the Fibonacci series: makelist(fib(n), n, 1, 10). It indeed gives us back ten numbers: [1, 1, 2, 3, 5, 8, 13, 21, 34, 55]. However, the cardinality of {1, 2, 3, 5, 8, 13, 21, 34, 55} is 9. Do you know why? Of course, makelist (fib (n), n, 1, 10); actually returns 10 numbers, but one of the "1"s is removed from the list by setify to form a set.

Therefore, it is very important to understand that all elements have to be different. Thus, we can define sets as C = setify ([1, 2, 3, a, b, c]); and the result will be the same as if we write C = setify ([1, 2, 3, a, b, c, 1, b, b]);.

Two sets are equal if they have the same elements. Please let me insist again on the fact that the order within a set is not relevant at all, so setequalp({a, b, c, d, e, f}, {f, e, d, c, b, a}); returns true because setequalp returns true if the two sets passed as arguments are equal.

On the contrary, setequalp({a, b, c, d, e, f}, {f, e, d, c, b, a, h}); returns false.

Let's work with sets in Python:

rainbow_colors = {"Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet", "Red"}
print(rainbow_colors) # sets cannot contain duplicate values and sets don't have an order: {'Indigo', 'Red', 'Yellow', 'Violet', 'Green', 'Blue', 'Orange'}
print(len(rainbow_colors)) # Get the length/cardinality of the set, 7
traffic_light_colors = {"Red", "Yellow", "Blue"} 
traffic_light_colors.remove("Blue") # We can remove items from a set with the remove method.
traffic_light_colors.add("Green") # We can add items from a set with the add method.
print(traffic_light_colors) # {'Green', 'Red', 'Yellow'}
if (rainbow_colors==traffic_light_colors): # Comparing sets in Python. Obviously, both sets are not equal. 
   print("Both sets are equal")
else:
   print("Both sets are not equal")

Set Operations

The intersection A ∩ B of two sets A and B is the set containing all the elements common to both sets. Formally, mySet ∩ yourSet = {element: element ∈ mySet and element ∈ yourSet}. If the intersection is empty, the sets are said to be disjointed because they do not have any common members or elements.

Let's ask Maxima for the intersection of two sets: intersect(mySet, otherSet); It returns {1, 2, 8} where mySet: setify(makelist(fib(n), n, 1, 10)); and otherSet: divisors(32); It gives back all the elements that are both in mySet ({1, 2, 3, 5, 8, 13, 21, 34, 55}) and otherSet ({1, 2, 4, 8, 16, 32}).

The command disjointp (mySet, otherSet); returns false because both sets are not disjointed, actually they share 1, 2, and 8. However, disjointp({2, 4, 6, 8}, {1, 3, 5, 7}); is true.

The union A ∪ B of two sets A and B is the set that contains all the elements of A plus all the elements of B and nothing else. Formally, mySet ∪ yourSet = {element: element ∈ mySet or element ∈ yourSet}

Let's obtain the union of two sets: union(mySet, otherSet); return {1, 2, 3, 4, 5, 8, 13, 16, 21, 32, 34, 55} and union(setify ([1, 2]), setify([6, 2, 7])); = {1, 2, 6, 7}. It gives back all the elements in the two sets and no others.

The Cartesian product A x B of two sets A and B is the set of all ordered pairs (a, b), where the first element of each pair, a, is an element from A, and the second, b, is an element from B. We could express this in mathematical language as A x B = {(item1, item2) where item1 ∈ A and item2 ∈ B}.

We can request Maxima for the Cartesian product: cartesian_product(mySet, otherSet); = {[1,1],[1,2],[1,4],[1,8],[1,16],[1,32],[2,1],[2,2],[2,4],[2,8],[2,16],[2,32],[3,1],[3,2],[3,4],[3,8],[3,16],[3,32],[5,1],[5,2],[5,4],[5,8],[5,16],[5,32],[8,1],[8,2],[8,4],[8,8],[8,16],[8,32],[13,1],[13,2],[13,4],[13,8],[13,16],[13,32],[21,1],[21,2],[21,4],[21,8],[21,16],[21,32],[34,1],[34,2],[34,4],[34,8],[34,16],[34,32],[55,1],[55,2],[55,4],[55,8],[55,16],[55,32]}. cartesian_product(setify ([1, 2, 5]), setify([5, 7])); = {[1,5],[1,7],[2,5],[2,7],[5,5],[5,7]}

The difference A - B between two sets A and B is the set containing all of the elements that are in the first set, but not in the second one. We can formulate it as: mySet - otherSet = {element: element ∈ mySet and element ∉ otherSet}. Then, we are going to ask Maxima for the difference between two sets: setdifference (mySet, otherSet); = {3, 5, 13, 21, 34, 55}. setdifference(setify ([1, 2, 5]), setify([5, 7])); = {1, 2}

The symmetric difference A △ B of two sets A and B is all the elements that are in either of the sets, but not in both sets. Strictly speaking: A △ B = { element: (element ∈ A ∧ element ∉ B) or (element ∉ A ∧ element ∈ B) }. Let's require the symmetric difference in Maxima: symmdifference (mySet, otherSet); ={3, 4, 5, 13, 16, 21, 32, 34, 55}. symmdifference(setify ([1, 2, 5]), setify([5, 7])); = {1, 2, 7}

Let's do it with Python:

rainbow_colors = {"Red", "Orange", "Yellow", "Green", "Blue", "Indigo", "Violet"}
my_favourite_colors = {"Red", "White"}   
print(rainbow_colors & my_favourite_colors) # Intersection of sets with the operator >>> {'Red'}
print(rainbow_colors.isdisjoint(my_favourite_colors)) # Two sets are disjoint if they have no elements in common >>> False
print(rainbow_colors | my_favourite_colors) # Union of sets with the operator "|" >>> {'White', 'Violet', 'Indigo', 'Yellow', 'Blue', 'Red', 'Orange', 'Green'}
print(rainbow_colors - my_favourite_colors) # Difference of sets with the operator "-" >>> {'Violet', 'Orange', 'Indigo', 'Green', 'Yellow', 'Blue'}
print(rainbow_colors ^ my_favourite_colors) # Symmetric difference is performed using the operator "^" >>> {'Green', 'Blue', 'White', 'Yellow', 'Orange', 'Violet', 'Indigo'}

Learning with WolframAlpha.

Let's do some queries: intersection (A intersect B), union (A union B), symmetric difference (symmetric difference of A and B), and a more complex operation: (A union B) insersect B) = (A ∪ B) ∩ C.

It returns a graphical representation called a Venn diagram in which the colored part of the image represents the result. A Venn diagram is a graphical way of representing the relationships between a finite collection of sets.

After that, you could try to ask for the intersection (Intersect[{a, b, c, d, e}, {a, c, j}]), difference (complement[{a, b, c, d, e}, {a, c, j}]), and union (union[{a, b, c, d, e}, {a, c, j}]) of the sets {a, b, c, d, e} and {a, c, j}. Finally, DisjointQ[{a, b, c, d, e}, {a, c, j}] return false because these two sets share "a" and "c" as common elements.

Sets of objects (Python)

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.")
        
    # 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 main():
    myFirstNumber = myNumber("101", 2)
    print("The hash is: %d" % hash(myFirstNumber))  # >>> The hash is: 5
    mySecondNumber = myNumber("12", 8)
    my_set = set()
    my_set.add(myFirstNumber)
    my_set.add(mySecondNumber)
    my_set.add(myNumber("5"))
    print(my_set)  # >>> {10, 5}
    print(my_set & {myNumber("2"), myNumber("4"), myNumber("6"), myNumber("8"), myNumber("10"), myNumber("12")}) # >>> {10}
    print(my_set | {myNumber("2"), myNumber("4"), myNumber("6"), myNumber("8"), myNumber("10"), myNumber("12")}) # >>> {2, 4, 5, 6, 8, 10, 12}
    print(my_set - {myNumber("2"), myNumber("4"), myNumber("6"), myNumber("8"), myNumber("10"), myNumber("12")}) # >>> {5}
    print(my_set ^ {myNumber("2"), myNumber("4"), myNumber("6"), myNumber("8"), myNumber("10"), myNumber("12")}) # >>> {2, 4, 5, 6, 8, 12}
    print(my_set.isdisjoint({myNumber("2"), myNumber("4"), myNumber("6"), myNumber("8"), myNumber("10"), myNumber("12")})) # >>> false
if __name__ == '__main__':
    main()

Decimals, Percentages, Mixed numbers and improper fractions

A man is like a fraction whose numerator is what he is and whose denominator is what he thinks of himself. The larger the denominator, the smaller the fraction, Leo Tolstoy.

Decimals, Fractions, and Percentages

Decimals, fractions, and percentages are, basically, alternative ways of representing the same value. For instance, 0.5 = 12 = 50% could represent half of a yummy pizza; 34 inch is a fraction of the base unit (three quarters of an inch), equals to 0.75 inches, or think of it as a halfway between 1⁄2 inch and a full inch or 75%.

How do we convert between them? It's not as difficult as it looks. Let's convert fractions (34) to decimals. Just divide the numerator (the top part, 3) by the denominator (the bottom part, 4), so 34 = 0.75.

Next, we will convert decimals (0.75) to fractions:

Let's convert fractions (3⁄4) to percentages. Divide the numerator (the top part, 3) by the denominator (the bottom part, 4, as 34 = 0.75), multiply the result by 100, and add the % sign, 0.75 x 100 = 75%.

How do you convert percentages (42.5%) to fractions?

Next, let's convert decimals (0.25) to percentages. Multiply by 100 and add the % sign, 0.25 x 100 = 25%.

Do you want to convert percentages (45%) to decimals? Easy peasy lemon squeezy. Just divide by 100 and drop the % sign, 45 : 100 = 0.45.

If you want to use Maxima to convert from fractions to decimals, you should use decimal points (12.0; = 0.5) or enclose the calculation within "float()" as float(34); = 0.75 Alternatively, you can use Wolfram Alpha: 35%, convert 1/4 to percent, 25% of 200, etc.

Mixed numbers and improper fractions

Improper fractions and mixed numbers are basically two different ways of expressing the same number, they are both used to show a value bigger than one whole unit.

Mixed numbers, such as 2 34, 3 14, or 4 35, are made up of a whole integer (2) and a fraction (3⁄4). Think of it as we are counting whole things (2 yummy pizzas), and parts of things (three-quarters of a third pizza) at the same time.

Let's convert a mixed number (2 3⁄4) to an improper fraction. First, multiply the whole number by the denominator and add the numerator: 2 * 4 + 3 = 11. This is the new numerator. Then, use the denominator of the fraction part of the mixed number as the denominator, 2 34 = 114.

As you can see in WolframAlpha, an improper fraction, such as 85, 74 or 32 has a numerator (8) greater than (or equal to) the denominator (5).

It corresponds to 1 3⁄5 and you can read it as one pizza and three-fifths of a second one. It also means eight pieces of two pizzas that have been divided into five pieces, so you have a whole pizza -5 pieces- and three pieces of the second.

Qalculate! is a multi-purpose cross-platform desktop calculator. It is simple to use but provides power and versatility normally reserved for complicated math packages, as well as useful tools for everyday needs (such as currency conversion and percent calculation). The top buttons (from left to right) switches between the general keypad and the programming keypad, switches between exact and approximate calculation, changes rational number form (it is selected Mixed fractions), selects display mode and selects number base in result

If you want to convert an improper fraction 7⁄3 to a mixed number, divide the numerator by the denominator (7 : 3 = 2 with a reminder of 1). Write down the quotient 2. Next, write down a fraction with the remainder (1) as the numerator and the same denominator of the improper fraction (3), 73 = 213.

Another program that you can use to learn fractions is KBruch (Ubuntu, Debian and derived distributions: sudo apt install kbruch). It is a small program to practice calculating with fractions and percentages.

Different exercises are provided for this purpose: arithmetic, comparison (you have to compare between two given fractions using the symbols >, < or =), conversion (you are given the task to convert a number into a fraction), factorization (in this exercise you have to factorize a given number into its prime factors), mixed numbers, and percentage. It is part of The KDE Education Project, a free educational suite for children and adults, students and teachers. It is available in GNU/Linux and Windows.

Python: Small changes to our class to manage improper fractions

class Fraction:[...]
 ## Gets a string representation of the fraction
    # @return a string in the format numerator/denominator
    def __str__(self):
        if self.denominator == 1:
            return str(self.numerator)
        # Manage improper fractions
        elif abs(self.numerator) > self.denominator:
            if self.numerator > 0:
                a = self.numerator // self.denominator
                b = self.numerator % self.denominator
                return "{} {}/{}".format(a, b, self.denominator)
            # Mathematically Python is not giving correct output for integer division for negative numbers
            else:
                a = - (abs(self.numerator) // self.denominator)
                b = - (abs(self.numerator) % self.denominator)
                return "{} {}/{}".format(a, -b, self.denominator)
def main():
    f1 = Fraction(2,-3)
    f2 = Fraction(3,-4)
    f3 = f1 + f2
    print(f1, "+", f2, "=", f3) # -2/3 + -3/4 = -1 5/12
    f3 = Fraction(4, -6)
    print(f3, "=", "{:.2f}".format(round(float(f3), 2)), "=",  round( float(f3)*100, 2  ), "%") # -2/3 = -0.67 = -66.67 %
    f4 = Fraction(1, 3)
    print(f4, "=", "{:.2f}".format(round(float(f4), 2)), "=",  round( float(f4)*100, 2  ), "%") # 1/3 = 0.33 = 33.33 %
    print(Fraction(8, 5)) # 1 3/5
    print(Fraction(-13, 6)) # -2 1/6

Fractions are really cool!

Five out of four people have trouble with fractions, Steven Wright.

A fraction is a fancy way of representing the division of a whole into parts, for example, half of a pizza (12), a quarter of an apple (14), etc.

How can you learn about them?

You can use JFractionLab. It's a free (it is licensed by the GNU GPL license), multi-platform (it runs on Windows, macOS, and Linux), and easy to use program for practicing fractions at school or at home.

It gives you graphical feedback, so you can always see and understand what you are doing.

It runs on Windows, GNU/Linux, and MacOS, but it requires the installation of the Java Runtime Enviroment. If you use Ubuntu or other Debian based distributions, type this command in your terminal: sudo apt install jfractionlab.

Launch it (JFractionLab), and select one category: Clicking the numerator, Defining or comparing fractions, or Extending fractions.

Observe that 3⁄6 is the fraction which is represented in the screenshot. The pie is divided into six parts and there are only three pieces which are coloured yellow.

48 is lower than 88 ( = 1), in other words, (almost) everyone prefers eating eight pieces of a yummy pizza (the whole pizza) rather than only four (half of it).

Equivalent fractions and reducing fractions


As you can see 620 and 310 look quite different, but they represent the same value, the same area colored in yellow.

These two fractions are equivalents. You can make equivalent fractions by multiplying or dividing the numerator and denominator by the same number.

In our example, if you multiply the numerator (3), and the denominator (10) of our second fraction (310) by the same number (2), you will get the first fraction (6⁄20). The idea is simple. It is the same to have 3 slices of a pizza that has been cut into 10 equal slices as having 6 slices of a pizza that has been cut into 20 slices.

Every fraction can be written in its lowest terms; simply put, it cannot be further reduced. This is called an irreducible fraction (or fraction in lowest terms or reduced fraction). A fraction is irreducible if and only if the numerator and denominator are coprimes: the only number that divides both of them evenly is 1.

It can be obtained by dividing both the numerator and denominator by the greatest common divisor.

Let's help you reduce any fractions to its lowest terms. You may use WolframAlpha, Maxima (just type your fraction) or Python.

wxMaxima is a document based interface for Maxima. Maxima is a free (it is released under the terms of the GNU General Public License) computer algebra system. It is written in Common Lisp and runs on macOS, Unix, BSD, and Linux, as well as under Microsoft Windows and Android. Installing it in windows is fairly simple. If you are in Linux (Ubuntu, Debian, etc.), open a terminal and type: sudo add-apt-repository ppa:blahota/wxmaxima. sudo apt-get update. sudo apt-get install wxmaxima.

To start using wxMaxima right away, start typing your command (8/20;) and press Shift + Enter to evaluate it.

Hi@Welcome:~ python
Python 3.8.6 (default, May 27 2021, 13:28:02) 
>>> from fractions import Fraction # Fraction module in Python
>>> print (Fraction(8, 20))
2/5
>>> print (Fraction(6, 14))
3/7
>>> import math
>>> print (math.gcd(8, 20)) # The Highest Common Factor (gcd) can be computed in python using a single function offered by the math module. 
4
>>> print (math.gcd(6, 14))
2

Fraction Arithmetic.

How do you add fractions together? Do they have the same denominator? If so, add the two numerators and write the sum over the same denominator: 23 + 53 = 73.

If the fractions do not have the same denominator, we must make sure that they do. For instance, 23 + 35. This step is done by finding a common denominator. A way of doing this is by multiplying both denominators (3 x 5 = 15) and rewriting both fractions using this product.

Take into account that you need to multiply both numerator and denominator by the same number to get equivalent fractions. Our fractions, 23 + 35, are rewritten to, = 1015 + 915, which in turn is equal to = 1915. Finally, rewrite, if necessary, the result as a simplified or reduced fraction.

How do you subtract fractions? If they have the same denominator, subtract both numerators while leaving the denominator unchanged: 73 - 53 = 43.

As it was explained before, if they do not share the same denominator, it will be necessary to make sure that they do. For example, 34 - 23 = 912 - 812 = 112. Rewrite, if necessary, the result as a simplified or reduced fraction.

Let's learn to multiply fractions. It will be easy peasy, lemon squeezy. You only need to multiply both numerators and denominators. For instance, 34 * 23 = 612. Rewrite, if necessary, the result as a simplified or reduced fraction = 12.

Finally, let's divide some fractions. Multiply the first fraction by the reciprocal of the second fraction. You just need to turn the second fraction upside down or, in other words, flip it over, so that the numerator becomes the denominator and the denominator becomes the numerator. For instance, 34 / 23 = 98. Simplify, if necessary the result.

Let's make a Fraction Class in python

# It is necessary to overload /
from __future__ import division

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

class Fraction:
    ## Contructs a rational number
    # @param n the numerator of the fraction (default is 0) 
    # @param d the denominator of the fraction (default is 1)
    def __init__(self, n = 0, d = 1):
        if (not isinstance(n, int)):
            raise TypeError("The numerator of a Fraction must be an integer")
        if (not isinstance(d, int)):
            raise TypeError("The denominator of a Fraction must be an integer")
        
        self.numerator = int(n / gcd(abs(n), abs(d)))
        self.denominator = int(d / gcd(abs(n), abs(d)))
        if self.denominator < 0:
            self.denominator = abs(self.denominator)
            self.numerator = -1*self.numerator
        # The denominator cannot be zero
        elif self.denominator == 0:
            raise ZeroDivisionError("Denominator cannot be zero")

    ## Adds a fraction to this fraction.
    # @params other the fraction to add.
    # @return the new Fraction object that is the result of this + other.
    def __add__(self, other):
        num = self.numerator * other.denominator + self.denominator * other.numerator
        den = self.denominator * other.denominator
        return Fraction (num, den)
    
    ## Substracts a fraction from this fraction.
    # @params other the fraction to substract.
    # @return the new Fraction object that is the result of this - other.
    def __sub__(self, other):
        num = self.numerator * other.denominator - self.denominator * other.numerator
        den = self.denominator * other.denominator
        return Fraction (num, den)
    
    ## Determines if this fraction is equal to another fraction.
    # @params other the righ-hand side fraction
    # @return True if the fractions are equal 
    def __eq__(self, other):
        return (self.numerator == other.numerator and self.denominator == other.denominator)
     
    ## Multiply this by another fraction.
    # @params other the fraction to multiply this by.
    # @return the new Fraction object that is the result of this * other. 
    def __mul__(self, other):
        return Fraction(self.numerator * other.numerator, self.denominator* other.denominator)
    
    ## Divide this by another fraction.
    # @params other the fraction to divide this by.
    # @return the new Fraction object that is the result of this / other.
    def __truediv__(self, other):
        return Fraction(self.numerator * other.denominator, self.denominator * other.numerator)
    
    def __float__(self):
        return self.numerator / self.denominator
    
    ## Gets a string representation of the fraction
    # @return a string in the format numerator/denominator
    #
    def __str__(self):
        if self.denominator == 1:
            return str(self.numerator)
        else:
            return str(self.numerator) + "/" + str(self.denominator)
def main():
    f1 = Fraction(2,-3)
    f2 = Fraction(3,-4)
    try:
        f3 = Fraction(3, 0)
    except ZeroDivisionError:
        print("Oops! Denominator cannot be zero")
    f3 = f1 + f2
    print(f1, "+", f2, "=", f3)
    f3 = f1 - f2
    print(f1, "-", f2, "=", f3)
    f3 = f1 * f2
    print(f1, "*", f2, "=", f3)
    f3 = f1 / f2
    print(f1, "/", f2, "=", f3)
    f3 = Fraction(4, -6)
    print("Are equal ", f3, " and ", f1, "? ", f3==f1)
    print(f3, "=", "{:.2f}".format(round(float(f3), 2))    )
if __name__ == '__main__':
    main()

Extra Bonus: Testing our code

import unittest
from fraction import Fraction

class TestFraction(unittest.TestCase):
    def setUp(self):
        print("setUp")
        self.fraction1 = Fraction(2,-3)
        self.fraction2 = Fraction(3,-4)
        self.fraction3 = Fraction(4, -6)
    def test_add(self):
        print("test_add")
        self.resultado = self.fraction1 + self.fraction2
        self.assertEqual(self.resultado, Fraction(-17, 12))
    def test_sub(self):
        print("test_sub")
        self.resultado = self.fraction1 - self.fraction2
        self.assertEqual(self.resultado, Fraction(1, 12))
    def test_mul(self):
        print("test_mul")
        self.resultado = self.fraction1 * self.fraction2
        self.assertEqual(self.resultado, Fraction(1, 2))
    def test_div(self):
        print("test_div")
        self.resultado = self.fraction1 / self.fraction2
        self.assertEqual(self.resultado, Fraction(8, 9))
    def test_init(self):
        print("test_init")
        with self. assertRaises(ZeroDivisionError):
            Fraction(1, 0)
        with self. assertRaises(TypeError):
            Fraction(1.2, 3)
            Fraction(a, 2)
    def test_equal(self):
        print("test_equal")
        self.assertEqual(self.fraction1 == self.fraction3, True)
    def test_float(self):
        print("test_float")
        self.assertEqual(round(float(self.fraction3), 2), -0.67)
        
if __name__ == '__main__':
    unittest.main()

Window & login managers, shells

File managers are essential for managing everyday activities. They provide a fast and convenient way to manage files and folders on your desktop. Some examples are Nautilus, Dolphin, Thunar, and Nemo: sudo apt-get install nautilus dolphin thunar nemo
A compositing window manager or compositor provides applications with an off-screen buffer for each window. The window manager composites the window buffers into an image representing the screen and writes the result into the display memory.
Picom is the most popular Compositor for Linux. It helps us customize our window appearances and behaviour: sudo apt install picom.
If you don't have an US Keyboard Layout, you need to edit the .xinitrc configuration file, vim .xinitrc: setxkbmap -layout 'es,es' -model pc105, where you are using setxkbmap (sets the keyboard layout).
Window managers are responsible for the placement and appearance of all windows. Some examples are: i3, Awesome WM, XMonad, etc.

We can use Rofi (sudo apt install rofi) as an alternative to dmenu. Rofi is a window switcher, application launcher, and dmenu replacement. Ctrl + tab switches between modes: run (lunch applications), window (switch between windows), and ssh.

-- Rofi
    awful.key({ modkey },            "r",     function () awful.util.spawn("rofi -show run") end,
              {description = "Dmenu", group = "launcher"}),
Display managers provide a nice graphical interface for logging into your Linux desktop. After you log in to a GUI desktop, the display manager turns control over to the window manager. A few examples are gdm, gdm3, lightdm, and kdm.
Ubuntu ships with GDM3 as a display manager: cat /etc/X11/default-display-manager returns /usr/sbin/gdm3. You can easily install lightdm: sudo apt install lightdm lightdm-gtk-greeter, and switch to lightdm with: sudo dpkg-reconfigure gdm3.
Linux shells are interactive interfaces that allow users to interact with the Operating System by executing commands and utilities.
neofetch
# Aliases
alias efish 'vim ~/.config/fish/config.fish'
alias evim 'vim ~/.vimrc'
alias cat 'bat'
alias rfish 'source ~/.config/fish/config.fish'
alias tobash="sudo chsh $USER -s /bin/bash && echo 'Now log out.'"

function fish_greeting
    fortune | cowsay
end

# PROMPT: https://fishshell.com/docs/current/cmds/fish_prompt.html
function fish_prompt -d "Write out the prompt"
    # This shows up as USER@HOST /home/user/ >, with the directory colored
    # $USER and $hostname are set by fish, so you can just use them
    # instead of using `whoami` and `hostname`
    printf '%s@%s %s%s%s > ' $USER $hostname \
        (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
end

Advanced Security

Linux terminal and administration

# Window Customization
window:   # Window dimensions (changes require restart)
  decorations: full # Borders and title bar
  startup_mode: Fullscreen 
scrolling: # Maximum number of lines in the scrollback buffer. It contains all the text that has been displayed on the screen.
  history: 10000
  multiplier: 3
background_opacity: .95 # Window opacity as a number from `0.0` (completely transparent) to `1.0` (opaque)
window.dynamic_title: true "# Allow terminal applications to change Alacritty's window title.
# Font Customization
font:
  normal:
    family: Hack NF
    style: Regular
  bold:
    family: Hack NF
    style: Bold
  italic:
    family: Hack NF
    style: Italic
  size: 12 # Text size
  offset:
    x: 0
    y: 0
  glyph_offset:
    x: 0
    y: 0
  use_thin_strokes: false
  debug:
    persistent_logging: false
draw_bold_text_with_bright_colors: true
# Colors (Gruvbox dark) https://github.com/eendroroy/alacritty-theme
colors:
  primary:
    background: '#000000'
    foreground: '#ebdbb2'
  normal:
    black:   '#282828'
    red:     '#cc241d'
    green:   '#98971a'
    yellow:  '#d79921'
    blue:    '#458588'
    magenta: '#b16286'
    cyan:    '#689d6a'
    white:   '#a89984'
  bright:
    black:   '#928374'
    red:     '#fb4934'
    green:   '#b8bb26'
    yellow:  '#fabd2f'
    blue:    '#83a598'
    magenta: '#d3869b'
    cyan:    '#8ec07c'
    white:   '#ebdbb2'
selection:
    semantic_escape_chars: ",?`|:\"' ()[]{}<>\t"
    text: '0xffffff'
    background: '0xdc3232'
    white:   '0xffffff'
    save_to_clipboard: true  # When set to `true`, selected text will be copied to the primary clipboard.
key_bindings:
  - { key: V, mods: Control, action: Paste } # Crtl + V = Paste
  - { key: C, mods: Control, action: Copy } # Crtl + C = Copy
  - { key: NumpadAdd, mods: Control, action: IncreaseFontSize } # Ctrl + "+", increase terminal's text size.
  - { key: NumpadSubtract, mods: Control, action: DecreaseFontSize }  Ctrl + "-", decrease terminal's text size.
  - { key: N, mods: Control, action: SpawnNewInstance }  # Ctrl + N, it creates a new instance.
  - { key: W, mods: Control, action: ToggleFullscreen } # Ctrl + W, toggle full screen terminal.
  - { key: F1, mods: Control, chars: "{" } # Barrier cannot type character that need Alt Gr key.
  - { key: F2, mods: Control, chars: "}" }
  - { key: F3, mods: Control, chars: "#" }
  - { key: F4, mods: Control, chars: "~" }
  - { key: F5, mods: Control, chars: "@" }
  - { key: F6, mods: Control, chars: "|" }

Cool Command Line Tools For Your Linux Terminal

Listening

If you talk to a man in a language he understands, that goes to his head. If you talk to him in his own language, that goes to his heart,Nelson Mandela.

Learn everything you can, anytime you can, from anyone you can; there will always come a time when you will be grateful you did,Sarah Caldwell.

Variety is the spice of life,Anonymous.

One of the basic strategies to significantly increase your language skills is to listen as much as possible.

What could you listen to? As the old saying goes, variety is the spice of life. It is all about listening as many subjects, topics, and sources as possible.

The web sites you might want to visit are: -free of charge, of course- 🙂

Inmersión

Si quieres aprender castellano, no hay nada mejor como viajar a España, Argentina o México o, mejor aún, tener una novia o un novio de habla hispana.

Sin embargo, estas opciones no están siempre disponibles. ¿Qué puedes hacer? Actualmente, Internet te proporciona los medios para sumergirte en el español sin salir de la comodidad de tu casa y practicándolo de forma gratuita o por una fracción del precio tradicional.