I’ve been trying to learn C++

A math problem can often look super simple before you actually try it and find you have no clue how to solve it. What we really need is more understanding of math, and implementing the algorithms yourself is the best way to do that.

33 = a3 + b3 + c3

The solution to this equation could not be determined.

Here’s my “solution” to the problem. Of course there are massive pitfalls and very few people actually need this : )

/*
The Uncracked Problem with 33
author: Jussi Konttinen
website: un3d.eu
last edited: June 2016
*/

#include <math.h>       /* pow */
#include <iostream>     /* Input/output stream  */
#include <stdlib.h>     /* srand, rand */
#include <time.h>
#include <limits>

using namespace std;

void PressEnterToContinue()
  {
  std::cout << "\nPress ENTER to continue... " << flush;
  std::cin.ignore( std::numeric_limits <std::streamsize> ::max(), '\n' );
  }

// result = a³ + b³ + c³
long cubesum(long a, long b, long c){
  long result;
  result = pow (a,3) + pow (b,3) + pow (c,3);
  return (result);
}

void show_results(long cubesum, long x, long y, long z){
  std::cout << "a^3 + b^3 + c^3 is: " << cubesum << std::endl;
  cout << "Value of a is: " << x << endl;
  cout << "Value of b is: " << y << endl;
  cout << "Value of c is: " << z << endl;
}

int main ()
{

   // local variable declaration:
   int loop = 0;
   long random_a, random_b, random_c;

   // initialize random seed:
   srand (time(NULL));

   // generate random numbers
   long a = rand();
   long b = rand();
   long c = rand();
   long result;

   // do loop execution
   do
   {
       // counting loops
       loop = loop + 1;
       // calling a function to add the values.
       result = cubesum(a, b, c);
       //result = 33; //unit test part 1

   // counting every 10 million iterations
   if (loop % 10000000 == 0 && loop != 0)
   {
       cout << "Solving the equation. Loop No. :" << loop << endl;
   }

   // check the condition
   if( result != 33 )
   {
       // if condition is not true
       a++;
       b = b - 2;
       c = c - 3;
   }
   else //( result == 33 )
   {
       // if condition is true then print the following
       show_results(result, a, b, c);
       cout << "Hip, hip, hurrah!";
       //cout << "Cubesum is " << cubesum(a, b, c) << endl; //unit test part 2
       break;
   }
   // 100 million loops
   }while( loop < 100000000 );

   if( result != 33 ){
       show_results(result, a, b, c);
       cout << "Better luck next time.";
   }

   PressEnterToContinue();
   return 0;
}

Ok, now that you’re already up and running, let’s run the program from within Code::Blocks.

Solving the equation. Loop No. :10000000
Solving the equation. Loop No. :20000000
Solving the equation. Loop No. :30000000
Solving the equation. Loop No. :40000000
Solving the equation. Loop No. :50000000
Solving the equation. Loop No. :60000000
Solving the equation. Loop No. :70000000
Solving the equation. Loop No. :80000000
Solving the equation. Loop No. :90000000
Solving the equation. Loop No. :100000000
a^3 + b^3 + c^3 is: -2147483648
Value of a is: 100005516
Value of b is: -199980621
Value of c is: -299999768
Better luck next time.
Press ENTER to continue...

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s