hkj447
hkj447

Reputation: 737

Finding largest palindrome from product of two 3 digit numbers (Project Euler problem)

I understand various solutions exist to this problem, but after viewing the solutions, I am not sure where my code is failing. Here is my code for reference:

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

char *strrev(char *str)
{
    if (!str || ! *str)
        return str;

    int i = strlen(str) - 1, j = 0;

    char ch;
    while (i > j)
    {
        ch = str[i];
        str[i] = str[j];
        str[j] = ch;
        i--;
        j++;
    }
    return str;
}

int main(){
    long int a,b, ab, maxA, maxB, pal;
    long int maxPal = 0;
    char string[6];
    char revString[6];

    for(a=999; a>100; a--){
        for(b=999; b>100; b--){
            ab = a*b;

            sprintf(string,"%ld",ab);
            strcpy(revString, strrev(string));
            strrev(string);

            if((revString[0] != '0') && (strcmp(string, revString)== 0)){
                printf("Palindrome %ld found!\n", ab);
                pal = ab;
                if(pal > maxPal){
                    maxPal = pal;
                    maxA = a;
                    maxB = b;
                }
            }
        }
    }


    printf("Maximim palindrome is %ld from the product of a = %ld and b = %ld \n", maxPal, maxA, maxB);
    return 1;
}

I am attempting this via a brute force method by going through all pairs of numbers from 100 to 999, converting the product to a string, and then seeing if the string is the same backwards. Doing this however I only find a 5 digit number as the largest palindrome instead of a 6 digit number. I think my logic seems pretty straightforward, but I am coming to the wrong conclusion. Instead of the right answer, I want to understand what is incorrect about my logic.

Upvotes: 1

Views: 50

Answers (1)

pifor
pifor

Reputation: 7882

I have modified your code to count the number of palindroms found:

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

char *strrev(char *str)
{
    if (!str || ! *str)
        return str;

    int i = strlen(str) - 1, j = 0;

    char ch;
    while (i > j)
    {
        ch = str[i];
        str[i] = str[j];
        str[j] = ch;
        i--;
        j++;
    }
    return str;
}

int main(){
    long int a,b, ab, maxA, maxB, pal;
    long int maxPal = 0;
    char string[6];
    char revString[6];
    int n6dp = 0;
    int n5dp = 0;

    for(a=999; a>100; a--){
        for(b=999; b>100; b--){
            ab = a*b;

            sprintf(string,"%ld",ab);
            strcpy(revString, strrev(string));
            strrev(string);

            if((revString[0] != '0') && (strcmp(string, revString)== 0)){
        if (strlen(string) == 6)
        {
            n6dp++;
        }
        if (strlen(string) == 5)
        {
            n5dp++;
        }
                pal = ab;
                if(pal > maxPal){
                    maxPal = pal;
                    maxA = a;
                    maxB = b;
                }
            }
        }
    }


    printf("number of 5 digits palindroms found = %d\n", n5dp);
    printf("number of 6 digits palindroms found = %d\n", n6dp);
    printf("Max. palindrome is %ld from the product of a = %ld and b = %ld \n", maxPal, maxA, maxB);

    return 0;
}

Here is what I get:

$ ./pal
number of 5 digits palindroms found = 1493
number of 6 digits palindroms found = 977
Max. palindrome is 906609 from the product of a = 993 and b = 913 

Your code has found 6 digits palindroms.

Upvotes: 1

Related Questions