0% found this document useful (0 votes)
24 views10 pages

Report

Uploaded by

ahmed.elragal02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views10 pages

Report

Uploaded by

ahmed.elragal02
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 10

Discrete Structures Project

by Ahmed Hassan Elragal

ID 7794

Problem 1
In this problem it is required to implement Fast Exponentiation in 3
versions two of them are the given naive1 and naive2 methods and the
iteration method, then time should be compared between the methods
and find the overflow value for each method.

We use modular exponentiation algorithm as shown in the previous


flowchart
How to calculate using modular exponentiation:
Step 1: Divide B into powers of 2 by writing it in binary
Start at the rightmost digit, let k=0 and for each digit:
If the digit is 1, we need a part for 2^k, otherwise we do not
Add 1 to k, and move left to the next digit

Step 2: Calculate mod C of the powers of two ≤ B


5^1 mod 19 = 5
5^2 mod 19 = (5^1 * 5^1) mod 19 = (5^1 mod 19 * 5^1 mod 19) mod 19
5^2 mod 19 = (5 * 5) mod 19 = 25 mod 19
5^2 mod 19 = 6
5^4 mod 19 = (5^2 * 5^2) mod 19 = (5^2 mod 19 * 5^2 mod 19) mod 19
5^4 mod 19 = (6 * 6) mod 19 = 36 mod 19
5^4 mod 19 = 17
5^8 mod 19 = (5^4 * 5^4) mod 19 = (5^4 mod 19 * 5^4 mod 19) mod 19
5^8 mod 19 = (17 * 17) mod 19 = 289 mod 19
5^8 mod 19 = 4
5^16 mod 19 = (5^8 * 5^8) mod 19 = (5^8 mod 19 * 5^8 mod 19) mod
19
5^16 mod 19 = (4 * 4) mod 19 = 16 mod 19
5^16 mod 19 = 16
5^32 mod 19 = (5^16 * 5^16) mod 19 = (5^16 mod 19 * 5^16 mod 19)
mod 19
5^32 mod 19 = (16 * 16) mod 19 = 256 mod 19
5^32 mod 19 = 9
5^64 mod 19 = (5^32 * 5^32) mod 19 = (5^32 mod 19 * 5^32 mod 19)
mod 19
5^64 mod 19 = (9 * 9) mod 19 = 81 mod 19
5^64 mod 19 = 5

Step 3: Use modular multiplication properties to combine the calculated


mod C values
5^117 mod 19 = ( 5^1 * 5^4 * 5^16 * 5^32 * 5^64) mod 19
5^117 mod 19 = ( 5^1 mod 19 * 5^4 mod 19 * 5^16 mod 19 * 5^32
mod 19 * 5^64 mod 19) mod 19
5^117 mod 19 = ( 5 * 17 * 16 * 9 * 5 ) mod 19
5^117 mod 19 = 61200 mod 19 = 1
5^117 mod 19 = 1

Pseudocode of iterative method


1.The function will take 3 variables a, array b and m
2.Calculating the length of array b and assigning it to variable length
3.power(p) = a mod m
4.for all numbers i from 0 to length-1
if value of array b equal i
then x=(x*p) mod m
p=(p*p) mod m
5.return x

Sample Runs
According to the previous sample runs we can see that the most time
efficient method is the iterative method in which we used the fast
modular exponentiation algorithm, followed by the naive 2 method
then lastly the naive 1 method.
The iterative method overflows first before naive1 and naive2 when
using a large value of power.

Both the naive1 and naive2 methods overflow if we use very large
numbers.
Problem 2
In this problem it is required to find prime numbers up to any given
limit using sieve of Eratosthenes algorithm.
Sieve of Eratosthenes is a simple and ancient algorithm used to find the
prime numbers up to any given limit and it is one of the most efficient
Let us take an example when n = 50. So we need to print all prime
numbers smaller than or equal to 50.
We create a list of all numbers from 2 to 50.

Acco
rding to the algorithm we will mark all the numbers which are divisible
by 2 and are greater than or equal to the square of it.

Now
we move to our next unmarked number 3 and mark all the numbers
which are multiples of 3 and are greater than or equal to the square of
it.
We continue this process and our final table will look like below:

So the prime numbers are the unmarked ones: 2, 3, 5, 7, 11, 13, 17, 19,
23, 29, 31, 37, 41, 43, 47.

Pseudocode
1.find primes up to N
2.For all numbers a : from 2 to sqrt(n)
IF a is unmarked THEN
a is prime
For all multiples of a (a < n)
mark multiples of as composite
3.All unmarked nummbers are prime!
Sample Runs

In this sample run we set the limit to 100 and find the prime numbers
from 0 to 100.
Problem 3

In this problem it is required to find prime factorization of a number


using trial division algorithm.
In trial division, the integer to be factorized (n) is divided by every
number less than it. It consists of continually testing if n is divisible by a
smaller number.
Pseudocode
1.function to check if a number is prime or not
if return 0= not prime
if return 1= Prime
in main function
2. get user input n to factorize the number
3. declare arrays factors[] and primes[]
4. for{
for all numbers i: from 2 to sqrt(n)
if i is prime primes[i]=i
else primes[i]=0
5.for all numbers i: from 2 to sqrt(n)
check if we found a prime number in array primes[i];
if found replace n=n/number then store it in factors array
check if n is prime if it is then store in factors array
6.repeat for all i;
}
7.print factors array

Sample Runs
In the above sample run we factorize 700 to its prime factors which are
22557

You might also like