Skip to content

Instantly share code, notes, and snippets.

@mortehu
Last active May 18, 2018 22:50
Show Gist options
  • Save mortehu/ccca0bafc7a9caa26d6008379057e3cc to your computer and use it in GitHub Desktop.
Save mortehu/ccca0bafc7a9caa26d6008379057e3cc to your computer and use it in GitHub Desktop.
Prime Triangle
#!/usr/bin/env python3
import fractions
import sys
def is_prime(n):
for d in range(2, int(n**0.5)):
if n % d == 0:
return False
return n > 1
def oeis_a054521(n, k):
return fractions.gcd(n, k) == 1
def gen_tri(n, out=sys.stdout.write):
for row in range(n):
for col in range(row + 1):
ok = False
for j in range(n):
if is_prime(n * row * (row + 1) // 2 + col + j *
(row + 1) + 1):
ok = True
break
out('\033[m' if oeis_a054521(row + 1, col + 1) else '\033[31;1m')
out('_' if ok else '#')
out('\033[m\n')
def main():
gen_tri(int(sys.argv[1]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment