You are on page 1of 3

Programming #0

SSP

Programming Assignment #0
When you are ready to submit your solutions, email all your source les (i.e. .py les) to the TAs (socorroTAS@gmail.com) with the subject programmingHW0. Be sure to include your name, the date, and a description of your program in comments at the top of your code. Please submit a separate le for each exercise and name each le according to the exercise. Specically, the le for exercise 1 should be called ex1 firstname lastname.py, the le for exercise 2 should be called ex2 firstname lastname.py, and so on. Feel free to discuss these exercises with other students, but refrain from directly copying code from anyone else. Also, the solutions to these exercises may be available online. Please be careful and keep the Code of Honor in mind as you complete this assignment. To be pedantic, you are permitted to use online resources to understand general features of the Python language, but you should not consult any online sources about the specic subjects of the exercises. Exercise 1. Write two functions, degToRad and radToDeg. The former should take an angle in decimal degrees and return it in radians and the latter should take an angle in radians and return it in decimal degrees. You can use Pythons built-in math.degrees and math.radians to verify your solution. Exercise 2. A person is eligible to be a US senator is they are at least 30 years old and have been a US citizen for at least 9 years. To be a US representative these numbers are 25 and 7, respectively. Write a program that asks the user for a persons age and years of citizenship and outputs that persons eligibility for the Senate and House. Exercise 3. A formula for computing Easter in the years 1982-2048, inclusive, is as follows. Let a = year%19, b = year%4, c = year%7, d = (19a + 24)%30, e = (2b + 4c + 6d + 5)%7. The date of Easter is March 22 + d + e (which could be in April). Write a program that asks the user for a year, veries that it is in the proper range, and then prints out the date of Easter that year. Exercise 4. Write a program to sum a series of numbers entered by the user. The program should rst prompt the user for how many numbers are to be summed. It should then input each of the numbers and print a total sum.

Programming #0

SSP

Exercise 5. Write a program that estimates the value of by summing the terms of this series: 4/1 4/3 + 4/5 4/7 + 4/9 4/11 + The program should prompt the user for n, the number of terms to sum, and then output the sum of the rst n terms of this series. Have your program report the accuracy of the approximation (e.g. subtract the approximation from the value of math.pi). Exercise 6. You have seen that the math library contains a function that computes the square root of a number. In this exercise, you are to implement an algorithm for computing square roots. One way to solve this problem is to use a guess-and-check approach. You rst guess what the square root might be and then see how close your guess is. You can use this information to make another guess and continue guessing until you have found the square root (or a close approximation to it). One particularly good way of making guesses is to use Newtons method. Suppose x is the number we want the root of and guess is the current guessed x guess+ guess answer. The guess can be improved by using as the next guess. 2 Write a program that implements Newtons method. The program should prompt the user for the value to nd the square root of (i.e. x) and the number of times to improve the guess. Starting with a guess of x/2, your program should loop the specied number of times applying Newtons method and report the nal value of guess. Your should also subtract your estimate from the value of math.sqrt(x) to show the user how close the approximation is. For the following exercises, please write code to compute the answer. Include the answer in a comment at the top of your program. Exercise 7. The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143? Exercise 8. A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. Find the largest palindrome made from the product of two 3-digit numbers. Exercise 9. 215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of the number 21000 ? 2

Programming #0

SSP

Exercise 10. Euler published the remarkable quadratic formula: n2 + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 412 + 41 + 41 is clearly divisible by 41. Using computers, the incredible formula n2 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coecients, 79 and 1601, is 126479. Considering quadratics of the form: n2 + an + b, where |a| < 1000 and |b| < 1000 where |n| is the absolute value of n (e.g. |11| = 11 and | 4| = 4) Find the product of the coecients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0.

You might also like