Skip to content

Commit ed3cdc0

Browse files
gudlu1925pre-commit-ci[bot]tianyizheng02
authored andcommitted
Added Coulomb_Law (TheAlgorithms#8714)
* Create coulomb_law.py * Update coulomb_law.py * Update coulomb_law.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update and rename coulomb_law.py to coulombs_law.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update coulombs_law.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update coulombs_law.py * Update coulombs_law.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update coulombs_law.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update coulombs_law.py --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
1 parent fc31f9b commit ed3cdc0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

physics/coulombs_law.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Coulomb's law states that the magnitude of the electrostatic force of attraction
3+
or repulsion between two point charges is directly proportional to the product
4+
of the magnitudes of charges and inversely proportional to the square of the
5+
distance between them.
6+
7+
F = k * q1 * q2 / r^2
8+
9+
k is Coulomb's constant and equals 1/(4π*ε0)
10+
q1 is charge of first body (C)
11+
q2 is charge of second body (C)
12+
r is distance between two charged bodies (m)
13+
14+
Reference: https://en.wikipedia.org/wiki/Coulomb%27s_law
15+
"""
16+
17+
18+
def coulombs_law(q1: float, q2: float, radius: float) -> float:
19+
"""
20+
Calculate the electrostatic force of attraction or repulsion
21+
between two point charges
22+
23+
>>> coulombs_law(15.5, 20, 15)
24+
12382849136.06
25+
>>> coulombs_law(1, 15, 5)
26+
5392531075.38
27+
>>> coulombs_law(20, -50, 15)
28+
-39944674632.44
29+
>>> coulombs_law(-5, -8, 10)
30+
3595020716.92
31+
>>> coulombs_law(50, 100, 50)
32+
17975103584.6
33+
"""
34+
if radius <= 0:
35+
raise ValueError("The radius is always a positive non zero integer")
36+
return round(((8.9875517923 * 10**9) * q1 * q2) / (radius**2), 2)
37+
38+
39+
if __name__ == "__main__":
40+
import doctest
41+
42+
doctest.testmod()

0 commit comments

Comments
 (0)