A Futurama Math   Conversation with David X. Cohen
David X. Cohen's Fermat Near-Miss Finders in C

From DXC: The programs are extremely similar, but the improved version DOES do a parity check, so that definitively answers the question of whether I was looking out for that! I have made no change whatsoever except to add a comment at the top explaining what it is. For some reason, I used x,y,z, and i instead of a,b,c, and n. Probably due to my programming habit of using a,b,c for integer variables and x,y,z for floating point. The program is not necessarily a model of elegance since I never expected anyone else to look at it!

Original Program

/*
   Fermat Near-Miss Finder

   Written by David X. Cohen
   May 11, 1995.

   This program generated the equation:

   1782^12 + 1841^12 = 1922^12

   For "The Simpsons" episode "Treehouse Of Horror VI".
   Production code: 3F04
   Original Airdate: October 30, 1995
*/

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

main()
{
  double x, y, i, z, az, d, upmin, downmin;

  upmin = .00001;
  downmin = -upmin;

  for(x = 51.0; x <= 2554.0; x ++)
    {
      printf("[%.1f]", x);

      for(y = x + 1.0; y <= 2555.0; y ++)
        {
          for(i = 7.0; i <= 77.0; i ++)
            {
              z = pow(x, i) + pow(y, i);

              if(z == HUGE_VAL) {
                printf("[*]");
                break;  }

              z = pow(z, (1.0/i));

              az = floor(z + .5);
              d = z - az;

              if(az == y) break;

              if((d < 0.0) && (d >= downmin))
                {
                  downmin = d;

                  printf("\n%.1f, %.1f, %.1f, = %13.10f\n", x, y, i, z);
                }

              else if((d >= 0.0) && (d <= upmin))
                {
                  upmin = d;

                  printf("\n%.1f, %.1f, %.1f, = %13.10f\n", x, y, i, z);
                }

              if(z < (y + 1.0)) break;
            }
        }
    }

  return(1);
}

Improved Program


/*
   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
*/

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


#define EVEN(x)  ((((x) / 2.0) == floor((x) / 2.0)) ? 1 : 0)


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 <= 4999.0; x ++)
    {
      printf("[%.0f]", x);

      evenx = EVEN(x);
      
      for(y = x + 1.0; y <= 5000.0; 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);
}


Dr. Sarah J. Greenwald, Appalachian State University


Legal Notice: The Simpsons and Futurama TM and copyright Twentieth Century Fox and its related companies. This web site is for educational use only. To view these pages, you must agree to these terms. We do not benefit financially in any way from this web site. Images on these pages were taken from episodes that are copyrighted by Twentieth Century Fox. We will not distribute audio, video or image files.
Disclaimer: This web site, its operators, and any content contained on this site relating to The Simpsons or Futurama are not specifically authorized by Fox.