pto100 = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ] def in_100(x): """Check if x is in the primes less than 100 i.e., in the first 25 primes. Useful for e.g. the first step in Baillie–PSW. """ return x in pto100 def hund_div(x): """Check if x is divisible by any of the 25 primes less than 100. Returns 0 if there is no such divisor, otherwise returns the first divisor. """ for v in pto100: if v >= x: return 0 if x % v == 0: return v return 0 def trd(x): """Express x as 2^r * d. Returns two values, r and d. """ count = 0 xc = x while xc % 2 == 0: count = count + 1 xc = xc // 2 return count, xc