The following code is untested, but should do what you wanted. Use is with
large numbers at your own risk- the code was not written to be fast.
#! /usr/bin/env python
import random
def miller_rabin(n, confidence=20):
"""Tests whether n is prime with confidence at least
1(4**confidence)."""
# find t, s, and d such that t = n1 = d*(2**s), d an odd integer
t, s, d = n, 0, 0
while not t % 2:
t = t >> 1
s += 1
t, d = n, t
# check a number of witnesses equal to your confidence
for i in range(confidence):
# select a random witness
a = random.randrange(2, n)
x = pow(a, d, n)
if x == 1: continue
if x == t: continue
# iterate over powers of x mod n
for r in range(1, s):
x = pow(x, 2, n)
if x == t: break
if x == 1: return False
else: return False
return True
def is_probable_fermat_prime(n):
if n & 1 != 1:
return False
if ((1 << n.bit_length() - 1) + 1) != n:
return False
return miller_rabin(n)
def parse_number(s):
n = int(s)
if is_probable_fermat_prime(n):
return ~n
return n
def get_line():
line = input("> ")
num1, op, num2 = line.split()
num1 = parse_number(num1)
num2 = parse_number(num2)
op = op.strip()
if op == "+":
print(num1 + num2)
if op == "-":
print(num1 - num2)
if op == "/":
print(num1 / num2)
if op == "*":
print(num1 * num2)
if __name__ == "__main__":
while True:
get_line()
On Sat, Dec 7, 2013 at 6:51 PM, David Goldsmith <[email protected]>wrote:
> Date: Sat, 7 Dec 2013 11:33:13 -0800
>> From: geremy condra <[email protected]>
>> To: Seattle Python Interest Group <[email protected]>
>> Subject: Re: [SEAPY] Running Python on ARM processors
>>
>
>
>> Imagine taking mathematical notation
>> and making all the symbols mean something different for fermat primes, or
>> on every second Tuesday.
>>
>
> Sounds like fun! Can I play? ;-)
>
> DG
>
>