/* Determines whether entry is a prime number, if not, gives factors. Enter "1" to quit. */ /* By mattt chisholm - September 2, 1996 */ /* September 29, 1996: Made it stop checking for factors after it reaches halfway */ #include #include main() { int pa; /* integer to test */ int pb = 0; /* possible integer factors */ double pg; /* half the integer to test, decimal */ int ph; /* half the integer to test plus 1 for good measure, integer */ double pc; /* a, in decimal form */ double pd; /* b, in decimal form */ double pe; /* c divided by d */ double pf; /* next greatest integer to e*/ int pz; /* off switch: checks for a being 1 */ int px; /* not a prime switch */ pz = 0; while ( pz < 1 ){ /* when z is 1, it means quit */ if (pb > 0) { printf("\nThat took %d steps", pb); } pb = 2; /* start with 2 because everything is divisible by 1 */ printf("\n\nEnter number, 1 to quit > "); scanf("%d", &pa); /* get number to test */ px = 0; /* we don't know if it is a prime yet */ if ( pa == 1) { /* did the user enter a 1 to quit ? */ pz = 1; } pg = (pa)/(2); /* factor is never going to be more than half of multiple */ ph = ceil(pg) + 1; /* add one for good measure (without, thinks 4 is prime) */ while ( pb < ph ) { /* start dividing by numbers less than test number */ pc = pa; /* set the integer a to the decimal c */ pd = pb; /* set the integer b to the decimal d */ pe = (pc) / (pd); /* divide */ pf = ceil(pe); /* get the closest integer to e*/ if ( pf == pe ) { /* if the closest integer is the same, it divides evenly, and is not a prime */ printf("\n%d is divisible by %d.", pa, pb); /* tell us about it */ px = 1; /* remember, this is not a prime */ } pb = pb + 1; /* move on to the next number to divide a by */ } if ( px == 0) { /* was this ever designated "not a prime?" */ printf("\n%d is a prime.", pa); /* tell us about it */ } } printf("\nthat took %d steps", pb); printf(" Either that, or it isn't a prime. Thank you for usingPrimeFinder.\n"); return(0); }