Skip to content

Instantly share code, notes, and snippets.

@EnricoMiccoli
Created July 4, 2016 21:01
Show Gist options
  • Save EnricoMiccoli/936e351d5236397b6cfaa9d2c1a1424f to your computer and use it in GitHub Desktop.
Save EnricoMiccoli/936e351d5236397b6cfaa9d2c1a1424f to your computer and use it in GitHub Desktop.
Slowly compute pythagorean triples with pyDatalog
from pyDatalog import pyDatalog as pyd
import sys
pyd.create_terms('L1, L2, I, triples')
def triples(maxleg):
return (L1.in_(range(1,maxleg)) &
L2.in_(range(1,maxleg)) &
I.in_(range(1, 2 * maxleg)) &
(I ** 2 == (L1 ** 2 + L2 ** 2))
)
# 2*maxleg is a nice integer upperbound for sqrt(2 maxleg^2)
# also valid because of the triangular inequality
s = triples(int(sys.argv[1]))
print('\n'.join([str(x) for x in sorted(s) if x[0] < x[1]]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment