CPSC120
Fundamentals of Computer Science

Activity 13

Return

Multiply Integers

Now that we know how to use functions, your homework assignments can be simplified. In your assignments, you will be provided with code that handles getting the input and printing the result. So all you have to do is write the function. To practice with the new format, complete the Multiply Integers programming challenge.

The challenge includes a main function that calls a function that has not been written yet. The program will produce an error if you don’t write the function. You do not need to modify any of the code in the main function. If you do, you may break it.

Epidemiology

Epidemiologists use mathematical models to predict the spread of disease. These models can be used to help guide public policy decisions. One of the simplest epidemiological models uses the reproduction rate, R0 (pronounced r-naught), of a disease. Suppose 100 people have a disease with an R0 of 1.5. In that case, the simulation predicts that after a time called a generation, 100 * 1.5 = 150 people will have the disease. And after two generations it predicts (100 * 1.5) * 1.5 = 225 people will have the disease.

Description

Create a Python program that defines the function simulate_spread(initial_population: int, reproduction_rate: float, num_generations: int) -> float. The function uses a disease’s reproduction rate to compute the spread of disease. The parameter intial_population is the initial population of people who are infected with the disease. The parameter reproduction_rate is the R0 for the disease. The function should return the number of people who will have the disease after num_generations.

Test Cases

import test

def main() -> None:
    test.equal(simulate_spread(100, 1.5, 1), 150.0)
    test.equal(simulate_spread(100, 1.5, 2), 225.0)
    # put more test cases here
    return None

main()

Hint

  • Because the number of generations is an input to the function, the number of multiplications of R0 is not known. So the function must use a loop to compute the spread.

  • Use an accumulator to compute the number of people with the disease after each generation.

Challenge

A massive flaw with this model is that if it is run for enough generations, it will predict more people than exist will contract the disease. A better model would consider that people become immune after contracting the disease. As the fraction of the resistant population goes up, the r0 of the disease goes down. Create a new function that adds a parameter for the population size simulates the disease spread by modulating R0 using the population’s immune fraction.

Using this simple model, you can predict whether a better public health policy is to encourage masks or herd immunity. Research the R0s for both of these situations and run the simulation to see which is more effective.

Twin Paradox

In the year 2047, a set of twins were born. One of these twins was placed in a rocket and launched towards a distant star at a significant speed. Unlike Superman, however, the rocket was programmed to return home after reaching its destination. Because of relativity, the twin who was launched into space will have aged less than their sibling. The question is, how much younger is the spacefaring twin?

Details

Create a Python program that defines the function compute_age_difference(distance_lightyears: float, percentage_of_light: float) -> float that uses relativity to compute how much younger the space-traveling twin is. The parameter distance_lightyears is a float representing a distance traveled, in light-years. The parameter percentage_of_light is a float in the range (0, 100) representing the percentage of the speed of light that the twin traveled. The function should return how many years younger the twin is.

The function should assume that all acceleration is instant, which simplifies the calculations. The speed of light is 299792458 m/s and the number of seconds passed for the twin on earth can be computed from the speed of the rocket in meters per second and the distance the rocket traveled in meters using the equation:

\(time\_on\_earth\_s = 2 \frac{rocket\_distance\_m}{rocket\_speed\_mps}\)

The fraction of time passed on the rocket as compared to Earth can be computed from the speed of the rocket using the dilation equation:

\(dilation = \sqrt{1 - \frac{rocket\_speed\_mps ^ 2}{speed\_of\_light ^ 2}}\)

Test Cases

import test

def main() -> None:
    test.equal(compute_age_difference(1.0, 10.0), 0.1002512578675994)
    # put more test cases here
    return None

main()

Hint

  • Convert the distance in light years to meters:

    \(rocket\_distance\_m = rocket\_distance\_lightyear\cdot 9460730472580800\)

  • Convert the percentage of light to meters per second:

    \(rocket\_speed\_mps = \frac{percent\_of\_light}{100}\cdot 299792458\)

  • Compute the elapsed seconds for the earth-bound twin:

    \(time\_on\_earth\_s = 2\frac{rocket\_distance\_m}{rocket\_speed\_mps}\)

  • Compute the time dilation:

    \(dilation = \sqrt{1 - \frac{rocket\_speed\_mps ^ 2}{299792458 ^ 2}}\)

  • Compute the elapsed seconds for the spacefaring twin:

    \(time\_on\_rocket\_s = dilation \cdot time\_on\_earth\_s\)

  • Compute the difference in age of the two twins:

    \(age\_difference\_s = time\_on\_earth\_s - time\_on\_rocket\_s\)

  • Convert the time in seconds to years:

    \(age\_difference\_y = \frac{age\_difference\_s}{31557600}\)

Challenge

What if, instead of just launching one of the children into space, these crazy scientists launched both? Likely, they would have tried to send two of the siblings off into space, but at different speeds. Write a new function called compute_relativistic_age_difference, which computes the difference between two siblings launched into space at different speeds.