0% found this document useful (0 votes)
17 views3 pages

Assignment 3

Uploaded by

psw3694
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Assignment 3

Uploaded by

psw3694
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Spring 2023

Programming Assignment 03
-
While loops

Instructions

This programming assignment consists of 2 programming exercises.

You have to:


1. create Python files on your computer (be careful of filenames)
2. edit them according to the assignment
3. verify on your computer that it works (run them and check the output in the shell)
4. upload the files to Gradescope (upload directly the .py files, not a .zip file)
5. check the auto-grader report on Gradescope
6. go back to step 2 if necessary

The auto-grader will evaluate your code for a few test-cases. If some test-cases fail, the
auto-grader should show you what is your code output, and what is the expected output.

The auto-grader will give you a score based on the test-cases, but a grader will manually
evaluate your coding style after the deadline. Style represents 30% of the coding assignment
grade.

Note:
The use of concepts that were not yet covered in class is prohibited.

Introduction to Computer Programming Page 1 of 3


Spring 2023

Exercise 1 - Krishnamurthy number


A Krishnamurthy number is a number which sum of the factorial of its digits is equal to the number
itself.

For example: Let us consider the number 145.


Factorial sum = 1! + 4! + 5! = 1 + 24 + 120 = 145. Therefore 145 is a Krishnamurthy number.
Other examples include: 1, 2, 40585.

Write a program (in the file exercise1.py) that does the following:
• asks the user to input an integer.
• computes whether the number is a Krishnamurthy number.
• then finally prints the result.
Note: You are not allowed to use the built-in function math.factorial.

Sample examples (the user input is in red, the printed output is in blue):

Enter a number: 145


145 is a Krishnamurthy number

Enter a number: 103


103 is not a Krishnamurthy number

Enter a number: 2
2 is a Krishnamurthy number

Enter a number: 75
75 is not a Krishnamurthy number

Enter a number: 40585


40585 is a Krishnamurthy number

Introduction to Computer Programming Page 2 of 3


Spring 2023

Exercise 2 - Square Root


Write a program (in the file exercise2.py) that does the following:
1. asks the user to input a positive value X
2. then, prints the approximated value Y of the square root of X according to Newton’s
method, up to the third decimal point

Newton’s method for square root



Algorithm for estimating Y ≈ X:
1. Initialize Y to X/2
2. Loop until Y 2 is close enough to X:
• Update Y with the average of Y and X/Y
• Stop when Y 2 distance to X is less than 0.001

Example for X = 10

Steps for estimating 10:
1. Y = 5
Y 2 − X = 15 → need update
2. Y = (5 + 10/5)/2 = 3.5
Y 2 − X = 2.25 → need update
3. Y = (3.5 + 10/3.5)/2 = 3.178571428571429
Y 2 − X = 0.10331632653061362 → need update
4. Y = 3.162319422150883
Y 2√− X = 0.00026412771269335167 → can stop
Result: 10 ≈ 3.162

Restrictions

You are not allowed to use the math module.


You have to implement the Newton’s method detailed above.

Sample examples (the user input is in red, the printed output is in blue, and the prompt is in black):
Enter a number: 78 Enter a number: 12.3 Enter a number: 9
Square root: 8.832 Square root: 3.507 Square root: 3.000

Introduction to Computer Programming Page 3 of 3

You might also like