/*
* A189416.c
* Calculates the entries of Sloane's A189416 (counting parallelograms on an n X n grid)
*
* Created by Nathaniel Johnston (nathaniel@nathanieljohnston.com)
* April 23, 2011
*/

#include <math.h>
#include <stdio.h>

unsigned char pt[256][256];
FILE *file;

unsigned char isParallelogram (unsigned short a1, unsigned short a2, unsigned short b1, unsigned short b2, unsigned short c1, unsigned short c2, unsigned short d1, unsigned short d2)
{
    int sd1, sd2, slp;
    sd1 = (b1-a1) + (c1-d1);
    sd2 = (b2-a2) + (c2-d2);
    slp = (b1-a1)*(c2-a2) - (c1-a1)*(b2-a2);

    if(sd1==0 && sd2==0 && slp!=0)return 1;
    return 0;
}

int main ()
{
    unsigned short a, b, c, d, a1, a2, b1, b2, c1, c2, d1, d2, maxsz, sz, sz2;
    unsigned long long numPts;

    printf("This tool will calculate the entries of Sloane's A189416 (number of parallelograms on an n X n grid).\nPlease enter the number of terms to compute (an integer from 1 to 255): ");
    scanf("%d",&maxsz);
    numPts = 0;

    file = fopen("b189416.txt","w");
    fprintf(file,"1 0\n");
    fclose(file);
    printf("1 0\n");

    for(sz=2;sz<=maxsz;sz++){
        numPts = 0;
        sz2 = sz*sz;
        for(a=0;a<sz2;a++){
            a1 = a % sz; a2 = floor(a/sz);
            for(b=a+1;b<sz2;b++){
                b1 = b % sz; b2 = floor(b/sz);
                for(c=b+1;c<sz2;c++){
                    c1 = c % sz; c2 = floor(c/sz);
                    for(d=c+1;d<sz2;d++){
                        if(isParallelogram(a1, a2, b1, b2, c1, c2, d % sz, floor(d/sz))){
                            numPts++;
                        }
                    }
                }
            }
        }
        file = fopen("b189416.txt","a");
        fprintf(file,"%d %d\n",sz,numPts);
        fclose(file);
        printf("%d %d\n",sz,numPts);
    }

    printf("Done!");
    getchar();
    return 0;
}