Classwork 7 - UMBC-CMSC104/General GitHub Wiki

Finding Prime Numbers

There is a naive algorithm to find prime numbers that's simple and straightforward to implement. The algorithm is as follows:

  • Take a number X
  • Test every number from 2 to X/2 (inclusive) to see if it divides cleanly into X (that is, no remainder).
  • If any of those numbers divide cleanly into X, then X is not prime.

The assignment

Name your code prime_finder.c.

For this assignment, we're going to be reading in a number from the user, and then testing every number from 3 to the user number and determining if that number is prime.

You should write a function to determine whether or not a function is prime. The function should return a flag to this effect.

Welcome to classwork 7!
Please enter the upper bound of numbers to test for primality: 13
2 is prime!
3 is prime!
5 is prime!
7 is prime!
11 is prime!
13 is prime!

Also, try this on 30,000 and capture the output. You can do this by typing:

echo 30000 | ./prime_finder > prime_output.txt

where prime_finder is the name of your executable.

To submit the code and the output, type:

submit cs104_wilson cw07 prime_finder.c prime_output.txt