Skip to content

Instantly share code, notes, and snippets.

@qookei
Created March 13, 2020 20:26
Show Gist options
  • Save qookei/0e229113aba6dac56fe2aef3faac279b to your computer and use it in GitHub Desktop.
Save qookei/0e229113aba6dac56fe2aef3faac279b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
def gen_primes():
D = {}
q = 2
while True:
if q not in D:
yield q
D[q * q] = [q]
else:
for p in D[q]:
D.setdefault(p + q, []).append(p)
del D[q]
q += 1
max = 2400000
print('#include <stdio.h>\n')
print('int main() {')
print('\tlong n = -1;\n')
print('\tprintf("Max number to calculate to?\\n> ");')
print('\tscanf("%ld", &n);\n')
print('\tif (n < 0 || n > 2400000) {')
print('\t\tprintf("%ld is not in range [0, 2400000]\\n", n);')
print('\t\treturn 1;')
print('\t}\n')
print('\tvoid *jmp_table[] = {')
for i in range(0, max + 1):
print(f'\t\t&&label_{i},')
print('\t};\n')
print('\tgoto *jmp_table[n];\n')
nums = {}
i = 0
it = gen_primes()
while i <= max:
prime = next(it)
while i < prime:
nums[i] = False
i += 1
nums[i] = True
i += 1
for i in range(max, -1, -1):
print(f'\tlabel_{i}:')
if nums[i]:
print(f'\tprintf("{i} ");')
print('\n\tprintf("\\nGoodbye!\\n");')
print('}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment