100825/tex/py100325hashchecksumall.py

# (c) David Vajda
# 10/03/25
# python 3 - hash excersize

def hash_davevajda (X,n,m,b):
    i = 0
    y = 0
    while i < n:
        y = (b*X[i]+y)%m
        i = i + 1
    return y

def adler32_davevajda (X,n,m):
    i = 0
    s1 = 1
    s2 = 0
    while i < n:
        s1 = (s1 + X[i]) % m
        s2 = (s2 + s1) % m
        i = i + 1
    print(s2,s1)
    return (s2*256)+s1

print ("(c) David Vajda")
print ("10/03/25")
print ("python 3 - hash excersize")

X = [2, 87, 16, 41, 76, 65, 33, 91, 7, 32]
i = 0
n = len (X)

print (end="X = [")
while i < n - 1:
    print (X[i], end=", ")
    i = i + 1
print (X[i], "]")

i=0
y=0
m=97
b=64

print ("m=97, base=64, hashval=",hash_davevajda(X,n,m,b))
print ("m=97, base=100, hashval=",hash_davevajda(X,n,m,100))
print ("m=97, base=256, adler 32 - checksum=",adler32_davevajda(X,n,251))