-
-
Notifications
You must be signed in to change notification settings - Fork 46.5k
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
Changes from 12 commits
1427fe3
0608c40
7457b25
b1ef5a3
c957bd7
07502c8
ead4f47
c18b39d
cb006c3
ab04734
69487f3
6a741dc
d13c09d
18df9ba
63734b9
aa5f176
9779c1a
aa4d4e8
7bb663f
52c2cb8
643958b
65f5851
7f14ab9
c05ed7c
886fb5d
97b89cd
a1c783a
1c2b7b2
5aa65b8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
""" | ||
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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
""" | ||
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() |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's rename the file to |
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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
|
||||||||
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") | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
|
||||||||
|
||||||||
if __name__ == "__main__": | ||||||||
import doctest | ||||||||
|
||||||||
doctest.testmod() |
There was a problem hiding this comment.
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