Skip to content

Stewart's Theorem #9887

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

Closed
wants to merge 27 commits into from
Closed
Changes from 19 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e300cbf
Add files via upload
rtang09 Oct 5, 2023
ac7cdba
Update fletcher16.py
rtang09 Oct 5, 2023
79feadf
Update fletcher16.py
rtang09 Oct 5, 2023
f4427ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
b4ec72f
Update fletcher16.py
rtang09 Oct 5, 2023
d141fee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
45e79a7
Update fletcher16.py
rtang09 Oct 5, 2023
a28d3c7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
114e415
Update fletcher16.py
cclauss Oct 5, 2023
7a28e89
Add files via upload
rtang09 Oct 5, 2023
7d7d0c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
7b8ef6c
Update stewart.py
rtang09 Oct 5, 2023
05cdc34
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
f4f7fb5
Update stewart.py
rtang09 Oct 5, 2023
7864c2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
e2accc7
Add files via upload
rtang09 Oct 5, 2023
1611ec2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 5, 2023
82950bc
Merge branch 'TheAlgorithms:master' into master
rtang09 Oct 6, 2023
364b1da
Delete hashes/fletcher32.py
rtang09 Oct 6, 2023
b64f1de
Update stewart.py
rtang09 Oct 8, 2023
7caf50d
Merge branch 'TheAlgorithms:master' into master
rtang09 Oct 10, 2023
c1ccfb8
Update primelib.py
rtang09 Oct 10, 2023
c5de3ff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
24f86be
Update primelib.py
rtang09 Oct 10, 2023
7438193
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 2023
ef337c2
Update primelib.py
rtang09 Oct 10, 2023
f89f7ce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 10, 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
42 changes: 42 additions & 0 deletions maths/stewart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
In geometry, Stewart's theorem yields a relation between the
lengths of the sides and the length of a cevian in a triangle.
Its name is in honour of the Scottish mathematician Matthew
Stewart, who published the theorem in 1746.[1]

Source: https://en.wikipedia.org/wiki/Stewart%27s_theorem
"""


def stewart(a: float, b: float, c: float, n: float, m: float) -> float:

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

Please provide descriptive name for the parameter: c

Please provide descriptive name for the parameter: n

Please provide descriptive name for the parameter: m

"""
Given the side lengths of the triangle (a,b,c), where the cevian intersects
the side with side length a and splits it into segments with lengths n and m,
this formula finds the length of the cevian.

>>> stewart(1,1,1,0.5,0.5)
0.8660254037844386
>>> stewart(1,2,3,4,5)
Traceback (most recent call last):
...
ValueError: This triangle violates the triangle inequality
>>> stewart(1,1,1,1,1)
Traceback (most recent call last):
...
ValueError: n+m must equal a
>>> stewart(3,2,4,1.7,1.3)
2.9308701779505686
"""
if a + b <= c or b + c <= a or a + c <= b:
raise ValueError("This triangle violates the triangle inequality")
if n + m != a:
raise ValueError("n+m must equal a")
if a <= 0 or b <= 0 or c <= 0 or n < 0 or m < 0:
raise ValueError("The side lengths of a triangle have to be positive")
return ((b**2 * m + c**2 * n - m * a * n) / a) ** 0.5


if __name__ == "__main__":
import doctest

doctest.testmod()