You are on page 1of 1

Weekly Challenge 15: Complexity Classes

CS 212 Nature of Computation


Habib University
mk07218
Fall 2023

1. Checking Primality
Explain succinctly why the language recognized by the following Turing machine, M , does
not belong to P. Assume the input to be a binary representation of a number.
M = On input n:
1. Check if 2 divides n, if so reject.
2. Repeat Step 1 for all numbers less than n. That is, check if 3 divides n. If so reject,
otherwise check if 4 divides n, if so reject, and so on.
3. If all numbers less than n have been checked, accept.

Solution: A language belongs to P if it is recognized by a single-tape deterministic Turing


Machine in polynomial time.
We will analyze the algorithm given to us so that we can determine whether L(M) belongs
to P or not. Considering that the length of the input n (basically the binary representation)
is r bits. The algorithm asks us to check for every number less than n, whether it divides n
or not. In total, M would have to check and operate on each combination i of r bits where
1 < i < 2r . Hence, in the worst case, if we reach step 3, we will end up with O(2r ). As
the time complexity of L(M ) is not expressible in polynomial O(nk ) (where k is a positive
integer), if falls outside P , and would be considered in exponential time, as with the increase
in the number of bits, the computation time would grow exponentially.
NOTE: This algorithm is a classic example of brute force where we have to go through
every possible solution to find our answer. Had n be represented in unary, the complexity
would have been O(n) but as n is represented in binary, the time complexity becomes O(2n ),
which is exponential.

You might also like