Here is the recursive function....
private void GeneratePalindromeTotals(double num1, double num2, ref int nc, ref ArrayList ar)
{
ar = new ArrayList();
string rvnum1 = String.Empty;
string rvnum2 = String.Empty;
double rnum = 0;
string snum2 = String.Empty;
string snum1 = num1.ToString();
int lnum1 = snum1.Length;
for (int u = lnum1 - 1; u >= 0; u--)
{
snum2 += snum1.Substring(u, 1);
}
num2 = double.Parse(snum2);
rnum = num1 + num2;
rvnum1 += rnum.ToString();
for (int rv = rnum.ToString().Length - 1; rv >= 0; rv--)
{
rvnum2 += rvnum1.ToString().Substring(rv, 1).ToString();
}
if (!(rvnum1.Equals(rvnum2)))
{
File.AppendAllText(@"C:\WriteLines.txt", "No is : " + num1.ToString() + " , Diff : rvnum1 = " + rvnum1 + " ," + "rvnum2 = " + rvnum2 + " ,nc = " + nc.ToString() + Environment.NewLine);
nc += 1;
if (nc.Equals(26)) return;
GeneratePalindromeTotals(double.Parse(rvnum1), double.Parse(rvnum2), ref nc, ref ar);
rvnum1 = String.Empty;
rvnum2 = String.Empty;
snum2 = String.Empty;
}
else
{
File.AppendAllText(@"C:\WriteLines.txt", "No is : " + num1.ToString() + " , Same : rvnum1 = " + rvnum1 + " ," + "rvnum2 = " + rvnum2 + " ,nc = " + nc.ToString() + Environment.NewLine);
nc = 0;
return;
}
}
//Calling routine
int a_nc = 0;
ArrayList a_ar = new ArrayList();
for (double a_no1 = 1; a_no1 < 200; a_no1++)
{
Application.DoEvents();
lblCounter.Text = a_no1.ToString();
GeneratePalindromeTotals(a_no1, a_no1, ref a_nc, ref a_ar);
if (a_nc.Equals(26)) { MessageBox.Show(a_no1.ToString()); break; }
}
Dry run of program as it iterates...
Answer : 196
Comments