You are on page 1of 2

Assignment No.

01
Total Marks: 20
SEMESTER Fall 2019
CS502- Fundamentals of Algorithms Due Date: 13/11/2019
Assignment Statement:

In mathematics Prime is a number which can be only divisible by 1 and itself. Examples of prime numbers
are 2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 etc. When these numbers get large, then it is very difficult to
manually identify them as to be prime or not prime. For example the numbers like, 443 and 44371 cannot
easily identify as Prime.
There may be many algorithms which can be written for the identification of a number to be prime or not
prime.

Question No 01: (Marks: 10)


You are required to design (write) a simple algorithm (Only Pseudo code) which can identify a number to be
Prime or Not Prime.
Answer:
Algorithm Identifying a Prime Number:

CHECK_PRIME(int n)
1 do IsPrime←true
2 for i←2 to n-1
3 do
4 if (n% i==0)
5 then IsPrime←false; break
6 if(n>1&&IsPrime==true)
7 then output “Prime”
8 else output “Not Prime”
Question No 02: (Marks: 10)
You are required to calculate (Step by Step) the worst case time complexity T(n) of the algorithm designed in
Question No. 01.
Answer:
Worst case time complexity T(n) of the CHECK_PRIME algorithm:
Assume that the input size is n-2 as the first and the last number which are (1 and n) will not be included and
for the running time we will count the number of times the loop is executed, which is n-2 in the worst case.
Therefore it is evident that the worst case time is Θ(n).

You might also like