Skip to content

Instantly share code, notes, and snippets.

@kylegibson
Created March 5, 2015 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kylegibson/5bb6b568a896355fb373 to your computer and use it in GitHub Desktop.
Save kylegibson/5bb6b568a896355fb373 to your computer and use it in GitHub Desktop.
Fermat near miss finder
/*
Fermat Near-Miss Finder
Written by David X. Cohen
February 3, 1998, 3:24 AM.
This program generated the equation:
3987^12 + 4365^12 = 4472^12
For "The Simpsons" episode "The Wizard of Evergreen Terrace".
Production code: 5F21
Original Airdate: September 20, 1998
HOW TO COMPILE:
$ gcc fermat.c -o fermat -lm
This will stop at 5000.
Increase the UPPER_BOUND if desired.
*/
#include <stdio.h>
#include <math.h>
#define UPPER_BOUND 5000.0
#define EVEN(x) ((((x) / 2.0) == floor((x) / 2.0)) ? 1 : 0)
int main()
{
double x, y, i, z, az, d, upmin;
double bigboy;
int evenx, eveny, evenz;
upmin = .001;
bigboy = 9.99 * pow(10.0, 99.0);
for(x = 2990.0; x <= UPPER_BOUND - 1; x ++)
{
printf("[%.0f]", x);
evenx = EVEN(x);
for(y = x + 1.0; y <= UPPER_BOUND; y ++)
{
eveny = EVEN(y);
for(i = 7.0; i <= 77.0; i ++)
{
z = pow(x, i) + pow(y, i);
if(z == HUGE_VAL) {
printf("[*]");
break; }
if(z > bigboy) break;
z = pow(z, (1.0/i));
az = floor(z + .5);
d = z - az;
if(az == y) break;
if(d < 0.0) continue;
evenz = EVEN(az);
if(evenx == eveny)
{
if(!evenz) continue;
}
else if(evenz) continue;
if(d <= upmin)
{
upmin = d;
printf("\n%.1f, %.1f, %.1f, = %13.10f\n", x, y, i, z);
}
if(z < (y + 1.0)) break;
}
}
}
return(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment