You are using a version without Ads of this website. Please, consider donating:

### Help with c++ projecteuler problem

Hey I am working on http://projecteuler.net/problem=4 and this is the program I have I don't know why its not working! Please help, looks like it is in an infinite loop for some reason...

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````int main() { long a, b; long biggest = 0; a = 999; b = 999; bool trueistrue = 1; for(;a != 0 && b != 0;) { long num, rem,numCopy; long sum = 0; numCopy = a * b; num = a * b; while(num!=0) { rem=num%10; num=num/10; sum=sum*10+rem; } if(numCopy == num) { if(biggest < num) biggest = num; } if(trueistrue) b = b - 1; else a = a - 1; if(b == 0) { trueistrue = 0; b = a - 1; a = 999; } if(a == 0) { trueistrue = 1; a = b - 1; b = 999; } } cout << biggest << " is the answer a = " << a << " b = " << b; cin.get(); cin.get(); return 0; }``````
Last edited on
May you tell us more about your problem?...
This is the code from the time of writing:

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````int main() { long a, b; long biggest = 1; a = 999; b = 999; bool trueistrue = 1; for(;a != 0 || b != 0;) { if(b == 1 && a == 2) break; long num, rem,numCopy; long sum = 0; numCopy = a * b; num = a * b; while(num!=0) { rem=num%10; num=num/10; sum=sum*10+rem; } if(a * b == num) { if(biggest < a * b) biggest = a * b; } if(trueistrue) b = b - 1; else a = a - 1; if(b == 0) { trueistrue = 0; b = a - 1; a = 999; } if(a == 0) { trueistrue = 1; a = b - 1; b = 999; } } cout << biggest << " is the answer a = " << a << " b = " << b; cin.get(); cin.get(); return 0; }``````

The problem now is that biggest doesn't change from its original value. I don't know why, that is what I am wondering about, I don't need anyone to write my program just tell me why biggest doesn't change. As for what the thing I am making is you can click that link in my original post...
Can someone help me understand
 ``123456`` ``````while(num!=0) { rem=num%10; num=num/10; sum=sum*10+rem; }``````

I found it online and made the rest of the program. It may have a flaw that makes my program un usable.
I solved it, took longer than it should! But I still don't understand all of this:
 ``123456`` ``````while(num!=0) { rem=num%10; num=num/10; sum=sum*10+rem; }``````
Last edited on
> But I still don't understand all of this

To check if the number d0d1d2d3 is a palindrome:

check if d0d1d2d3 == d3d2d1d0

d3d2d1d0 == d3 * 1000 + d2 * 100 + d1 * 10 + d0 == ( ( ( ( d3 * 10 ) + d2 ) *10 ) + d1 ) * 10 ) + d0

> took longer than it should!

a. For positive a and b, a*b is greater than a*(b-1), a*(b-2) etc., we can omit those checks.

b. 789 * 687 == 687 * 789, we need not check both

c. The check for a number being a palindrome is far more expensive than the check if a number is greater than another number. We can place the inexpensive check first.

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include bool is_palindrome( int number ) { int n = number, reverse_number = 0 ; while( n ) { reverse_number = reverse_number * 10 + n % 10 ; n /= 10 ; } return number == reverse_number ; } int main() { enum { SMALLEST = 100, LARGEST = 999 } ; int largest_palindrome = 0 ; int num1 = 0, num2 = 0 ; for( int i = LARGEST ; i >= SMALLEST ; --i ) for( int j = i-1 ; j >= SMALLEST ; --j ) { int product = i * j ; if( ( product > largest_palindrome ) && is_palindrome(product) ) { num1 = i ; num2 = j ; largest_palindrome = product ; break ; // no need to check for smaller values of j } } std::cout << num1 << " * " << num2 << " == " << largest_palindrome << '\n' ; // 993 * 913 == 906609 }``````
thank you very much that helped me very much! :)
Just to further your education a little:

`bool trueistrue = 1;`

Stick to the the idea that bool variables are either true or false - then re-evaluate this piece of code to come to an inescapable conclusion.

With use of this variable in this code:

 ``123`` `````` if(a == 0) { trueistrue = 1;``````

This translates to: if a is false, then trueistrue is true. A lesson in nightmare confusion. lol

 ``1234567891011`` ``````if(a == 0) //can be written if(!a) {} // if not a, in other words if a equals false //can also do this if (a) // is variable a true? bool Quit = false; //user wants to quit Quit = true;``````

With this:

`for(;a != 0 || b != 0;) {}`

We have `for(initialise none; end-condition; increment none) {}`

A while loop looks like this `while(end-condition){}` Do you see my point here?

I hope this has cleared up some concepts for you, good luck with your coding, and have fun !!
Thanks for the lesson, I didn't understand very much of that but atleast I got the program to work and I understand the algorythm decently. The reason I called it trueistrue is just because it is so fun to make trueistrue false :P But I made the program overly complicated and stupid in the first few posts...

Edit: Actually TheIdeasMan I understand it :)
Last edited on
Topic archived. No new replies allowed.

You are using a version without Ads of this website. Please, consider donating: