Understanding an algorithm

49 Views Asked by At

enter image description here

I want to understand the above algorithm. My solution says that the algorithm should return $0$ if $n$ is a prime or 1. Otherwise it returns the smallest (positive) non-trivial divisor.

Lets consider an example: Let $n=5$

We assign $d=2$ and $q=5$ in the first steps. It holds that $5>2$ so we assign $q=\frac{5}{2}$

Now it holds that $\lceil 2.5 \rceil=3 \neq 2.5$ so we return 2

But $5$ is a prime, so it should be returned $0$. What is wrong?

Sorry, if this question is easy. Iam pretty new to algorithms.

1

There are 1 best solutions below

0
On

The algorithm says to return $d$ if $\lceil q\rceil=q$. In your case, $\lceil q\rceil\ne q$, so we proceed to the "else" part of the statement instead of returning $d$.