Prime numbers

positive integer only divisible by itself and 1

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...


prompt
read num
if its prime				if (is_prime(num))
  say 'Prime'				  
else					test if arg is prime or not
  say 'Not prime'		        return true or false


prime== not divisible by any number between 2 and itself-1
	See if not divisible by any number:
	loop i from 2 to num-1
	   if num% i == 0 // i evenly divides the num
	      Not prime

bool is_prime (int num) {
  int i;
  for (i=2; i<num; i++)
    if (num%i == 0)
      return false;   // not prime, quit function.
  return true;  // went thru all i of loop, 
                //none divided into num, so is prime
}

Program that determines if a number is prime prime.cpp

Program that determines all primes to some limit. primeall.cpp

Program that determines if a number is prime and how long it takes the CPU to determine that. primetime.cpp


©David Wills