Begginer simple program help. (math related)

Here is the assignment (not for marks).

Situation

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

Deliverable

Code a solution that outputs all palindromic numbers made from the product of two 3-digit numbers.

Once complete, output a statement that tells the user the largest possible palindromic number found.
*******************************************************************************

Can someone fix my code? Doesen't seem to work.

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
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    #include <string>

    using namespace std;

    int main ()
    {
            int mulcon = 99;
        do
       {

            mulcon++;

         for (long int mul = 99; mul < 1000; mul++)                                 
        {
            int mul2 = 99 ;
            mul2++;

            int reverse; 
            int no = mul * mul2; 
            int digit ; 

            while(no !=0) 
            { 
                digit = no%10;  
                reverse = reverse*10+digit; 
                no=no/10; 
            } 

            if ( no == reverse )
            {
              cout << no << endl;
            }
        }

       }  while ( mulcon < 1000 );

    system ("pause");
    return 0;
    }
1. Where do you use mulcon? You just increase it, so the for loop is executed 900 times with the same result
2. mul2 is always 100
3. Observation: on line 16 you don't need a long int
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
78
79
80
81
82
83
84
#include <iostream>
#include <vector>
#include <iomanip>
#include <algorithm>

using namespace std;

int palTest(int arg, int result)
{
    if(arg == 0)
	return result;
    return palTest(arg/10, 10*result + arg%10);
}

int main()
{
    int i = 0, j = 0; 
    int factor1, factor2;
    int contor = 0;
    int num, max = 0;
    vector<int> palin;
    while(i * j < 998001)				// max(i*j) is 999*999 = 998001 
    {							// which is not palindrome
	for(i = 101 ; i < 1000; i++)
	{
	    for(j = 101; j < 1000; j++)
	    {
		num = i * j;
		if (palTest(num, 0) == num)		// num is palindrom?
		{
		    ++contor;				// palindrom counting
		    if(num > max)			// testing for actual max
		    {
			max = num;			// num is actual max
			factor1 = i; factor2 = j;	// and i and j are max's 3-digit factors
		    }
		    palin.push_back(num);		// vector palin is populated with palindromes
		}
	    }
	}
    }	
    vector<int> vect;					// after the next for loop vect will contain
    for (i = 1; i < contor; i++)			// palindromes without duplicates(in the above two for loops
    {							// the factors i and j generate duplicate numbers,
	bool same = false;				// e.g.: 906609 = 913 * 993 = 993 * 913 and so on)
	for (j = 0; (j < i) && (same == false); j++) 
	if(palin[i] == palin[j]) same = true;
	if (!same) 
	    vect.push_back(palin[i]);			// vect contains only distinct palindromes.
    }	
    sort(vect.begin(), vect.end());			// for a cute output vect is sorted
    int tab = vect.size() / 6;
    for(int i = 0 ; i < tab ; i++)
    {
	cout << setw(4) << i + 0 * tab << setw(7) << vect[i + 0 * tab] << setw(6);
	           cout << i + 1 * tab << setw(7) << vect[i + 1 * tab] << setw(6);
	           cout << i + 2 * tab << setw(7) << vect[i + 2 * tab] << setw(6);
	           cout << i + 3 * tab << setw(7) << vect[i + 3 * tab] << setw(6);
	           cout << i + 4 * tab << setw(7) << vect[i + 4 * tab] << setw(6);
	           cout << i + 5 * tab << setw(7) << vect[i + 5 * tab] << '\n';
    }
    cout << "\nThere are " << vect.size() << 
    " palindromes and the greatest is " << max << " = " 
    << factor1 << " * " << factor2 << '\n';
		
    return 0;
}

Output two fragments of the huge list of palindromes as was required in the problem statement:

   0  11211   109  40504   218  63536   327  88088   436 232232   545 487784
   1  12221   110  40704   219  63736   328  88288   437 234432   546 488884
   2  12321   111  40804   220  63936   329  88688   438 235532   547 489984
   3  13231   112  41114   221  64246   330  88788   439 238832   548 491194
   4  13431   113  41314   222  64446   331  88888   440 239932   549 493394
   5  14241   114  41514   223  64546   332  89198   441 242242   550 505505
.............................................................................
 104  39693   213  62726   322  87078   431 221122   540 474474   649 855558
 105  39893   214  62826   323  87178   432 222222   541 476674   650 861168
 106  40004   215  62926   324  87278   433 225522   542 477774   651 886688
 107  40304   216  63036   325  87478   434 227722   543 484484   652 888888
 108  40404   217  63336   326  87978   435 231132   544 485584   653 906609

There are 654 palindromes and the greatest is 906609 = 913 * 993
Skipping the first part: Code a solution that outputs all palindromic numbers made from the product of two 3-digit numbers.

For the second part: Output a statement that tells the user the largest possible palindromic number found.
I remember having seen an elegant solution somewhere on the web.

It went something like this:

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
#include <iostream>
#include <algorithm>

bool is_palindrome( int n ) // invariant: n is a positive six digit number
{
     return n/100000 == n%10 && // first digit == last digit
             (n/10000)%10 == (n/10)%10 && // second digit == fifth digit
             (n/1000)%10 == (n/100)%10 ; // third digit == fourth digit
}

int main()
{
    int max_so_far = 100001 ;
    int first = 0 ;
    int second = 0 ;

    // the six digit numbers are of the form a * b,
    // where a and b are three digit numbers with a <= b
    // all six digit palindromes are divisible by 11
    // therefore either a or b must be divisioble by 11
    for( int a = 999 ; a > 99 ; --a )
    {
        const int min_b = std::max( max_so_far/a, a ) - 1 ;
        int init_b = 999 ;
        int decrement = 1 ;

        if( a%11 != 0 ) // a is not divisible by 11
        {
            // b must be divisible by 11
            init_b = 990 ;
            decrement = 11 ;
        }

        for( int b = init_b ; b > min_b ; b -= decrement )
        {
            const int n = a * b ;
            if( is_palindrome(n) ) // this is the largest palindrome so far
                        // (smaller ones are eliminated by our choice of min_b)
            {
               max_so_far = n ;
               first = a ;
               second = b ;
               break ;
            }
        }
    }

    std::cout << max_so_far << " (" << first << 'x' << second << ")\n" ;
}

http://coliru.stacked-crooked.com/a/509969c79816c2af
Topic archived. No new replies allowed.