Application using the C language
for a C and UNIX Class
[ b a
c k ]
#include "primes.h"
/**********************************************************
This driver has three operations all involving prime
numbers. This program displays a menu and depending
upon the choice of the user, involkes the approporiate
function. User enters 0 to quit the loop and program.
Author: William martin
Compiled: 1:46:33 AM 2/24/2000
**********************************************************/
/*********************************************************
FUNCTIONS
**********************************************************/
void primes(int howMany, int startAt);
void howManyPrimes(void);
void howManyFrom(void);
void goldbachsConjecture(void);
void putHeader(void);
void selectionException(void);
/*********************************************************
MAIN
**********************************************************/
int main(void) {
int ans;
while (ans != 0) {
putHeader();
scanf("%d", &ans);
switch (ans) {
case 0 :
printf("\n Have a nice day !");
break;
case 1 :
howManyPrimes();
continue;
case 2 :
howManyFrom();
continue;
case 3 :
goldbachsConjecture();
continue;
default :
printf("\nWRONG SELECTION: Select1,2,3 or 0 to quit!\n\n\n");
continue;
}
}
return 0;
}
/**********************************************************
The primes function prints out primes.
Input: integer how many and where to start at.
Output: formatted list of primes
Precondition: user should only use integer input. other
characters involk an infinate loop
***********************************************************/
void primes(int howMany, int startAt ) {
int i, temp = startAt;
for (i = 0; i != howMany; ++i) {
printf("%5d:", i+1);
while (!is_prime(temp))
temp += 1;
printf(" %d\n", temp);
temp += 1;
}
}
/**********************************************************
This function prompts the user for the number of first
primes that they would like printed.
Input: keyboard
Output: sends user defined integer to function primes.
Precondition: user should only use integer input. other
characters involk an infinate loop
***********************************************************/
void howManyPrimes(void) {
int i, s = 0;
printf("\nHow many first primes do you want to see ==> ");
scanf ("%d", &i);
printf("\n\n");
primes(i, s);
return 0;
}
/**********************************************************
This function prompts the user for the number of first
primes and at what index to start at.
Input: keyboard
Output: sends user defined integers to function 'primes'.
Precondition: user should only use integer input. other
characters involk an infinate loop
***********************************************************/
void howManyFrom(void) {
int i, s;
printf("\nHow many first primes do you want to see ==> ");
scanf ("%d", &i);
printf("\nWhere do you want to start ==> ");
scanf ("%d", &s);
printf("\n\n");
primes(i, s);
return 0;
}
/**********************************************************
This function gives the Goldbachs conjecture and tests it.
Input: keyboard - user types the upper and lower bound numbers
to test the conjecture.
Output: prints proof that the conjecture is true.
[EXAMPLE: 700 = 17 + 683 ]
***********************************************************/
void goldbachsConjecture(void) {
int i, j, startP, finishP, testNumber = 0;
printf("\n\n\n"
"\nGOLDBACH'S CONJECTURE:"
"\nEvery even number n > 6 is the sum of two odd primes.\n"
"\nTo test Goldbach's Conjecture - please submitt starting "
"\nand ending bounds. The numbers between the bounds will"
"\nattempt to prove that the conjecture is true."
"\n\n"
"Enter starting point ==> ");
scanf ("%d", &startP);
printf("\nWhere do you want to end ==> ");
scanf ("%d", &finishP);
printf("\n\n");
if ((startP%2) != 0)
++startP;
for (i = startP; i <= finishP; i += 2) {
for (j = 0; !is_prime(testNumber); j++){
if (is_prime(j))
testNumber = (i - j);
}
j--;
printf("%5d = %3d +%3d\n", i, j, testNumber);
testNumber = 0;
}
}
/**********************************************************
This function displays the menu.
Input: none
Output: prints instructions.
***********************************************************/
void putHeader(void){
printf("\n"
"===================================================== \n"
" PRIMES \n"
"===================================================== \n"
" \n"
"You have three choices to test for primes: \n"
" \n"
" 1 - to specify the number of first primes to view.\n"
" 2 - to specify the index and number of primes. \n"
" 3 - to try Goldenbach's conjecture. \n"
"===================================================== \n"
" \n"
"Make a selection, (1,2,3) or 0 to quit ==> ");
}
/********************************************************************/