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 ...
随机推荐
- SGU 112.a^b - b^a
题意: 如标题. 方法: 简单高精度... 代码(继续JAVA 水过) import java.util.*; import java.math.*; public class Solution { ...
- SGU 123.The sum
#include <iostream> using namespace std; int f[50]={0,1,1}; int main(){ int n,s=0; cin>> ...
- delegate 中的BeginInvoke和EndInvoke方法
开发语言:C#3.0 IDE:Visual Studio 2008 一.C#线程概述 在操作系统中一个进程至少要包含一个线程,然后,在某些时候需要在同一个进程中同时执行多项任务,或是为了提供程序的性能 ...
- 利用set实现去重
最近读了一些有关于ES6的文章,觉得真是一个超级大的进步,就是不知道兼容性怎么样,鉴于我还在初学,先写个小例子练手,顺便时刻提醒自己要坚持学下去.未来的趋势肯定是替代es5没跑了. var arr=[ ...
- PHPMailer发匿名邮件及Extension missing: openssl的解决
原文链接:http://www.tongfu.info/phpmailer%E5%8F%91%E5%8C%BF%E5%90%8D%E9%82 %AE%E4%BB%B6%E5%8F%8Aextensio ...
- PHP常用代码:
1.$array=explode(separator,$string); //字符串->数组 2.$string=implode(glue,$array);//数组->字符串 3.file ...
- 手工构建ISO的基本步骤
1.完成rpm包的构建 登录测试机,ssh 10.xx.xx.xxx cd /home/svn/desktop/trunk/ svn update ...
- find_cmd函数分析
一.概述 1.函数位置 common/command.c 2.函数功能分析 解析命令的关键环节是如何根据输入命令查找对应命令的信息,从而跳转到对应命令的函数处执行程序.这必然涉及到如何存放命令的详细信 ...
- OSSchedLock()函数透析
uC/OS-II的OSSchedLock()和OSSchedUnlock()函数允许应用程序锁定当前任务不被其它任务抢占. 使用时应当注意的是:当你调用了OSSchedLock()之后,而在调用OSS ...
- 关于Unity导出的Android应用在小米、联想等机型上崩溃的问题
应用在三星手机上运行没有出现问题,但在小米和联想手机上会崩溃.这个问题在刚开始时一直查不到问题所在,后来发现是因为Android清单文件中声明的权限出现了重复,去掉了重复的权限之后,就没有出现崩溃的情 ...