Skip to content

Instantly share code, notes, and snippets.

@dlubarov
Created November 9, 2013 06:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlubarov/7382357 to your computer and use it in GitHub Desktop.
Save dlubarov/7382357 to your computer and use it in GitHub Desktop.
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