Finding the largest product palindrome of two three-digit numbers

So, on Project Euler, Problem 4 states the following:

The palindromic number reads the same in both directions. The largest palindrome from the product of two two-digit numbers of the number 9009 = 91 99.

Find the largest palindrome of the product of two three-digit numbers.

I tried the following:

    #include <stdio.h>
    #include <stdlib.h>

    int check(int result)
    {
        char b[7];
        sprintf(b, "%d", result);
        if (b[0] == b[5] && b[1] == b[4] && b[2] == b[3])
        {
            return 1;
        }
        else 
        {
            return 0;
        }
    }

    int main () {
        int i;
        int g;
        int final;
        for (i = 999; i > 99; i--)
        {
            for (g = 999; g > 99; g--)
            {
                if (check(g*i) == 1)
                {
                    final = g*i;
                    goto here;
                }
            }
        }
        here:
        printf("%d", final);
}

But that does not work. Instead of the correct answer, I get 580085, which I think is the palindrome at least, but still not the right answer.

Let me explain my program, starting with int main:

  • int iand int gare my factors. These are two three-digit numbers.
  • int final - This is the number that will store the largest palindrome.
  • I start two cycles to get the opportunity of each number.
  • , goto, (, , ).
  • , .

:

  • , , , a char, , 999 * 999, 6, , , , sprintf \0 .
  • , , char , result ( i*g int main) char b[7].
  • b, , , .
  • : 1 2 .

, - . ?

+5
10

:

, .

999*100 = 99900 998*101 = 100798, .

+13

, , , .

:

i = 900, g = 850 -> 765000
i = 880, g = 960 -> 844800

, i, g, .

, , .

+2

, . , . , .

.

  bool found = false;
  for (int i = 998; i >= 100; i--)
  {
    char j[7];
    sprintf(j,"%d",i);
    j[3]= j[2];
    j[4]= j[1];
    j[5]= j[0];
    int x =atoi(j);
    int limit = sqrt((float) x);
    for (int z = 999; z >= limit; z--)
    {
      if (x%z==0){
        printf("%d",x);
        found = true;
        break;
      }
    }
    if (found) break;
  }
+2

,

, i g. , j k :

i > j and
g < k

(, ).

+1

Java:

public class Palindrome {

    public static void main(String[] args)
     {       int i, j;
            int m = 1;
            int k =11;
            boolean flag = false;

            while (true)
            {;
                if (flag) j = m + 1;
                else j = m;

                for (i = k; i > 0; i--)
                {

                    j++;



                    int number, temp, remainder, sum = 0;
                    number = temp = (1000 - i) * (1000 - j);

                    while (number > 0)
                    {
                        remainder = number % 10;
                        number /= 10;
                        sum = sum * 10 + remainder;
                    }

                    if (sum == temp)
                    {
                        System.out.println("Max value:"+temp);

                        return;
                    }


                }

                if (flag)
                    m++;
             k=k+11;
                flag = !flag;
            }

     }

}
+1

. , . , 999 * 999, 999 * 998 .. , 998 * 999, 999 * 998.

, , , - , . . - ...

for (i = 999; i > 99; i--)
{
  for (g = i; g > 99; g--)
  {
...

, , , , , . , . ; 999 * 999, 999 * 998, 998 * 998, 999 * 997, 998 * 997 ..

, , - ():

x = 999;
n = 0;

while (++n <= x)
{
  j = x;
  k = j - n;

  while (j >= k)
  {
    y = j-- * k;
    if (check(y))
      stop looking
  }
}
0
0

All of the above answers are great, but still I could not limit myself to writing code. The code posted by @thyrgle is absolutely perfect. The only minor fix he needs to make is simply to check which product is the maximum. The code may be like

int i,j,max=0,temp;
for(i=999;i>=100;i--){
    for(j=i;j>=100;j--){
        temp=i*j;
        if(isPalin(temp) && temp>max){
            max=temp;
        }
    }
}
cout<<max<<"\n";
0
source
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int a[6];

void convertToString(int xy){
    int i,t=100000;
    for(i=0;i<6;i++){
        a[i]=xy/t;
        xy = xy % t;
        t=t/10;
    }
}

int check(){
    int i;
    for(i=0;i<3;i++){
        if(a[i]!=a[6-i]){
            return 0;
        }
    }
    return 1;
}

void main(){
    int x,y,xy,status=0;
    int i=0,j=0,p=0;
    for(x=999;x>99;x--){
        for(y=x;y>99;y--){
            xy=x*y;
            convertToString(xy);
            status = check();
            if(status==1){
                if(xy>p){
                    p=xy;
                    i=x;
                    j=y;
                }
            }
        }
    }

    printf("\nTwo numbers are %d & %d and their product is %d",i,j,p);

}
0
source
x,y=999,999

k=0

pal=[]

while (y>99):
    while (x>=100):
        m=x*y
        n=x*y
        while (n!=0):
            k=k*10+(n%10)
            n=int(n/10)
        if(m==k):
            if k not in pal:
                pal.append(k)
        x=x-1
        k=0
    else:
        y,x=y-1,999


pal.sort()
print(pal)

he gives 906609 as the largest palindrome number

0
source

All Articles