Hello, friends.

I did the fourth challenge on Project Euler and I'd like to know what I can do to improve the efficiency of my code. Right now it takes 1.562s to run and the source code is as follows:

#include <iostream>
#include <string.h>

using namespace std;

int Number = 0;
int Reversed_Number = 0;
int First_Number = 0;
int Second_Number = 0;
int Product = 0;
int Biggest_Palindrome = 0;
bool Is_Palindrome = false;
string Text_Valued_Number = "";
string Text_Valued_Reversed_Number = "";

void Reverse_Number (int);
void For_Loop (int,int);
void Check_if_Number_is_a_Palindrome(int,int);
void Show_Number(int);

void For_Loop(int First_Number, int Second_Number)
{
    for (First_Number = 999; First_Number >= 100; First_Number--)
    {
        for (Second_Number = First_Number; Second_Number >= 100;Second_Number--)
        {
        Product = First_Number * Second_Number;
        Reverse_Number(Product);
        if (Is_Palindrome == true)
        {
            if(Product > Biggest_Palindrome)
            {
            Biggest_Palindrome = Product;
            }
        }
        }
    }
    Show_Number(Biggest_Palindrome);
}

void Reverse_Number (int Product)
{
    string First = ""; string Second = ""; string Third = ""; string Fourth = ""; string Fifth = ""; string Sixth = "";
    Text_Valued_Number = to_string(Product);
    if (Product <= 100000)
    {
   First = Text_Valued_Number[4]; Second = Text_Valued_Number[3]; Third = Text_Valued_Number[2];
   Fourth = Text_Valued_Number[1],Fifth = Text_Valued_Number[0];
   if(First == "0")
   {
       Text_Valued_Reversed_Number = Second +  Third + Fourth + Fifth;
   }
   else
   {
      Text_Valued_Reversed_Number = First + Second +  Third + Fourth + Fifth;
   }
   Reversed_Number = stoi(Text_Valued_Reversed_Number);
    }
    else if (Product > 100000)
    {
   First = Text_Valued_Number[5]; Second = Text_Valued_Number[4]; Third = Text_Valued_Number[3];
   Fourth = Text_Valued_Number[2], Fifth = Text_Valued_Number[1]; Sixth = Text_Valued_Number[0];
    if(Sixth == "0")
   {
       Text_Valued_Reversed_Number = Second +  Third + Fourth + Fifth + Sixth;
   }
   else
   {
      Text_Valued_Reversed_Number = First + Second +  Third + Fourth + Fifth + Sixth;
   }
   Reversed_Number = stoi(Text_Valued_Reversed_Number);
    }
   Check_if_Number_is_a_Palindrome(Product, Reversed_Number);
}

void Check_if_Number_is_a_Palindrome(int Product, int Reversed_Number)
{
    if (Product == Reversed_Number)
    {
        Is_Palindrome = true;
    }
    else
        Is_Palindrome = false;

}

void Show_Number(int Biggest_Palindrome)
{
    cout << Biggest_Palindrome << endl;
}

int main()
{

    For_Loop(First_Number, Second_Number);
    return 0;
}

Any help would be appreciated.

Thank you,
Petcheco

Recommended Answers

All 5 Replies

Change line 73. Here I would definitely opt for Is_Palindrome = Product == Reversed_Number; and remove the Check_if_Number_is_a_Palindrome function.

Some nit-picking here. Your functions set global variables instead of returning values. Change that so that the results of a functional computation is returned without setting a global variable. Your current practice will come back and bite you in the future unless you change it. IE, instead of void Reverse_Number (int Product), the function should be int Reverse_Number (int Product) and it will return the reversed number.

@rubberman

Could you explain the difference between doing what you said and what I was doing and why it's important to do what you said?

Thank you very much.

Up!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.