Problem Set 3#

Distance#

Write a function called s1() that prompts the user to enter two points on the Cartesian plane with different coordinates and calculates the slope between the two points and the distance between the two points. The formulas for both of these quantities are as follows between points (x1,y1) and (x2,y2):

\[ \begin{align}\begin{aligned}slope = \frac{y_{2}-y_{1}}{x_{2}-x_{1}}\\dist = \sqrt{(y_{2}-y_{1})^2 + (x_{2}-x_{1})^2}\end{aligned}\end{align} \]

For example, if you have two points: (1,1) and (2,2), the slope and distance between the two points should be 1 and 1.4.

When you call s1(), the output should be like this:

../_images/s1.gif

Universal Gravitation#

The force of gravity, or gravitational force, pulls objects with mass toward each other. We often think about the force of gravity from Earth. This force is what keeps your body on the ground. But any object with mass exerts a gravitational force on all other objects with mass.

The gravitational force between two objects is larger when the masses of the objects are larger. That’s why you can feel the gravitational force between you and Earth, but the force between you and objects with smaller masses is too weak to feel.

The gravitational force between two objects also depends on the distance between their centers. The further objects are from one another, the weaker the force is.

The equation for gravitation force thus takes the form:

\[F = G\frac{m1m2}{r^2}\]

where

  • F: gravitational force acting between two objects(in Newton)

  • m1 and m2: the masses of the objects(in kg)

  • r: the distance between the centers of their masses(in m)

G is the gravitational constant, which is \(6.67*10^{-11}\)

Write a function called s2() that prompts the user to enter the masses of two objects(for example, two stars), and the distance between them. Then it should calculate the gravitational force. When you call s2(), the output should be like this:

../_images/s2.gif

Solve Equation#

Write a function called solve() to solve quadratic equation: \(ax^2+bx+c=0\)

When you call solve(), the output should be like this:

../_images/solve.gif

Lottery#

Most lotteries allow users to choose some balls painted in different numbers, out of the box. If the user choose all the balls correctly, then he/she wins the lottery.

Write a method gamble() to calculate a person’s chance of winning a lottery. For example, if the user needs to choose 1 ball out of 6, then the chance of winning should be 0.1666.

The number of possible choices of balls is \(\frac{n!}{(n-k)! * k!}\), when the user is choosing k balls out of n. When you call gamble(), the output should be look like this:

../_images/gamble.gif

提交:#

replit链接:https://replit.com/team/SCLS-CS2023/HW5

每个函数至少需要测试(调用)两次。