Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions maths/factorial.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
return value


def factorial_recursive(n: int) -> int:
def factorial_recursive(num: int) -> int:
"""
Calculate the factorial of a positive integer
https://en.wikipedia.org/wiki/Factorial
Expand All @@ -52,17 +52,17 @@
...
ValueError: factorial() not defined for negative values
"""
if not isinstance(n, int):
if not isinstance(num, int):
raise ValueError("factorial() only accepts integral values")
if n < 0:
if num < 0:
raise ValueError("factorial() not defined for negative values")
return 1 if n in {0, 1} else n * factorial_recursive(n - 1)
return 1 if num in {0, 1} else num * factorial_recursive(num - 1)


if __name__ == "__main__":
import doctest

doctest.testmod()

n = int(input("Enter a positive integer: ").strip() or 0)
print(f"factorial{n} is {factorial(n)}")
num = int(input("Enter a positive integer: ").strip() or 0)
print(f"factorial{n} is {factorial(num)}")

Check failure on line 68 in maths/factorial.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (F821)

maths/factorial.py:68:23: F821 Undefined name `n`
12 changes: 10 additions & 2 deletions maths/prime_check.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Prime Check."""
"""PRIME CHECK."""

import math
import unittest
Expand All @@ -7,7 +7,8 @@


def is_prime(number: int) -> bool:
"""Checks to see if a number is a prime in O(sqrt(n)).
"""
Checks to see if a number is a prime in O(sqrt(n)).

A number is prime if it has exactly two factors: 1 and itself.

Expand All @@ -29,14 +30,21 @@ def is_prime(number: int) -> bool:
True
>>> is_prime(67483)
False

If the number entered is not whole number i.e not an integer
For example-
>>> is_prime(16.1)
Traceback (most recent call last):
...
ValueError: is_prime() only accepts positive integers

If the number entered is negative integer
For example-
>>> is_prime(-4)
Traceback (most recent call last):
...
ValueError: is_prime() only accepts positive integers

"""

# precondition
Expand Down
22 changes: 5 additions & 17 deletions physics/centripetal_force.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
"""
Description : Centripetal force is the force acting on an object in
curvilinear motion directed towards the axis of rotation
or centre of curvature.

The unit of centripetal force is newton.

The centripetal force is always directed perpendicular to the
direction of the object's displacement. Using Newton's second
law of motion, it is found that the centripetal force of an object
moving in a circular path always acts towards the centre of the circle.
The Centripetal Force Formula is given as the product of mass (in kg)
and tangential velocity (in meters per second) squared, divided by the
radius (in meters) that implies that on doubling the tangential velocity,
the centripetal force will be quadrupled. Mathematically it is written as:

F = mv²/r
Where, F is the Centripetal force, m is the mass of the object, v is the
speed or velocity of the object and r is the radius.
Where, F = Centripetal Force
m = Mass of the Body
v = Tangential Velocity
r = Radius of Circular Path

Reference: https://byjus.com/physics/centripetal-and-centrifugal-force/
"""


Expand Down
40 changes: 17 additions & 23 deletions physics/ideal_gas_law.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
"""
The ideal gas law, also called the general gas equation, is the
equation of state of a hypothetical ideal gas. It is a good approximation
of the behavior of many gases under many conditions, although it has
several limitations. It was first stated by Benoît Paul Émile Clapeyron
in 1834 as a combination of the empirical Boyle's law, Charles's law,
Avogadro's law, and Gay-Lussac's law.[1] The ideal gas law is often written
in an empirical form:
------------
| PV = nRT |
------------
P = Pressure (Pa)
V = Volume (m^3)
n = Amount of substance (mol)
R = Universal gas constant
T = Absolute temperature (Kelvin)

(Description adapted from https://en.wikipedia.org/wiki/Ideal_gas_law )

PV = nRT

Where, P = Pressure (Pa)
V = Volume (m^3)
n = Amount of substance (mol)
R = Universal gas constant
T = Absolute temperature (Kelvin)

Description adapted from https://en.wikipedia.org/wiki/Ideal_gas_law

"""

UNIVERSAL_GAS_CONSTANT = 8.314462 # Unit - J mol-1 K-1
Expand All @@ -32,9 +26,9 @@ def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float:
...
ValueError: Invalid inputs. Enter positive value.
"""
if moles < 0 or kelvin < 0 or volume < 0:
if moles <= 0 or kelvin <= 0 or volume <= 0:
raise ValueError("Invalid inputs. Enter positive value.")
return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume
return (moles * kelvin * UNIVERSAL_GAS_CONSTANT) / volume


def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float:
Expand All @@ -48,9 +42,9 @@ def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float:
...
ValueError: Invalid inputs. Enter positive value.
"""
if moles < 0 or kelvin < 0 or pressure < 0:
if moles <= 0 or kelvin <= 0 or pressure <= 0:
raise ValueError("Invalid inputs. Enter positive value.")
return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure
return (moles * kelvin * UNIVERSAL_GAS_CONSTANT) / pressure


def temperature_of_gas_system(moles: float, volume: float, pressure: float) -> float:
Expand All @@ -64,7 +58,7 @@ def temperature_of_gas_system(moles: float, volume: float, pressure: float) -> f
...
ValueError: Invalid inputs. Enter positive value.
"""
if moles < 0 or volume < 0 or pressure < 0:
if moles <= 0 or volume <= 0 or pressure <= 0:
raise ValueError("Invalid inputs. Enter positive value.")

return pressure * volume / (moles * UNIVERSAL_GAS_CONSTANT)
Expand All @@ -81,7 +75,7 @@ def moles_of_gas_system(kelvin: float, volume: float, pressure: float) -> float:
...
ValueError: Invalid inputs. Enter positive value.
"""
if kelvin < 0 or volume < 0 or pressure < 0:
if kelvin <= 0 or volume <= 0 or pressure <= 0:
raise ValueError("Invalid inputs. Enter positive value.")

return pressure * volume / (kelvin * UNIVERSAL_GAS_CONSTANT)
Expand Down
Loading