-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Closed
Stewart's Theorem #9887
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 ac7cdba
Update fletcher16.py
rtang09 79feadf
Update fletcher16.py
rtang09 f4427ba
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b4ec72f
Update fletcher16.py
rtang09 d141fee
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 45e79a7
Update fletcher16.py
rtang09 a28d3c7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 114e415
Update fletcher16.py
cclauss 7a28e89
Add files via upload
rtang09 7d7d0c3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 7b8ef6c
Update stewart.py
rtang09 05cdc34
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f4f7fb5
Update stewart.py
rtang09 7864c2a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] e2accc7
Add files via upload
rtang09 1611ec2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 82950bc
Merge branch 'TheAlgorithms:master' into master
rtang09 364b1da
Delete hashes/fletcher32.py
rtang09 b64f1de
Update stewart.py
rtang09 7caf50d
Merge branch 'TheAlgorithms:master' into master
rtang09 c1ccfb8
Update primelib.py
rtang09 c5de3ff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 24f86be
Update primelib.py
rtang09 7438193
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ef337c2
Update primelib.py
rtang09 f89f7ce
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
""" | ||
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() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
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