Project Euler Problem 4?

Hi, I'm trying Project Euler's Problem 4 and I'm having trouble finding the correct answer (I know my method isn't the most efficient, but I was planning on improving it after I find the answer with this first method). I keep getting the number 580085 as the answer but Project Euler says it's wrong. I'm really stumped as to what would be wrong about my code. Any help would be awesome.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*	PROJECT EULER: PROBLEM 4:

	A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

	Find the largest palindrome made from the product of two 3-digit numbers.
*/


#include <iostream>

using namespace std;


bool isPalindrome(int product){

	if(product/100000 < 1){

		int one = product%10;
		int two = (product%100 - product%10)/10;
		int three = (product%1000 - product%100)/100;
		int four = (product%10000 - product%1000)/1000;
		int five = (product%100000 - product%10000)/10000;

		if((one == five) && (two == four)){
			return true;
		}else{
			return false;
		}

	}else{

		int one = product%10;
		int two = (product%100 - product%10)/10;
		int three = (product%1000 - product%100)/100;
		int four = (product%10000 - product%1000)/1000;
		int five = (product%100000 - product%10000)/10000;
		int six = (product%1000000 - product%100000)/100000;

		if((one == six) && (two == five) && (three == four)){
			return true;
		}else{
			return false;
		}
	}
}


int main(){

	for(int i = 100; i < 999; i++){
		for(int j = 100; j < 999; j++){

			int product = i*j;
			if(isPalindrome(product)){
				cout << product << '\n';
			}

		}
	}


	return 0;
}
Last edited on
Nevermind! I figured it out :D

I didn't take notice that I wasn't printing out the palindromes in least to greatest order.

This is my updated code that worked:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*	PROJECT EULER: PROBLEM 4:

	A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.

	Find the largest palindrome made from the product of two 3-digit numbers.
*/


#include <iostream>
#include <vector>

using namespace std;


bool isPalindrome(int product){

	if(product/100000 < 1){

		int one = product%10;
		int two = (product%100 - product%10)/10;
		int three = (product%1000 - product%100)/100;
		int four = (product%10000 - product%1000)/1000;
		int five = (product%100000 - product%10000)/10000;

		if((one == five) && (two == four)){
			return true;
		} else{
			return false;
		}

	}else{

		int one = product%10;
		int two = (product%100 - product%10)/10;
		int three = (product%1000 - product%100)/100;
		int four = (product%10000 - product%1000)/1000;
		int five = (product%100000 - product%10000)/10000;
		int six = (product%1000000 - product%100000)/100000;

		if((one == six) && (two == five) && (three == four)){
			return true;
		}else{
			return false;
		}
	}
}


int main(){

	vector<int> palindrome;

	for(int i = 100; i < 999; i++){
		for(int j = 100; j < 999; j++){

			int product = i*j;
			if(isPalindrome(product)){
				palindrome.push_back(product);
			}

		}
	}


	/* Finding the largest number */

	int greatest = palindrome[0];
	for(int i=0; i < palindrome.size(); i++){
		if(greatest < palindrome[i]){
			greatest = palindrome[i];
		}
	}

	cout << greatest << '\n';

	return 0;
}


But if anyone does stumble across this, are there any recommendations for a better way to go about solving this problem? I feel like this method is wildly inefficient.
Last edited on
When you solve a Project Euler problem you gain access to a forum where other people have solved the problem. Perusing that forum might be informative.
Topic archived. No new replies allowed.