Skip to content

Stewart's Theorem #9779

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 17 commits into from
Closed
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
36 changes: 36 additions & 0 deletions hashes/fletcher16.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""
The Fletcher checksum is an algorithm for computing a position-dependent
checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs
in the late 1970s.[1] The objective of the Fletcher checksum was to
provide error-detection properties approaching those of a cyclic
redundancy check but with the lower computational effort associated
with summation techniques.

Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum
"""


def fletcher16(text: str) -> int:
"""
Loop through every character in the data and add to two sums.

>>> fletcher16('hello world')
6752
>>> fletcher16('onethousandfourhundredthirtyfour')
28347
>>> fletcher16('The quick brown fox jumps over the lazy dog.')
5655
"""
data = bytes(text, "ascii")
sum1 = 0
sum2 = 0
for character in data:
sum1 = (sum1 + character) % 255
sum2 = (sum1 + sum2) % 255
return (sum2 << 8) | sum1


if __name__ == "__main__":
import doctest

doctest.testmod()
40 changes: 40 additions & 0 deletions hashes/fletcher32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
The Fletcher checksum is an algorithm for computing a position-dependent
checksum devised by John G. Fletcher (1934–2012) at Lawrence Livermore Labs
in the late 1970s.[1] The objective of the Fletcher checksum was to
provide error-detection properties approaching those of a cyclic
redundancy check but with the lower computational effort associated
with summation techniques.

Source: https://en.wikipedia.org/wiki/Fletcher%27s_checksum
"""


def fletcher32(text: str) -> int:
"""
Turns the data into a list, and then loops them in pairs of two and adds to sums

>>> fletcher32('Apple')
705355030
>>> fletcher32('ABCDEFGHI')
3220837722
>>> fletcher32("abcd")
690407108
"""
data = bytes(text, "ascii")
sum1 = 0
sum2 = 0
data_list = list(data)
if len(data_list) % 2 == 1:
data_list.append(0)
for idx in range(len(data_list) // 2):
v = (data_list[idx * 2 + 1] << 8) + data_list[idx * 2]
sum1 = (sum1 + v) % 65535
sum2 = (sum1 + sum2) % 65535
return (sum2 << 16) | sum1


if __name__ == "__main__":
import doctest

doctest.testmod()
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()