Created
November 9, 2013 06:15
-
-
Save dlubarov/7382357 to your computer and use it in GitHub Desktop.
Enumerate the primes using only unary arithmetic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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