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 ...
随机推荐
- Fedora 21 安装VirtualBox
注: 所有操作需要root权限 如果不是root用户在下面所有命令前加sudo 装dkms,kernel-devel,makecache: yum install dkms yum install ...
- js转换/Date(........)/
eval('new ' + (datetime.replace(/\//g, ''))); 好记性不如烂笔头,记下以备后用.
- PHP框架_Smarty_实现登录功能
1.项目框架 |--mvc |--data 数据 |--cache 缓存 |--template_c 模板生成目录 |--framework |--function |--function.php 功 ...
- nodejs版本控制
本方法基于https://segmentfault.com/a/1190000004855835修改 配置: 使用的nvmw的git 地址https://github.com/hakobera/nvm ...
- yii2源码学习笔记(七)
今天继续了解model类 /** 2 * Returns the form name that this model class should use. 3 * 4 * 返回表单的名称,就是这个 mo ...
- php基础知识【函数】(7)url和ob函数
一.URl函数 1.urlencode -- 编码 URL 字符串 2.urldecode -- 解码已编码的 URL 字符串 3.rawurlencode -- 按照 RFC 1738 对 URL ...
- MySQL中删除重复数据只保留一条
用SQL语句,删除掉重复项只保留一条 在几千条记录里,存在着些相同的记录,如何能用SQL语句,删除掉重复的呢 1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 SELECT ...
- linux hadoop 集群安装步骤
http://blog.csdn.net/xjavasunjava/article/details/12013677 1,时间同步hadoop集群的每台机器的时间不能相差太大. 安装集群前最好进行一下 ...
- Js Framework
http://www.mhtml5.com/2012/06/5119.html http://www.mhtml5.com/2012/06/5118.html http://cubiq.org/isc ...
- cf B Three matrices
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; ][] ...