Skip to content

Instantly share code, notes, and snippets.

@ashgillman
Created March 6, 2018 05:53
Show Gist options
  • Save ashgillman/7858462290bab7e1faec8dc29f249268 to your computer and use it in GitHub Desktop.
Save ashgillman/7858462290bab7e1faec8dc29f249268 to your computer and use it in GitHub Desktop.
Pythagorean Triple Numbers (Marijn Heule)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char** argv) {
unsigned int a, b, c, max, offset = 0, nVars = 0, nClauses = 0;
if (argc < 2) {
printf ("run using ./ptn-encode MAX [OFFSET=0]\n"); exit (0); }
max = atoi (argv[1]);
if (argc > 2) offset = atoi(argv[2]);
for (a = 1; a <= max; a++)
for (b = a; (a*a + b*b) <= (max * max); b++) {
c = sqrt(a*a + b*b);
if ((c*c) == (a*a + b*b)) {
nClauses += 2;
if (c > nVars) nVars = c; } }
printf ("p cnf %i %i\n", nVars + offset, nClauses);
for (a = 1; a <= max; a++)
for (b = a; (a*a + b*b) <= (max*max); b++) {
c = sqrt(a*a + b*b);
if ((c*c) == (a*a + b*b)) {
printf ("%i %i %i 0\n", a + offset, b + offset, c + offset);
printf ("%i %i %i 0\n", -a - offset, -b - offset, -c - offset); } } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment