The following algorithm checks whether a number is prime:
Given a number n,loop over all numbers smaller than n and check whether they divide n.
If one of them divides n, answer no. Otherwise, answer yes.
Now, I have to analyse the number of division operations performed by the algorithm as a function of the length of its input in the following two cases:
1) The number is encoded in unary (i.e, 4 is 1111). How do I show that the number of divisions is polynomial?
2) The number is encoded in binary (i.e, 4 is 100). How do I show that the number of divisions is exponential?
Suppose we have
n1‘s strung together (notated1^n).nis the length of our input, obviously. We will divide all the integers from11,111, … ,1^(n-1)into1^n. How many numbers will you be dividing into1^n, as a function ofn? Is this a polynomial?Note that it takes
log_2(x)(log base 2 ofx) bits to representx, approximately, in binary. Also note that we will be performingx-2divisions (2,3,4,5, … ,x-1will be divided intox). So, forlog_2(x)bits we usex-2divisions. Suppose, instead, that we letnbe the size of our input. So we haven = log_2(x). How many divisions will we take, then, in terms of a function ofn?