Project Euler #4
This one also took a bit more time than I would have liked, but after fixing the problem I understood how I missed it. Before if the left value ( left * right ) was higher it'd be considered the "largest" palindrome, which was incorrect. I hadn't realized this until I started "debugging" my thoughts and the math behind everything, eventually I checked made the program check the next palindrome up to see if it were larger than the current "answer", if it was larger it'd replace it as the answer. This is pretty easy to understand, so I won't go into explaining it:
int Reverse( int n )
{
int tO = n, t1 = 0;
while( tO > 0 )
{
t1 *= 10;
t1 += tO % 10;
tO /= 10;
}
return t1;
}
int Problem4()
{
int Answer = 0;
int Maybe = 0;
for( int i = 999; i >= 100; i-- )
{
for( int c = 999; c >= 100; c-- )
{
if( ( i * c ) == Reverse( i * c ) )
{
Maybe = ( i * c );
if( Maybe > Answer ) Answer = ( i * c );
}
}
}
}
Project Euler #3
Woo, that one was harder than I would have liked. Okay, well not so much hard, but tedious. My first couple iterations of this program took more than 20 minutes to find number I eventually found out I didn't need! But I eventually optimized it and found the answer, and wrote it in a way so that if I ever encounter this problem somewhere else with an even bigger number I'll be ready.
So First off, I had to write two of my own functions, with a bit of help from the internet of course. One was a function for square rooting 64bit numbers:
double Sqrt64( double number )
{
double t1 = 0, t2 = 0, t3 = 0;
while( ( t1 * t1 ) <= number ) t1 += 0.1;
t2 = t1;
for( double c = 0; c < 10; c++ )
{
t3 = number;
t3 /= t2;
t3 += t2;
t3 /= 2;
t2 = t3;
}
return t3;
}
And the other function which would check if a number was Prime or not:
// Int64 is defined in my header as: #define Int64 _int64
bool IsPrime( Int64 Number )
{
bool IsPrime = true;
for( int i = 2; i < Number + 1; i++ )
{
if( i != Number && Number != 1 )
{
if( Number % i == 0 ) IsPrime = false;
}
}
return IsPrime;
}
After those I could finally get to the actual solution to Problem 3, which used this C++ code written by me ( hurr ):
int Problem3()
{
Int64 Answer = 0, Total = 1, Value = 600851475143;
for( Int64 i = 1; i < Sqrt64( Value ); i++ )
{
if( IsPrime( i ) )
{
if( Value % i == 0 && i != Value )
{
Total *= i;
if( Total == Value )
{
Answer = i;
i = Sqrt64( Value );
}
}
}
}
return Answer;
}
Basically what I'm doing in all of that is Defining 3 variables as 64 Bit Integers: Answer, which as before is going to be our return value, and solution. Total, which is used as a container to add the product of all prime factors found during run-time. And Value, which is just so I don't have to keep typing 600851475143 into the program a bunch.
After defining the variables we run through a for loop with an initial value of 1 that after incrementing cannot exceed the square root of our given Value, which in this case is 600851475143. Inside this loop we check if the initialized variable: 'i', is Prime using our handy dandy IsPrime() function. Within that conditional we have another that checks to see if i is a factor of our Value and isn't actually our value. After checking that we need to multiply i to our Total and then check if our Total has met our Value, when that happens we have found all Prime Factors of our given Value. If we have met our Value we assign the last given prime factor to our Answer and then set 'i' to the square root of our Value just so we get out of the loop. And bam, you have your answer to problem 3.
Project Euler #2
Just finished problem 2 of Project Euler. Another simple one, sadly I was set back a good 30 minutes because of an evil semi-colon hiding in the mist of my syntax. Anyways, here's my solution in C++:
int Problem2()
{
int Answer = 0, FTerm = 0, i1 = 0, i2 = 0;
while( FTerm < 4000000 )
{
FTerm = i1 + i2;
i1 = i2; i2 = FTerm;
if( !(FTerm % 2) ) Answer += FTerm;
}
return Answer;
}
I'll try and explain that more than I did the last problem. I create 4 int variables: Answer, our return value. FTerm, the current number in our Fibonacci sequence. i1 and i2, the two previous numbers from our Fibonacci sequence.
What we do is setup a while loop that keeps pushing through our sequence until FTerm exceeds 4,000,000. Inside this loop we redefine FTerm with the sum of our two previous values. Then we use the modulo ( %) operator to find out if the current FTerm value is even by checking if there is any remainder when dividing the FTerm value by 2, if there is any remainder it is not an even number, if there isn't then it's even. If the FTerm value is even we finally add it to the Answer.
Project Euler #1
Started working on the Project Euler problems. Just going to work my way through all of them one at a time. I'm hoping it'll be an amazing learning experience.
The solution to problem #1 in C++:
int Problem1()
{
for( int i = 1; i < 1000; i++ )
{
if( i % 3 == 0 || i % 5 == 0 ) Answer += i;
}
return Answer;
}
Simple enough, had myself a bit confused at first though making a silly mistake. Instead of checking if the value was divisible for both numbers I was checking if it was divisible for one and then checking if it was divisible for the other, meaning I was adding the multiple twice if it worked for both. But I realized my mistake and fixed it, slightly proud of myself.
MString
Programmed my own little string class based off std::string. It's nothing special, but it's nice to have my function to edit while I need it, this way I don't need to make tons of code changes over 10 different files to do one thing, instead I can just add the function to the class or edit one and bam!
So far the only functions I've added to the Mobility String that aren't standard in a normal string are:
- MString MakeUpper(), which converts the string to all caps.
- MString MakeLower(), the exact same as MakeUpper() but to lowercase.
- MString Chunk( int begin, int end ), just a simplified substr function. Parameter "end" is optional, if it's not provided it just starts from "begin" and goes t'll the end of the string.
- const wchar_t *WStr(), returns the string in a wide character array.
- static const char *Narrow( wchar_t *str ), returns a given wide array as a simple char array. Because it's static you can use it outside a specific object.
- static const wchar_t *Widen( char *str ), the exact same as Narrow( wchar_t *str ) but widens the char array.
Obviously it isn't complete, but I'll add features as I need them. Which is the beauty of this class for me. Anyways, off to copying Garry's Hot Compiling.