Euclidian Algorithm
1. Multiples of the larger number( L) are subtracted from
Greatest common the smaller (S) until the remainder is less than S. So, we
Divisor have a quotient q0 and a remainder r.
Problem: How to find the greatest L=q0*S+r
common divisor of two natural
numbers? 2. Then, the smaller number (S) take the place of the larger;
The remainder (r) take the place of the smaller number;
L=S;
Solution: For this problem it’s S=r
recommended to use the Euclidian
Algorithm. It is named after the 3. After that, we do the same operation as the one
ancient Greek mathematician Euclid, described in the first step.
who first described it in his book L=qn*S+r
“Elements” . It is an example of one
of the oldest algorithms in common 4. We repeat step 2 and 3 while the remainder (r) is 0.
use. It can be used to
reduce fractions to their simplest 5. The greatest common divisor is the last remainder who is
form and is a part of many other different from 0 (r=0).
number-theoretic and cryptographic
calculations.
1
Sieve of Eratosthenes • Create a table with 10 columns and 10 rows and
Problem: Find all prime numbers between 1 complete it with the natural numbers from 1 to 100.
and 100 (or any other limit)
2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20
Solution: One of the efficient way to find all
prime numbers is the Sieve of Erasthostenes 21 22 23 24 25 26 27 28 29 30
which is an ancient algorithm for finding 31 32 33 34 35 36 37 38 39 40
all prime numbers up to any given limit.
41 42 43 44 45 46 47 48 49 50
It does so by iteratively marking
as composite the multiples of each prime, 51 52 53 54 55 56 57 58 59 60
starting with the first prime number, 2. 61 62 63 64 65 66 67 68 69 70
The earliest known reference to the sieve is 71 72 73 74 75 76 77 78 79 80
in Nicomachus of Gerasa’s “Introduction to 81 82 83 84 85 86 87 88 89 90
Arithmetic ” which describes and attributes 91 92 931 94 95 96 97 98 99 100
it to Eratosthenes of Cyrene, a Greek
mathematician.
3
Sieve of Eratosthenes
• Initially, let p equal 2, the smallest prime • Then, turn by turn we equal p to 3,5 and 7 (all
[Link] the multiples of p by prime numbers until √n
counting in increments • All numbers who are not marked are prime
of p from 2p to n(100), and mark them in the numbers.
list (these will be 2p, 3p, 4p, ...; the p itself
should not be marked).
2 3 4 5 6 7 8 9 10 2 3 4 5 6 7 8 9 10
11 12 13 14 15 16 17 18 19 20 11 12 13 14 15 16 17 18 19 20
21 22 23 24 25 26 27 28 29 30 21 22 23 24 25 26 27 28 29 30
31 32 33 34 35 36 37 38 39 40 31 32 33 34 35 36 37 38 39 40
41 42 43 44 45 46 47 48 49 50 41 42 43 44 45 46 47 48 49 50
51 52 53 54 55 56 57 58 59 60 51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70 61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80 71 72 73 74 75 76 77 78 79 80
81 82 83 84 85 86 87 88 89 90 81 82 83 84 85 86 87 88 89 90
91 92 931 94 95 96 97 98 99 100 91 92 93 94 95 96 97 98 99 100
4
• That was the ancient algorithm, • We will very if 2 is a deviser of n.
but in our case I will use the • If not, we will verify if every odd
same idea, but I will model it number from the interval [2; √n]
using math is a deviser of n
• I will use a series of numbers • If there is not any number which
starting with 2 and up to √n. is a divisor of n, then n is a
• Now, I will verify if in this interval prime number.
of integer numbers [2; √n] there
is at least a number who divides
n.
Perfect numbers
• Problem: identify if a number is • n is an integer number.
perfect or not.
• We compute all divisors of n:
• Solution: perfect number is a positive
integer that is equal to the sum of its
Dn ={1, d1 , d2 , …. n}
positive divisors, excluding the • Now we compute the sum of
number itself. So, to calculate whether them, wxcluding the number
or not a number is perfect, wee need itself:
to compute its divisors. After this we
will add them, and if the sum is the sum=1+ d1+ d2+….+dx
same as the number, it means te • If sum=n, then n is a perfect
number is perfect. number.