go to previous page   go to home page   go to next page

Answer:

See below.


Break Removed

Here is a version that uses early returns (one of them inside the loop):

Here is a strictly structured version:

Probably the early-returns version is best. The structured version makes use of how C treats any non-zero as true so n%trial counts as true when trial does not divide n evenly.

If you truely need a prime number tester, there are better algorithms to follow than the one above. Our function tests if the number is divisible by 2, and if not tries to divide by 3, 4, 5, 6, ... But there is no need to try even divisors since we know the number is not even, and there is no need to try multiples of 3, and so on. The Sieve of Eratosthenes makes use of this idea.


QUESTION 13:

What about speed? Does using unstructured code speed up a program?