Skip to content

Instantly share code, notes, and snippets.

@dlubarov dlubarov/
Created Nov 9, 2013

What would you like to do?
Enumerate the primes using only unary arithmetic
def is_prime(n):
if n in ('', '1'):
return False
d = '11'
while difference(n, d):
if is_divisible(n, d):
return False
d += '1'
return True
def is_divisible(a, b):
while not is_greater(b, a):
a = difference(a, b)
return a == ''
def difference(a, b):
if is_greater(b, a):
raise Exception('second operand was greater')
while a and b:
a = a[1:]
b = b[1:]
return a
def is_greater(a, b):
while a and b:
a = a[1:]
b = b[1:]
return a != ''
n = ''
while True:
if is_prime(n):
print n
n += '1'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.