CMSC 104, Section 01, Fall 2016
Problem Solving and Computer Programming
Tuesday, November 29th @ 5:30 PM
This project is graded with a 75 point max score.
To become more familiar with:
- Modular Programming
- Simple Functions that return one value
- Functions that return void
- (35 points)
Prime numbers have been studied by mathematicians for centuries. A prime number is an integer that has no divisors other than 1 and itself (for example, the integers 2, 3, 5, 7, and 11 are prime). Write a modular program called campusid_prime.c that finds the smallest divisor of a number or determines that the number is prime.
To determine whether an integer n is prime, your program should test integers that are smaller than n until it finds an integer that is a divisor of n. The integer values to be tested should be limited to 10,000 or less.
The program should prompt the user for the integer n to be tested. Note that n must be greater than 1 to be valid and less than or equal to 10,000. Error checking should be included as well as repetition to get valid input from the user.
Your program must contain two functions in addition to main; one for finding the smallest divisor and one to determine if a number is even. These functions should be named FindDivisor and IsEven respectively. FindDivisor should take one integer as input and return one integer that is the smallest divisor of the input. The IsEven function should take one integer as input and return 1 if the integer is even and 0 otherwise.
Note that if any number is even, then its smallest divisor other than 1 is 2. If a number is not even,
then the odd number starting from 3 should be tested to see if they divide the number perfectly. Your
program should take this information into account.
Your program should print the smallest divisor of a given number, along with that number, if the number
is not prime. Otherwise, your program should print the given number and that it is prime.
- (40 points)
Factoring a number is another interesting mathematical problem. Write a program called
campusid_factor.c that takes an ineger value and factors that number into its prime factors.
The program should contain a new function called Factor and should incorporate both functions
from part 1, FindDivisor and IsEven. The Factor function should take
one integer as input, and return nothing else (i.e. void).
The FindDivisor function returns the smallest divisor of a number. In order to find all
of the factors, FindDivisor should be called iteratively on the result of the number divided
by its smallest divisor. As an example, consider the number 84:
84 = 2 x 42
42 = 2 x 21
21 = 3 x 7
7 = 1 x 7
Therefore, the factorization of 84 is
84 = 2 x 2 x 3 x 7
The program should ask the user for an integer to factor. The input should be checked for validity and
re-entered if incorrect. You should ensure that the input number is greater than 1 and less than 10,000.
The output of the program should be the input number and the number's factorization, as seen above.
- (10 points EXTRA CREDIT)
A twin prime is a prime number that is either two less or two more than another prime number -- for example, the twin prime pair (41,43). Write a program, campusid_twinprime.c that prompts the user to enter a minimum and maximum integer in the range 1 and 10,000. If the user enters an invalid range, they should be prompted again. Once a valid range has been entered, the program should print all the twin prime pairs in the given range. The following is example output for the minimum value 4 and maximum value 20:
When you have finished, submit your campusid_prime.c
, and (optionally) campusid_twinprime.c
files using the following commands:
curl -L -F file=@campusid_prime.c http://userpages.umbc.edu/~reicht1/classes/cmsc104/fall16/submit.php
curl -L -F file=@campusid_factor.c http://userpages.umbc.edu/~reicht1/classes/cmsc104/fall16/submit.php
curl -L -F file=@campusid_twinprime.c http://userpages.umbc.edu/~reicht1/classes/cmsc104/fall16/submit.php
Additionally, email your campusid_prime.c
, and (optionally) campusid_twinprime.c
files as an attachment to firstname.lastname@example.org
using the subject line Project Submission - 7 - Lastname, Firstname
, replacing Lastname, Firstname
with your name.
Last Modified: November 22nd, 2016 9:18 PM