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.

求10000以内的不可按以上方法迭代得出回文的数的个数。

#include <iostream>
#include <string>
using namespace std; string num2str(int n)
{
string ans = "";
while (n)
{
int a = n % 10;
char b = a + '0';
ans = b + ans;
n /= 10;
}
return ans;
} string strplus(string a, string b)
{
int len = a.length(); int flag = 0;
string ans = "";
for (int i = len - 1; i >= 0; i--)
{
int tmp = a[i] + b[i] - '0' - '0' + flag;
flag = tmp / 10;
tmp = tmp % 10;
char p = tmp + '0';
ans = p + ans;
}
if (flag == 1)
ans = '1' + ans;
return ans;
} bool pali(string a)
{
for (int i = 0; i < a.length() / 2; i++)
{
if (a[i] != a[a.length() - 1 - i])
return false;
}
return true;
} bool isLychrel(int n)
{
string a, b;
a = num2str(n);
b.assign(a.rbegin(), a.rend());
for (int i = 1; i <= 50; i++)
{
a = strplus(a, b);
if (pali(a))
return false;
b.assign(a.rbegin(), a.rend());
}
return true;
} int main()
{ int count = 0;
for (int i = 1; i <= 10000; i++)
{
if (isLychrel(i))
count++;
}
cout << count << endl;
system("pause");
return 0;
}

Project Euler:Problem 55 Lychrel numbers的更多相关文章

  1. Project Euler:Problem 88 Product-sum numbers

    A natural number, N, that can be written as the sum and product of a given set of at least two natur ...

  2. Project Euler:Problem 61 Cyclical figurate numbers

    Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...

  3. Project Euler:Problem 42 Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  4. Project Euler:Problem 87 Prime power triples

    The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is ...

  5. Project Euler:Problem 89 Roman numerals

    For a number written in Roman numerals to be considered valid there are basic rules which must be fo ...

  6. Project Euler:Problem 93 Arithmetic expressions

    By using each of the digits from the set, {1, 2, 3, 4}, exactly once, and making use of the four ari ...

  7. Project Euler:Problem 28 Number spiral diagonals

    Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is forme ...

  8. Project Euler:Problem 47 Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2 × 7 15 = 3 × 5 The ...

  9. Project Euler:Problem 63 Powerful digit counts

    The 5-digit number, 16807=75, is also a fifth power. Similarly, the 9-digit number, 134217728=89, is ...

随机推荐

  1. SGU 117.Counting

    时间限制: 0.25 sec. 空间限制: 4096 KB 题目大意: 给你n,m,k(都小于10001),和 n 个数,求这n个数中有多少个数的m次幂能够整除k.(即 n i^m % k==0). ...

  2. hibernate映射关系之多对多

    多对多: * 关系在第三张表中,和两张表本身没有关系 * 多对多谁维护关系:谁都能维护关系(效率是一样的),一般情况下可以通过页面 来体现 * 关系体现: 第三张表的维护:增加.删除 course类对 ...

  3. js 支持的原始数据类型

    原始数据类型: 数值型: 1.十进制数 <script> var a =12; a = -12 a = 12.4 a =.23e2 //=>23 a = 2e3 //=>200 ...

  4. PHP-HTML重要知识点笔记

    1.用frameset.frame和iframe还实现多窗口 2.在图片上利用映射距离usemap来实现按钮跳转.------第8尾集 3.表单必须要有name和value,因为抓包的时候,可发现必须 ...

  5. PHP面向对象(OOP):__set(),__get(),__isset(),__unset()四个方法的应用

    一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是, 对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数”__get()”和”__set()”来获取和赋值其属性 ...

  6. 认识Web和HTTP

    一:了解Web.   首先,Web应用的产生起源于1989年,当时CERN(欧洲核子研究组织)的蒂姆·伯纳斯-李(Time BernersLee)博士提出了一种能让远隔两地的研究者们共享知识的设想.借 ...

  7. Python学习 - 编写一个简单的web框架(一)

    自己动手写一个web框架,因为我是菜鸟,对于python的一些内建函数不是清楚,所以在写这篇文章之前需要一些python和WSGI的预备知识,这是一系列文章.这一篇只实现了如何处理url. 参考这篇文 ...

  8. QLineEdit

    The QLineEdit widget is a one-line text editor. Header: #include <QLineEdit> qmake: QT += widg ...

  9. 如何在js文件中实现获取request.getCotextPath();

    我们在jsp中可以方便的使用“request.getCotext()”来获取工程的根目录. 但是如果我们的js代码存在一个单独的js文件中,这时候再想获取根目录,我们就要自己截取了.可以采用下面的方式 ...

  10. 如何使用SplitContainer控件[转]

    原文地址:http://yinzhihua2008.blog.163.com/blog/static/794306720120511150457/ 在Windows资源管理器中,当把鼠标指针移动到Tr ...