Scenario

When we want to test whether a number \(p\) is a prime, we need to divide \(p\) with all numbers between \(2\) and \(p-1\). If none of these numbers divides \(p\), then we can conclude that p is a prime.

In the precess, we can use a state variable, e.g. isprime, initially \(1\) (true), to denote that \(p\) is probably a prime. 

#include <stdio.h>

int main()
{
	int p=101;
	int i;
	int isprime = 1;		        //1 means as if p is probably a prime 
	
	//scanf("%d",&p);
	for (i = 2; i < p; i++)
		if (p % i == 0) isprime = 0;    //if i divides p, then p is NOT a prime
	
	if (isprime)			        //isprime == 1 means it IS a prime
		printf("%d is a prime.\n",p);
	else
		printf("%d is a not prime.\n",p);		
			
	return 0;
}

In many similar scenarios, we may use isvalid to denote a possible solution is propably a valid solution.

You have no rights to post comments