关键词 > Python代写
Project 2 – Old Math
发布时间:2022-02-07
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
Project 2 – Old Math
Objective: The aim of this project is to familiarize you with the use of Boolean logic, branching statements and loops.
Description:
For this assignment, you will be implementing the so-called “Russian Peasant” or “Ancient Egyptian” method for multiplication. It looks a little odd, but just think of it as an algorithm, a recipe for doing multiplication in a way other than what you learned in grade school.
For a detailed description of how the algorithm works, please visit the links :
· http://en.wikipedia.org/wiki/Ancient_Egyptian_multiplication
· http://www.cut-the-knot.org/Curriculum/Algebra/PeasantMultiplication.shtml
The algorithm is as follows. If A and B are the 2 integers (only integers) to be multiplied, we repeatedly multiply A by 2 and divide B by 2, until B can’t be divided any further, that is until its value becomes 0 (remember, this is integer division). During each step, whenever B is an odd number, we add the corresponding A value to the product we are generating. In the end, the sum of the A values that had corresponding odd B values is the product. Get it?
Here’s an example:
If the two integers to be multiplied are 34 and 19, the operations would be:
A B Comment |
|
34 19 add A to the product, B is odd |
68 9 add A to the product, B is odd |
136 4 ignore this A value, B is even |
272 2 ignore this A value, B is even |
544 1 add A to the product, B is odd |
Sum up all the A values that had odd B values and you get:
34 + 68 + 544 = 646 => Final Product
Specifications
1. Prompt the user for two integers. Only integers are allowed and no error checking is required.
2. In calculating the product, show how the algorithm proceeds. See the example output at the end of this document. This ensures that you are using the required algorithm.
3. After the product is calculated, please indicate the sign of the product (positive, negative or zero)
4. After the result is printed, your program will ask if you wish to perform another calculation. If ‘y’ or ‘Y’ is entered, another calculation can be performed. If ‘n’ or ‘N’ is enter the program will quit and print a nice message. Any other input will print an error note and end the program.
5. The program should give correct results for all integers i.e. positive as well as negative numbers.
6. Constructs that must be in the final program include:
a. input to prompt the user and conversion to int
b. print to print results
c. at least one branching mechanism (if statement)
d. at least one loop (while loop)
e. Boolean logic
Project Requirements:
You must follow all of the good programming practices discussed in class:
· Comment your code thoroughly.
· Indent your code appropriately.
· Use meaningful variable names.
· Provide the user with understandable prompts and instructions.
· Make sure your name is included in comments at the top of your code.
If this is not done, points will be deducted from your program and it will be impossible to earn an ‘A’.
Submission: Using the Canvas assignment feature, you should submit the source code (.py file). Make sure you click to submit your assignment after uploading the file attachment!
Notes and Hints:
1. Start with positive numbers and get the algorithm to work.
2. Make sure to use integer division when dividing by 2.
3. Once you get positive numbers working, handle the case for negative numbers. Hint – Multiply a negative number by -1 and set a boolean variable that indicates a negative was entered. Multiply the final answer by -1 as needed.
4. Make sure to handle the case of either number being zero. Print out a special message for this case.
Sample Program Dialog
Please the first number: 22
Enter the second number: 33
A = 22 and B = 33
B was odd, we add A to make the product: 22
A = 44 and B = 16
A = 88 and B = 8
A = 176 and B = 4
A = 352 and B = 2
A = 704 and B = 1
B was odd, we add A to make the product: 726
Product is positive
The product of the two numbers is: 726
Do you want to continue?(y/n)y
====================================================
Please the first number: -22
Enter the second number: 33
A = -22 and B = 33
B was odd, we add A to make the product: -22
A = -44 and B = 16
A = -88 and B = 8
A = -176 and B = 4
A = -352 and B = 2
A = -704 and B = 1
B was odd, we add A to make the product: -726
Product is negative
The product of the two numbers is: -726
Do you want to continue?(y/n)Y
====================================================
Please the first number: -22
Enter the second number: -33
A = -22 and B = -33
B was odd, we add A to make the product: -22
A = -44 and B = 16
A = -88 and B = 8
A = -176 and B = 4
A = -352 and B = 2
A = -704 and B = 1
B was odd, we add A to make the product: -726
Product is positive
The product of the two numbers is: 726
Do you want to continue?(y/n)YES
Bad input, quitting