Oxaric’s Blog

A compendium of amazing things…

Euler Project Problem #99 Solution

Posted by oxaric on November 24, 2008

It uses C.


Click to directly download euler-solution-99.c

Right click to save the needed data file base_exp.txt

// filename 'euler-solution-99.c'
// By: Louis Casillas, oxaric@gmail.com

// Euler Problem #99
// Determine which line number that has the greatest numerical value.
// Calculate by: first number ^ second number
// The file is 'base_exp.txt'

#include <stdio.h>
#include <gmp.h>

int main()
{
   mpz_t temp_result;
   mpz_init( temp_result );

   mpz_t max_result;
   mpz_init( max_result );
   mpz_set_ui( max_result, 0 );

   unsigned long int base;
   unsigned long int exp;
   unsigned long int number_holder = 0;
   
   unsigned int line_number = 0;
   unsigned int max_line_number = 1;

   char current_char;

   FILE *in_file = fopen("base_exp.txt", "rt");

   while( (current_char = fgetc( in_file )) != EOF )
   {   
      if ( current_char == '\r' )
      {
      }
      else if ( current_char == '\n' )
      {
         exp = number_holder;
         number_holder = 0;
         line_number++;

         mpz_ui_pow_ui( temp_result, base, exp );
         
         if ( mpz_cmp( temp_result, max_result ) > 0 )
         {
            mpz_set( max_result, temp_result );
            max_line_number = line_number;
         }
         
         printf( "\nline number: %d\n", line_number );

         base = 0;
         exp = 0;
      }
      else if ( current_char == ',' )
      {
         base = number_holder;
         number_holder = 0;
      }
      else
      {
         number_holder *= 10;
         number_holder += (current_char - '0');
      }
   }

   printf( "\n\nThe max line number is: %lu\n\n", max_line_number );

   //mpz_ui_pow_ui( temp, base, exp );

   //mpz_out_str( out_file, 10, temp );

   //printf("\n");
   //mpz_out_str( stdout, 10, temp );
   //printf("\n\n");
   
   fclose( in_file );

   printf("\n\n");

   return 0;
}

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>