diff --git a/hashes/fletcher16.py b/hashes/fletcher16.py new file mode 100644 index 000000000000..7c23c98d72c5 --- /dev/null +++ b/hashes/fletcher16.py @@ -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() diff --git a/hashes/fletcher32.py b/hashes/fletcher32.py new file mode 100644 index 000000000000..0470777f6ac0 --- /dev/null +++ b/hashes/fletcher32.py @@ -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() diff --git a/maths/stewart.py b/maths/stewart.py new file mode 100644 index 000000000000..c657ebefc2c3 --- /dev/null +++ b/maths/stewart.py @@ -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()