Craft 1

Beautiful Code
Ka-Ping Yee

Make sure you understand the material in Explore 1 before you do this assignment.

Your mission is to write a program to prime-factorize numbers. (Yeah, i know the subject matter is kind of boring, but we have to start somewhere. The point of this exercise is just to get your feet wet writing a working program. I promise things will get more interesting as the semester continues.)

Your program should read numbers from the keyboard and print their prime factorizations. It should keep accepting numbers until the user enters a blank line. Try to make the program behave just like this:

cory% python
Number: 34234
 34234 = 2 * 17117
Number: 938
 938 = 2 * 7 * 67
Number: 981
 981 = 3 * 3 * 109
Number: 9480
 9480 = 2 * 2 * 2 * 3 * 5 * 79

(The lines that say cory% at the top and bottom are from the Unix shell, not the program. I just showed them in this example to illustrate that the program finishes when the user enters a blank line.)

You don't have to write this program to be especially efficient, or to handle any number with more than five digits. Make your program print an insulting message when the user enters a number that's too big.

cory% python
Number: 3456
 3456 = 2 * 2 * 2 * 2 * 2 * 2 * 2 * 3 * 3 * 3
Number: 34567890
 The number of digits shall be at most FIVE.  8 is RIGHT OUT.
Number: 34567
 34567 = 13 * 2659

A trailing comma at the end of the print statement will prevent it from automatically printing a newline.

For getting input, use the raw_input() function, which will read one line of input from the keyboard and return a string. You can assume that the input will always be either a valid integer or a blank line.

Try to make your code as clean and readable as possible. Choose meaningful names for your variables, and try to express operations in simple ways. If you want to add comments, use the hash sign (#). Anything from the hash sign to the end of the line is considered a comment. (Sometimes comments are nice, but don't assume that making code readable is just a matter of adding lots of comments: the code itself should be readable, too.)

When you're done, you can submit your program using this page.

Once you log in at the top of the page, you'll see a form here.

This assignment is due at 6 pm on Monday 27 January. You get a bonus if it's correct and submitted at least 24 hours early.