Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding avg and mps speed formulae for ideal gases #10229

Merged
merged 29 commits into from
Oct 14, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
1427fe3
avg and mps speed formulae added
Baron105 Oct 10, 2023
0608c40
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
7457b25
avg and mps speed formulae added
Baron105 Oct 10, 2023
b1ef5a3
fixed_spacing
Baron105 Oct 10, 2023
c957bd7
.
Baron105 Oct 10, 2023
07502c8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
ead4f47
spacing
Baron105 Oct 10, 2023
c18b39d
.
Baron105 Oct 10, 2023
cb006c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
ab04734
ws
Baron105 Oct 10, 2023
69487f3
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
6a741dc
added amicable numbers
Baron105 Oct 10, 2023
d13c09d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
18df9ba
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 10, 2023
63734b9
added amicable numbers
Baron105 Oct 10, 2023
aa5f176
descriptive
Baron105 Oct 10, 2023
9779c1a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
aa4d4e8
spacing
Baron105 Oct 10, 2023
7bb663f
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
52c2cb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
643958b
removed
Baron105 Oct 10, 2023
65f5851
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 10, 2023
7f14ab9
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 12, 2023
c05ed7c
changed name of file and added code improvements
Baron105 Oct 12, 2023
886fb5d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 12, 2023
97b89cd
issues fixed due to pi
Baron105 Oct 12, 2023
a1c783a
Merge branch 'master' of https://github.com/Baron105/Python
Baron105 Oct 12, 2023
1c2b7b2
requested changes added
Baron105 Oct 12, 2023
5aa65b8
Merge branch 'TheAlgorithms:master' into master
Baron105 Oct 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions maths/amicable_pair.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
Amicable numbers are two different natural numbers such that the sum
of the proper divisors of each is equal to the other number.
That is, s(a)=b and s(b)=a, where s(n) is equal to
the sum of positive divisors of n except n itself.
Here, a and b form a pair of amicable numbers.

More information about amicable numbers can be found here:
https://en.wikipedia.org/wiki/Amicable_numbers

Here, we have defined a function to check if two numbers are amicable.
We have also defined an auxiliary function,
to find the sum of the proper divisors of a number.
"""

def sum_of_divisors(n: int) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

"""
Find the sum of the proper divisors of a number.

Examples:
>>> sum_of_divisors(220)
284
>>> sum_of_divisors(284)
220
"""
sum = 0
for i in range(1, n):
if n % i == 0:
sum += i

return sum

def is_amicable_pair(a: int, b: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

"""
Check if two numbers (a and b) are amicable numbers.
Arguments must be positive integers.

Examples:
>>> is_amicable_pair(220, 284)
True
>>> is_amicable_pair(1184, 1210)
True
>>> is_amicable_pair(127, 729)
False
>>> is_amicable_pair(7, 13)
False
>>> is_amicable_pair(0, 12)
Traceback (most recent call last):
...
ValueError: Numbers must be positive integers.
>>> is_amicable_pair(12, -1)
Traceback (most recent call last):
...
ValueError: Numbers must be positive integers.
>>> is_amicable_pair(42, 42)
False
"""
if a <= 0 or b <= 0:
raise ValueError("Numbers must be positive integers.")

return sum_of_divisors(a) == b and sum_of_divisors(b) == a

if __name__ == "__main__":
import doctest
doctest.testmod()
95 changes: 95 additions & 0 deletions physics/avg_and_mps_speeds_of_gaseous_molecules.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename the file to speeds_of_gas_molecules.py to keep it shorter

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
The root-mean-square, average and most probable speeds are derived from
the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a
probability distribution that describes the distribution of speeds for particles
in a gas.
The distribution is given by the following equation:

-------------------------------------------------
| f(v) = (M/2πRT)^(3/2) * 4πv^2 * e^(-Mv^2/2RT) |
-------------------------------------------------

where:
f(v) is the fraction of molecules with a speed v
M is the molar mass of the gas in kg/mol
R is the gas constant
T is the absolute temperature

More information about the Maxwell-Boltzmann distribution can be found here:
https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution

The average speed can be calculated by integrating the Maxwell-Boltzmann distribution
from 0 to infinity and dividing by the total number of molecules. The result is:

---------------------
| vavg = √8RT/πM |
---------------------

The most probable speed is the speed at which the Maxwell-Boltzmann distribution
is at its maximum. This can be found by differentiating the Maxwell-Boltzmann
distribution with respect to v and setting the result equal to zero. The result is:

---------------------
| vmp = √2RT/M |
---------------------

The root-mean-square speed is another measure of the average speed
of the molecules in a gas. It is calculated by taking the square root
of the average of the squares of the speeds of the molecules. The result is:

---------------------
| vrms = √3RT/m |
---------------------

Here we have defined functions to calculate the average and
most probable speeds of molecules in a gas given the
temperature and molar mass of the gas.
"""

# necessary constants
PI = 3.1415926535 # pi
R = 8.3144626181 # gas constant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we import these constants from existing libraries rather than hard-coding them?



def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float:
"""
Takes the temperature (in K) and molar mass (in kg/mol) of a gas
and returns the average speed of a molecule in the gas (in m/s).

Examples:
>>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K
454.34887551126405
>>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K
445.5257273482451
"""
if temperature < 0:
raise Exception("Absolute temperature cannot be less than 0 K")
if molar_mass <= 0:
raise Exception("Molar mass should be greater than 0 kg/mol")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of completeness, could you add doctests that test these cases?

else:
return (8 * R * temperature / (PI * molar_mass)) ** 0.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else:
return (8 * R * temperature / (PI * molar_mass)) ** 0.5
return (8 * R * temperature / (PI * molar_mass)) ** 0.5



def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float:
"""
Takes the temperature (in K) and molar mass (in kg/mol) of a gas
and returns the most probable speed of a molecule in the gas (in m/s).

Examples:
>>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K
402.6562070215111
>>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K
394.83689555229637
"""
if temperature < 0:
raise Exception("Absolute temperature cannot be less than 0 K")
if molar_mass <= 0:
raise Exception("Molar mass should be greater than 0 kg/mol")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with these cases

else:
return (2 * R * temperature / molar_mass) ** 0.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else:
return (2 * R * temperature / molar_mass) ** 0.5
return (2 * R * temperature / molar_mass) ** 0.5



if __name__ == "__main__":
import doctest

doctest.testmod()