Project Euler:Problem 55 Lychrel numbers
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的更多相关文章
- 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 ...
- Project Euler:Problem 61 Cyclical figurate numbers
Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers are all figurate (polygon ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- for语句嵌套使用 实现9*9乘法表
这个实例主要考察对for循环语句的使用,出现递增规律的乘法表. 开发环境 开发工具:Microsoft Visual Studio2010 旗舰版 具体步骤 先是制作一个 ...
- [转] 使用CSS3 will-change提高页面滚动、动画等渲染性能 ---张鑫旭
一.先来看一个例子 下面这个例子来自某外文,我这里简单转述下. 视差滚动现在不是挺流行的嘛,然后Chris Ruppel当其使用background-attachment: fixed实现背景图片不随 ...
- Flask_SqlAlchemy 1215, 'Cannot add f oreign key constraint'
Flask_SqlAlchemy 1215, 'Cannot add f oreign key constraint'报错 sqlalchemy.exc.IntegrityError: (pymysq ...
- 练习2 B题 - 求绝对值
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Description 求实数 ...
- Standard Numeric Format Strings
The following table describes the standard numeric format specifiers and displays sample output prod ...
- JavaScript 资源装备
书籍 随着JS的普及,大家能获取到的JS书籍实在太多了,但是在我看来只有很少一部分JS书籍可以提供够新够有意思的内容.以下是我看过之后,觉得很值得推荐给大家的: JavaScript高级程序设计 作者 ...
- Deferred
http://blog.allenm.me/2012/01/jquery_deferred_promise_method/ http://www.ruanyifeng.com/blog/2011/08 ...
- sql restore mode
refer : https://msdn.microsoft.com/en-us/library/ms189272.aspx SELECT name, recovery_model_desc FROM ...
- Multiple
poj1465:http://poj.org/problem?id=1465 题意:给你一个数n(0~4999):以及m个不同十进制的数,问有这些十进制数组成的最小的n的倍数是多少.如果有则输出,没有 ...
- TWinControl的DoubleBuffered属性的作用与举例
留个爪,网上搜一篇,仔细分析一下.