How can I be certain that my code is flawless? [duplicate]
- by David
This question already has an answer here:
Theoretically bug-free programs
5 answers
I have just completed an exercise from my textbook which wanted me to write a program to check if a number is prime or not.
I have tested it and seems to work fine, but how can I be certain that it will work for every prime number?
public boolean isPrime(int n)
{
int divisor = 2;
int limit = n-1 ;
if (n == 2)
{
return true;
}
else
{
int mod = 0;
while (divisor <= limit)
{
mod = n % divisor;
if (mod == 0)
{
return false;
}
divisor++;
}
if (mod > 0)
{
return true;
}
}
return false;
}
Note that this question is not a duplicate of Theoretically Bug Free Programs because that question asks about whether one can write bug free programs in the face of the the limitative results such as Turing's proof of the incomputability of halting, Rice's theorem and Godel's incompleteness theorems. This question asks how a program can be shown to be bug free.