// Arup Guha
// 11/7/2014
// Code for solution to Homework 5c Question 2

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

#define REPEAT 100

void pick(int* ptrA, int*ptrB, int prime);
int count(int prime, int a, int b);

int main() {

    srand(time(0));

    // Five spaced out primes from 500 to 1000.
    int primes[5] = {599, 641, 757, 829, 997};
    int i,j;

    // Loop through primes.
    for (i=0; i<5; i++) {

        // Try several a's and b's.
        for (j=0; j<REPEAT; j++) {

            // Get a valid a and b.
            int a, b, prime = primes[i];
            pick(&a,&b, prime);
            while (4*a*a*a % prime == 27*b*b % prime)
                pick(&a, &b, prime);

            // Count points and print.
            int numPts = count(prime,a,b);
            printf("%d\n", numPts);

        }
        printf("\n");
    }
}

// Pick a random a and b and set them from 0 to p-1.
void pick(int* ptrA, int*ptrB, int prime) {
    *ptrA = rand()%prime;
    *ptrB = rand()%prime;
}

int count(int prime, int a, int b) {

    // Brute Force Loop - count all valid points.
    int x, y, cnt=1;
    for (x = 0; x < prime; x++)
        for (y = 0; y < prime; y++)
            if (y*y % prime == (x*x*x +a*x+b) % prime)
                cnt++;
    return cnt;
}
