Albion College

CS 171

Introduction to Computer Science I & Lab

Spring 2026

Tasks

(20 points) Complete the homework as indicated in the source code.

Download the homework page

Download the source file hw04.py and save it on your computer as <EMAIL>hw04.py where <EMAIL> is your Albion email (ABC12) — don't put any spaces in your file name. The file contains the stubs of several Python functions. Save it directly to your folder with your other lab files and open it in the Python environment. Complete the stub methods to perform the indicated function. Verify your code by running the program with the included test cases (don't remove or change them). Add additional test cases as needed.

In most cases, your function will not print any values. Rather, your function will return values to the calling function and any values will be used there, including possibly printing.

Seek help as soon as you are experiencing difficulty with this assignment.
Do not wait until the deadline to seek help!

Notes

Exponential Function (5 points)

Complete the function called exp that takes a double parameter \(x\) and an int parameter \(k\) and returns a double that represents \(e^x\), where $$e \approx 2.7182818284590452353602874713526624977572470936999595749669676277$$ is called Euler's number and appears in many places in mathematics. Similar to the sine and cosine function, the value of \(e\) can be computed using an infinite sum: $$ e^x = 1 + x + \dfrac{x^2}{2!} + \dfrac{x^3}{3!} + \dfrac{x^4}{4!} + \cdots = \sum_{k=0}^{\infty} \underbrace{\dfrac{x^{k}}{k!}}_{\text{term}(x,k)}$$ As in the lab, we will use an approximation with \(n+1\) terms: $$e^x \approx 1 + x + \dfrac{x^2}{2!} + \dfrac{x^3}{3!} + \dfrac{x^4}{4!} + \cdots = \sum_{k=0}^{n} \dfrac{x^{k}}{k!}$$ Remember that the computer is only a model of our real number system!

Complete the exp function so it returns \(e^x\) using \(n\) terms.

Prime Numbers (5 points)

Recall a positive integer \(n\) is called a prime if its only positive factors are 1 and \(n\). The first 25 prime numbers are $$2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97.$$ There are infinitely many prime numbers and they play a significant role in mathematics. They are also at the heart of the encryption techniques that allow one to securely send information over the internet.

The simplest (but not the best) technique for determining if a number \(n\) is prime is by dividing \(n\) by the numbers from 2 through \(n-1\). If a division results in a remainder of zero, then the number is not prime. If none of the remainders are 0, then the number is prime.

Complete the isPrime function using the above described process so the function returns the boolean True if the given number is a prime, or the boolean False otherwise.

Square Root (5 points)

Most modern calculators include a way to compute the square root of a number. Python's math library also has a square root function (math.sqrt). This exercise will explore one possible way of computing the square root.

Given a positive real number \(x\), one can find a number \(s\) such that \(x = s^2\) by computing the following sequence: $$\begin{aligned} s_0 &= x \\ s_1 &= (s_0 + x/s_0)/2 \\ s_2 &= (s_1 + x/s_1)/2 \\ s_3 &= (s_2 + x/s_2)/2 \\ &\ldots\\ s_n &= (s_{n-1} + x/s_{n-1})/2 \\ \end{aligned}$$ As \(n\) gets large, the value of \(s_n\) will approach \(\sqrt{x}\).

Complete the sqrt function so it returns an approximation of \(\sqrt{x}\) using \(n = 10\) steps.

Collatz (5 points)

We talked about the Collatz function in class, which is defined $$\text{Collatz}(n) = \begin{cases} n/2&\text{if $n$ is even};\\ 3n+1&\text{otherwise} \end{cases} . $$ One can repeatedly apply this function to generate a sequence of values. For example, starting with the value 3, $$\begin{aligned} \text{Collatz}(3) &= 3\cdot 3 + 1 = 10 \\ \text{Collatz}(10) &= 10/2 = 5 \\ \text{Collatz}(5) &= 3\cdot 5 + 1 = 16 \\ \text{Collatz}(16) &= 16/2 = 8 \\ \text{Collatz}(8) &= 8/2 = 4 \\ \text{Collatz}(4) &= 4/2 = 2 \\ \text{Collatz}(2) &= 2/2 = 1 \\ \end{aligned}$$ Note this took 7 steps to reach the value of 1. This is often called the hailstone sequence because the numbers increase and decrease like a hailstone rising and falling in a thundercloud. An open question is if this sequence ultimately reaches the value of 1.

Complete the collatzCount function so it returns the number of steps required to reach 1 for the given number \(n\).

Code

# Homework 4 # YOUR NAME HERE import math # returns x**k/k! # WORKS AS IS def term(x, k): result = 1 for i in range(1, k+1): result *= x/i return result # returns approximation to e to the power of x using n>0 terms # use for loop and range function # don't use any math library functions # dont use the ** operator # dont use your factorial or power functions # use your term function def exp(x, n): return None #FIXME # return True if n is prime, False otherwise # a prime is an integer greater than 1, # having only the positive integer divisors 1 and n def isPrime(n): return None #FIXME # return the square root of x # assume x > 0 # use for loop and range function # don't use any math library functions # dont use the ** operator # dont use your factorial or power functions # use your term function # assume x is a non-negative def sqrt(x): return None #FIXME # WORKS AS IS def collatz(n): if (n%2==0): return n//2 else: return 3*n + 1 # count the number of times it takes for repeated applications # of the collatz function to reach the value of 1 # assume n is a positive integer def collatzCount(n): return None #FIXME # Testing code def main(): print("term") print(term(3,5), "should be 2.025") print() print("exp") print(exp(0,1000), "should be close to", math.exp(0)) print(exp(1,1), "should be close to", math.exp(1)) print(exp(1,10), "should be closer to", math.exp(1)) print(exp(1,100), "should be closer to", math.exp(1)) print(exp(1,1000), "should be closer to", math.exp(1)) print() print("isPrime") print("-5:", isPrime(-5), "should be False") print("0:", isPrime(0), "should be False") print("1:", isPrime(1), "should be False") print("2:", isPrime(2), "should be True") print("3:", isPrime(3), "should be True") print("37:", isPrime(37), "should be True") print("57:", isPrime(57), "should be False") print("73:", isPrime(73), "should be True") print("100:", isPrime(100), "should be False") print() print("square root") print(sqrt(1), "should be very close to", math.sqrt(1)) print(sqrt(2), "should be very close to", math.sqrt(2)) print(sqrt(3), "should be very close to", math.sqrt(3)) print(sqrt(4), "should be very close to", math.sqrt(4)) print(sqrt(100), "should be very close to", math.sqrt(100)) print() print("collatz") print(collatz(3), "should be 10") print() print("collatzCount") print(collatzCount(1), "should be 0") print(collatzCount(2), "should be 1") print(collatzCount(3), "should be 7") print(collatzCount(4), "should be 2") print(collatzCount(5), "should be 5") print(collatzCount(6), "should be 8") print(collatzCount(7), "should be 16") print(collatzCount(8), "should be 3") main()

Deliverables

Send me ([email protected]) your Python .py file as an attachment to an email message with Homework 4 as the subject line.