From 1427fe34f0bf364f860b9f3c4ecd811215e0cdcb Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:22:41 +0530 Subject: [PATCH 01/19] avg and mps speed formulae added --- ...avg_and_mps_speeds_of_gaseous_molecules.py | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 physics/avg_and_mps_speeds_of_gaseous_molecules.py diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py new file mode 100644 index 000000000000..70f189b91912 --- /dev/null +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -0,0 +1,79 @@ +""" +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 + +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") + else: + 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") + else: + return (2 * R * temperature / molar_mass) ** 0.5 + +if __name__ == "__main__": + import doctest + + doctest.testmod() + \ No newline at end of file From 0608c40a89a1cba314551633795318f140466825 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:00:21 +0000 Subject: [PATCH 02/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...avg_and_mps_speeds_of_gaseous_molecules.py | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 70f189b91912..b851ae3e2542 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,10 +1,10 @@ """ 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 @@ -12,19 +12,19 @@ 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: --------------------- @@ -35,13 +35,14 @@ """ # necessary constants -PI = 3.1415926535 # pi -R = 8.3144626181 # gas constant +PI = 3.1415926535 # pi +R = 8.3144626181 # gas constant + 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 @@ -54,11 +55,12 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: raise Exception("Molar mass should be greater than 0 kg/mol") else: 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 @@ -71,9 +73,9 @@ def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: raise Exception("Molar mass should be greater than 0 kg/mol") else: return (2 * R * temperature / molar_mass) ** 0.5 - + + if __name__ == "__main__": import doctest doctest.testmod() - \ No newline at end of file From 7457b25d709d242468b8d31b29307dee3914771d Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:33:39 +0530 Subject: [PATCH 03/19] avg and mps speed formulae added --- ...avg_and_mps_speeds_of_gaseous_molecules.py | 37 ++++++++++++------- 1 file changed, 24 insertions(+), 13 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 70f189b91912..8ee71c56f903 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,5 +1,8 @@ """ -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: +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) | @@ -11,27 +14,34 @@ 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 +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: +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: +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: +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. +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 @@ -40,8 +50,9 @@ 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). - + 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 @@ -54,11 +65,12 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: raise Exception("Molar mass should be greater than 0 kg/mol") else: 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). - + 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 @@ -71,9 +83,8 @@ def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: raise Exception("Molar mass should be greater than 0 kg/mol") else: return (2 * R * temperature / molar_mass) ** 0.5 - + if __name__ == "__main__": import doctest doctest.testmod() - \ No newline at end of file From b1ef5a36fbec003505091eda2731e3bb8aca4955 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:39:08 +0530 Subject: [PATCH 04/19] fixed_spacing --- physics/avg_and_mps_speeds_of_gaseous_molecules.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 8ee71c56f903..20baff29082e 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -3,11 +3,11 @@ 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 @@ -16,14 +16,14 @@ 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: @@ -31,7 +31,7 @@ --------------------- | 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: From 07502c875b13e28836d1e1769613d192ce87bf74 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:11:38 +0000 Subject: [PATCH 05/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...avg_and_mps_speeds_of_gaseous_molecules.py | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index f861520d334b..8dfaa9bd9944 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,46 +1,46 @@ """ -The root-mean-square, average and most probable speeds are derived from -the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a +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: +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 +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 +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 +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 +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. """ @@ -51,7 +51,7 @@ 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 + 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: @@ -67,9 +67,10 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: else: 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 + 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: @@ -85,8 +86,8 @@ def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: else: return (2 * R * temperature / molar_mass) ** 0.5 + if __name__ == "__main__": import doctest doctest.testmod() - From ead4f47af540c24a9575fa1eda95897bec18a76b Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:44:07 +0530 Subject: [PATCH 06/19] spacing --- .../avg_and_mps_speeds_of_gaseous_molecules.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index f861520d334b..3b7e622184e7 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,7 +1,8 @@ """ 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. +probability distribution that describes the distribution of speeds for particles +in a gas. The distribution is given by the following equation: ------------------------------------------------- @@ -24,24 +25,25 @@ | 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: +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: +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. +most probable speeds of molecules in a gas given the +temperature and molar mass of the gas. """ # necessary constants From cb006c383bbba43063a51f079f47a528900230a8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 14:19:02 +0000 Subject: [PATCH 07/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../avg_and_mps_speeds_of_gaseous_molecules.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 2a7fd542289d..3157741208e9 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,7 +1,7 @@ """ -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 +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: @@ -25,24 +25,24 @@ | 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 +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 +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 +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. """ From ab04734aa2ec0ef0af14fc5e233d3067da27eb3c Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 19:50:16 +0530 Subject: [PATCH 08/19] ws --- .../avg_and_mps_speeds_of_gaseous_molecules.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/avg_and_mps_speeds_of_gaseous_molecules.py index 2a7fd542289d..3157741208e9 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/avg_and_mps_speeds_of_gaseous_molecules.py @@ -1,7 +1,7 @@ """ -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 +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: @@ -25,24 +25,24 @@ | 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 +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 +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 +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. """ From 6a741dc63f7ba3140c24e2cf7bde364b9ae4afa2 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 20:37:45 +0530 Subject: [PATCH 09/19] added amicable numbers --- maths/amicable_pair.py | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 maths/amicable_pair.py diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py new file mode 100644 index 000000000000..d596cfc9884b --- /dev/null +++ b/maths/amicable_pair.py @@ -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: + """ + 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: + """ + 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() \ No newline at end of file From d13c09d2ca2399abd3126a3a63584dc5f308d582 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:08:24 +0000 Subject: [PATCH 10/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/amicable_pair.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index d596cfc9884b..a94dcab32fee 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -13,10 +13,11 @@ to find the sum of the proper divisors of a number. """ + def sum_of_divisors(n: int) -> int: """ Find the sum of the proper divisors of a number. - + Examples: >>> sum_of_divisors(220) 284 @@ -27,14 +28,15 @@ def sum_of_divisors(n: int) -> int: for i in range(1, n): if n % i == 0: sum += i - + return sum + def is_amicable_pair(a: int, b: int) -> bool: """ Check if two numbers (a and b) are amicable numbers. Arguments must be positive integers. - + Examples: >>> is_amicable_pair(220, 284) True @@ -60,6 +62,8 @@ def is_amicable_pair(a: int, b: int) -> bool: return sum_of_divisors(a) == b and sum_of_divisors(b) == a + if __name__ == "__main__": import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From 63734b9d97e34ec9d31f34d196f4d24099911ad4 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 20:41:56 +0530 Subject: [PATCH 11/19] added amicable numbers --- maths/amicable_pair.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index d596cfc9884b..144d7e2a42ae 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -13,7 +13,7 @@ to find the sum of the proper divisors of a number. """ -def sum_of_divisors(n: int) -> int: +def sum_of_divisors(number: int) -> int: """ Find the sum of the proper divisors of a number. @@ -24,15 +24,15 @@ def sum_of_divisors(n: int) -> int: 220 """ sum = 0 - for i in range(1, n): - if n % i == 0: + for i in range(1, number): + if number % i == 0: sum += i return sum -def is_amicable_pair(a: int, b: int) -> bool: +def is_amicable_pair(number_1: int, number_2: int) -> bool: """ - Check if two numbers (a and b) are amicable numbers. + Check if two numbers (number_1 and number_2) are amicable. Arguments must be positive integers. Examples: @@ -55,10 +55,10 @@ def is_amicable_pair(a: int, b: int) -> bool: >>> is_amicable_pair(42, 42) False """ - if a <= 0 or b <= 0: + if number_1 <= 0 or number_2 <= 0: raise ValueError("Numbers must be positive integers.") - - return sum_of_divisors(a) == b and sum_of_divisors(b) == a + + return sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1 if __name__ == "__main__": import doctest From 9779c1a623a84c5729bc568302c330047ad1d42c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:13:42 +0000 Subject: [PATCH 12/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/amicable_pair.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index ac50168f7deb..a15a0a87dcf5 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -13,6 +13,7 @@ to find the sum of the proper divisors of a number. """ + def sum_of_divisors(number: int) -> int: """ Find the sum of the proper divisors of a number. @@ -30,6 +31,7 @@ def sum_of_divisors(number: int) -> int: return sum + def is_amicable_pair(number_1: int, number_2: int) -> bool: """ Check if two numbers (number_1 and number_2) are amicable. @@ -57,8 +59,11 @@ def is_amicable_pair(number_1: int, number_2: int) -> bool: """ if number_1 <= 0 or number_2 <= 0: raise ValueError("Numbers must be positive integers.") - - return sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1 + + return ( + sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1 + ) + if __name__ == "__main__": import doctest From aa4d4e8b89cecd6252dffe72b0b6d5850bea9d96 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 20:45:55 +0530 Subject: [PATCH 13/19] spacing --- maths/amicable_pair.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index ac50168f7deb..c392671db352 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -2,7 +2,7 @@ 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. +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: @@ -10,7 +10,7 @@ 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. +to find the 'sum' of the proper divisors of a number. """ def sum_of_divisors(number: int) -> int: @@ -57,8 +57,11 @@ def is_amicable_pair(number_1: int, number_2: int) -> bool: """ if number_1 <= 0 or number_2 <= 0: raise ValueError("Numbers must be positive integers.") - - return sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1 + + if sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1: + return True + else: + return False if __name__ == "__main__": import doctest From 52c2cb82d7210d704229ea9ef2deeb45a397e267 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 10 Oct 2023 15:17:23 +0000 Subject: [PATCH 14/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/amicable_pair.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py index 0bc287d744da..ed8f9184353d 100644 --- a/maths/amicable_pair.py +++ b/maths/amicable_pair.py @@ -65,6 +65,7 @@ def is_amicable_pair(number_1: int, number_2: int) -> bool: else: return False + if __name__ == "__main__": import doctest From 643958b1f429bf9afdb7ebdab235a1fad183bf1f Mon Sep 17 00:00:00 2001 From: Baron105 Date: Tue, 10 Oct 2023 20:48:27 +0530 Subject: [PATCH 15/19] removed --- maths/amicable_pair.py | 71 ------------------------------------------ 1 file changed, 71 deletions(-) delete mode 100644 maths/amicable_pair.py diff --git a/maths/amicable_pair.py b/maths/amicable_pair.py deleted file mode 100644 index 0bc287d744da..000000000000 --- a/maths/amicable_pair.py +++ /dev/null @@ -1,71 +0,0 @@ -""" -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(number: int) -> int: - """ - 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, number): - if number % i == 0: - sum += i - - return sum - - -def is_amicable_pair(number_1: int, number_2: int) -> bool: - """ - Check if two numbers (number_1 and number_2) are amicable. - 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 number_1 <= 0 or number_2 <= 0: - raise ValueError("Numbers must be positive integers.") - - if sum_of_divisors(number_1) == number_2 and sum_of_divisors(number_2) == number_1: - return True - else: - return False - -if __name__ == "__main__": - import doctest - - doctest.testmod() From c05ed7cc8a6821315b477abb6161dd8e993d8a29 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Thu, 12 Oct 2023 21:54:51 +0530 Subject: [PATCH 16/19] changed name of file and added code improvements --- ...olecules.py => speeds_of_gas_molecules.py} | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) rename physics/{avg_and_mps_speeds_of_gaseous_molecules.py => speeds_of_gas_molecules.py} (75%) diff --git a/physics/avg_and_mps_speeds_of_gaseous_molecules.py b/physics/speeds_of_gas_molecules.py similarity index 75% rename from physics/avg_and_mps_speeds_of_gaseous_molecules.py rename to physics/speeds_of_gas_molecules.py index 3157741208e9..004670148530 100644 --- a/physics/avg_and_mps_speeds_of_gaseous_molecules.py +++ b/physics/speeds_of_gas_molecules.py @@ -46,10 +46,8 @@ temperature and molar mass of the gas. """ -# necessary constants -PI = 3.1415926535 # pi -R = 8.3144626181 # gas constant - +# import the constants R and PI from the scipy.constants library +from scipy.constants import R, pi as PI def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ @@ -58,16 +56,24 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: Examples: >>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K - 454.34887551126405 + 454.3488755020387 >>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K - 445.5257273482451 + 445.52572733919885 + >>> avg_speed_of_molecule(-273, 0.028) # invalid temperature + Traceback (most recent call last): + ... + Exception: Absolute temperature cannot be less than 0 K + >>> avg_speed_of_molecule(273, 0) # invalid molar mass + Traceback (most recent call last): + ... + Exception: Molar mass should be greater than 0 kg/mol """ + 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") - 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: @@ -77,16 +83,24 @@ def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float: Examples: >>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K - 402.6562070215111 + 402.65620701908966 >>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K - 394.83689555229637 + 394.836895549922 + >>> mps_speed_of_molecule(-273, 0.028) # invalid temperature + Traceback (most recent call last): + ... + Exception: Absolute temperature cannot be less than 0 K + >>> mps_speed_of_molecule(273, 0) # invalid molar mass + Traceback (most recent call last): + ... + Exception: Molar mass should be greater than 0 kg/mol """ + 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") - else: - return (2 * R * temperature / molar_mass) ** 0.5 + return (2 * R * temperature / molar_mass) ** 0.5 if __name__ == "__main__": From 886fb5d37f6290c2c51d0db8846d38e4c2befe53 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 12 Oct 2023 16:25:28 +0000 Subject: [PATCH 17/19] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/speeds_of_gas_molecules.py | 1 + 1 file changed, 1 insertion(+) diff --git a/physics/speeds_of_gas_molecules.py b/physics/speeds_of_gas_molecules.py index 004670148530..18de1f466cc1 100644 --- a/physics/speeds_of_gas_molecules.py +++ b/physics/speeds_of_gas_molecules.py @@ -49,6 +49,7 @@ # import the constants R and PI from the scipy.constants library from scipy.constants import R, pi as PI + 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 From 97b89cdfdfe4024afcc29eeda08bdce268fffe28 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Thu, 12 Oct 2023 21:57:34 +0530 Subject: [PATCH 18/19] issues fixed due to pi --- physics/speeds_of_gas_molecules.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/physics/speeds_of_gas_molecules.py b/physics/speeds_of_gas_molecules.py index 004670148530..c73201767001 100644 --- a/physics/speeds_of_gas_molecules.py +++ b/physics/speeds_of_gas_molecules.py @@ -46,8 +46,8 @@ temperature and molar mass of the gas. """ -# import the constants R and PI from the scipy.constants library -from scipy.constants import R, pi as PI +# import the constants R and pi from the scipy.constants library +from scipy.constants import R, pi def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: """ @@ -73,7 +73,7 @@ def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float: 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") - 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: From 1c2b7b2e5f905a8257725cfe39806f87936e2594 Mon Sep 17 00:00:00 2001 From: Baron105 Date: Thu, 12 Oct 2023 22:24:17 +0530 Subject: [PATCH 19/19] requested changes added --- physics/speeds_of_gas_molecules.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/physics/speeds_of_gas_molecules.py b/physics/speeds_of_gas_molecules.py index 92cc607da9cd..a50d1c0f6d76 100644 --- a/physics/speeds_of_gas_molecules.py +++ b/physics/speeds_of_gas_molecules.py @@ -1,8 +1,9 @@ """ -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 root-mean-square, average and most probable speeds of gas molecules are +derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann +distribution is a probability distribution that describes the distribution of +speeds of particles in an ideal gas. + The distribution is given by the following equation: ------------------------------------------------- @@ -22,7 +23,7 @@ from 0 to infinity and dividing by the total number of molecules. The result is: --------------------- - | vavg = √8RT/πM | + | vavg = √(8RT/πM) | --------------------- The most probable speed is the speed at which the Maxwell-Boltzmann distribution @@ -30,7 +31,7 @@ distribution with respect to v and setting the result equal to zero. The result is: --------------------- - | vmp = √2RT/M | + | vmp = √(2RT/M) | --------------------- The root-mean-square speed is another measure of the average speed @@ -38,7 +39,7 @@ of the average of the squares of the speeds of the molecules. The result is: --------------------- - | vrms = √3RT/m | + | vrms = √(3RT/M) | --------------------- Here we have defined functions to calculate the average and