欧拉计划Problem 55

If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.

Not all numbers produce palindromes so quickly. For example,

349 + 943 = 1292,
1292 + 2921 = 4213
4213 + 3124 = 7337

That is, 349 took three iterations to arrive at a palindrome.

Although no one has proved it yet, it is thought that some numbers, like 196, never produce a palindrome. A number that never forms a palindrome through the reverse and add process is called a Lychrel number. Due to the theoretical nature of these numbers, and for the purpose of this problem, we shall assume that a number is Lychrel until proven otherwise. In addition you are given that for every number below ten-thousand, it will either (i) become a palindrome in less than fifty iterations, or, (ii) no one, with all the computing power that exists, has managed so far to map it to a palindrome. In fact, 10677 is the first number to be shown to require over fifty iterations before producing a palindrome: 4668731596684224866951378664 (53 iterations, 28-digits).

Surprisingly, there are palindromic numbers that are themselves Lychrel numbers; the first example is 4994.

How many Lychrel numbers are there below ten-thousand?

NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers.



#define N 10000
#define M 50


bool Palindromes(const char n[])
{
for (int i = 0,d = strlen(n); i <=d/2;i++)
{
if (n[i] != n[d-i-1])
{
return false;
}
}
return true;
}


void add(char n1[], char n2[])
{
char temp[M] = {0};
for (int i = strlen(n1)-1,j = 0,d = 0; i >= 0; j++,i--)
{
temp[j] =(n1[i]-'0'+n2[i]-'0'+d)%10+'0';
d = (n1[i]-'0'+n2[i]-'0'+d)/10;
}
if (d != 0)
{
temp[j] = d+'0';
j++;
}
temp[j] = '\0';
strrev(temp);
strcpy(n1,temp);
}


int main()
{
clock_t ts,te;
ts=clock();
int answer = N-1;
char num1[M],num2[M];
for (int i = 1; i < N; i++)
{
memset(num1,0,sizeof(num1));
memset(num2,0,sizeof(num2));
sprintf(num1,"%d",i);
strcpy(num2,num1);
strrev(num2);
for (int j = 0; j < M; j++)
{
add(num1,num2);
if (Palindromes(num1) == true)
{
answer--;
break;
}
strcpy(num2,num1);
strrev(num2);
}
}
printf("\nanswer %d",answer);
te=clock();
printf("\ntime difference: %ds\n",(te-ts)/CLOCKS_PER_SEC);
getchar();
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值