
Many of the examples we've done in class have involved dealing with prime numbers: decide whether a number is a prime, find the next prime, print the first 100 primes, etc. The concept of a prime number is fundamental in mathematics, particularly in number theory. In fact, the fundamental theorem of arithmetic states that every positive integer (greater than 1) can be represented uniquely (where order doesn't matter) as a product of one or more primes.
0: Exit. 1: Is N a prime? 2: List the first N prime numbers. 3: Display the Nth prime number (1-based). 4: Find first prime after N. 5: Factor N. 6: Display this help message.You should validate that the command is legal. All commands (except 0 and 6) require an additional piece of information. For those commands, you'll ask the user to specify an integer value N. You will then compute and print the answer using N, or print an error message and the menu. Command 0 exits the program; command 6 asks for information.
It's suggested that you leave the commands as strings, rather than converting them to integers. See the discussion below in the Programming Hints section.
For the commands that require N, you should validate that as well: it must be an integer. There may also be a constraint that it be larger than some K. For commands 1 and 3, N must be positive; for command 2, N must be non-negative; for command 5, N should be at least 2; for command 4, there is no constraint. (I used a single function containing a loop to input and validate N for all of those five commands, passing the constraint value as a parameter.)
If a legal command (and possibly N) is entered, perform the command and display the answer. Within the slides are several functions on primes including isPrime and findNextPrime. Use these; you don't have to re-code them. You'll have to code some others. The hardest one is almost certainly giving the prime factorization. See the section below on that. Below are some output examples.
> python PrimeFactory.py Welcome to the Prime Factory! The following commands are available: 0: Exit. 1: Is N a prime? 2: List the first N prime numbers. 3: Display the Nth prime number (1-based). 4: Find first prime after N. 5: Factor N. 6: Display this help message. Please enter a command: 10 Command 10 not recognized. Try again! The following commands are available: 0: Exit. 1: Is N a prime? 2: List the first N prime numbers. 3: Display the Nth prime number (1-based). 4: Find first prime after N. 5: Factor N. 6: Display this help message. Please enter a command: 0 Thanks for visiting our factory! Goodbye. > python PrimeFactory.py Welcome to the Prime Factory! The following commands are available: 0: Exit. 1: Is N a prime? 2: List the first N prime numbers. 3: Display the Nth prime number (1-based). 4: Find first prime after N. 5: Factor N. 6: Display this help message. Please enter a command: 1 You've asked if N is a prime. What is N? -10 Illegal value. Try again! What is N? 1 1 is not prime Please enter a command: 1 You've asked if N is a prime. What is N? -10 Illegal value. Try again! What is N? 1234567 1234567 is not prime Please enter a command: 2 You've asked to display the first N prime numbers. What is N? -15 Illegal value. Try again! What is N? 15 The first 15 primes are: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] Please enter a command: 3 You've asked for the Nth prime number. What is N? 0 Illegal value. Try again! What is N? -5 Illegal value. Try again! What is N? 15 The 15th prime is: 47 Please enter a command: 4 You've asked for the first prime number after N. What is N? 100000000 The first prime after 100000000 is: 100000007 Please enter a command: 4 You've asked for the first prime number after N. What is N? -1000 The first prime after -1000 is: 2 Please enter a command: 5 You've asked for the prime factorization of N. What is N? 123456789 The prime factorization of 123456789 is: [3, 3, 3607, 3803] Please enter a command: 5 You've asked for the prime factorization of N. What is N? 1 Illegal value. Try again! What is N? -10 Illegal value. Try again! What is N? 3803 The prime factorization of 3803 is: [3803] Please enter a command: 6 The following commands are available: 0: Exit. 1: Is N a prime? 2: List the first N prime numbers. 3: Display the Nth prime number (1-based). 4: Find first prime after N. 5: Factor N. 6: Display this help message. Please enter a command: 0 Thanks for visiting our factory! Goodbye. > python PrimeFactory.py Welcome to the Prime Factory! The following commands are available: 0: Exit. 1: Is N a prime? 2: List the first N prime numbers. 3: Display the Nth prime number (1-based). 4: Find first prime after N. 5: Factor N. 6: Display this help message. Please enter a command: info # See Programming Tips below The following commands are available: 0: Exit. 1: Is N a prime? 2: List the first N prime numbers. 3: Display the Nth prime number (1-based). 4: Find first prime after N. 5: Factor N. 6: Display this help message. Please enter a command: quit # See Programming Tips below Thanks for visiting our factory! Goodbye. >Notice that command 3 displays the Nth prime. The output could be better. E.g., it would display the "1th", "2th", and "3th" primes, instead of the "1st", "2nd", and "3rd". You're welcome to fix that if you like, but you don't have to. (It's a bit harder than you might think. You'd want to produce "23rd", but also "13th", so it depends on more than just the last digit.)
set the list of factors to the empty list
set d to 2
while num is not prime and num > 1:
while d divides num:
add d to the list of factors
replace num by num // d
set d to the next biggest prime
at this point either num is prime or n is 1:
if num is not equal to 1, add it to the list of factors
return the list of factors
You should play with this algorithm on paper until you're very
confident how it works. Then code it.
Your file must compile and run before submission. It must also contain a header with the following format:
# Assignment: Project 2 # File: PrimeFactory.py # Student: # UT EID: # Course Name: CS303E # # Date Created: # Date Last Modified: # Description of Program:
Remember: if the command is stored in variable comm, comm.lower() doesn't change comm. You'd want to do something like:
commLower = comm.lower()Also, when using a menu driven system like the one you're coding here, I find it annoying when I can't remember either the command to exit, or the command to get the help menu. In my own implementation of the Prime Factory, I allowed 'exit', 'quit', 'leave' as alternatives to exit the program, and allowed 'help' and 'info' as commands that would print the help menu. I typically don't publicize such commands because they're outside the "official" interface. But they make things more user-friendly. You don't have to do this in your program, but you're welcome to if you like. The TAs will only test the official interface.
Choose the correct type: The user inputs a number (0, 1, ..., 6) but it's read as a string. You could convert it to a int, but there's no reason to do that because you never do any arithmetic using these inputs. Just leave them as strings.
There's another reason not to convert for this assignment: If you want to add alternative commands, like "exit", "info", etc. then you don't need to hassel with dealing commands that can be either strings or integers. I defined the following global constant:
LEGAL_COMMANDS = [EXIT, IS_PRIME, LIST_N_PRIMES, SHOW_NTH_PRIME,
FIRST_PRIME_AFTER, FACTOR, HELP,
'exit', 'quit', 'help', 'leave', 'info']
where EXIT, IS_PRIME, etc are also global constants for the 7 numeral
commands. Having this global constant makes it easy to add new
ones. A command is syntactically legal if and only if it's in the list.Integer literals: When Python is reading an integer literal, as in an input statement, you can use underscores to make the number more readable, e.g., 100_000_000. Python simply discards the underscores inside an integer literal. Use them anywhere within the number except at the beginning or end, and you can't have two in a row. So 1_2_3 is legal, but _123, 123_ and 1__23 are not. However, if you're using .isdigit() to validate input to test if a string represents an integer, be careful. Because "1_000".isdigit() is False; to Python "1_000" is just a string, not an integer. However, int("1_000") works fine.