Description

While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,noticed that the telephone number of his brother-in-law H. Smith had the following peculiar property: The sum of the digits of that number was equal to the sum of the digits of the prime factors of that number. Got it? Smith's telephone number was 493-7775. This number can be written as the product of its prime factors in the following way: 
4937775= 3*5*5*65837
The sum of all digits of the telephone number is 4+9+3+7+7+7+5= 42,and the sum of the digits of its prime factors is equally 3+5+5+6+5+8+3+7=42. Wilansky was so amazed by his discovery that he named this kind of numbers after his brother-in-law: Smith numbers. 
As this observation is also true for every prime number, Wilansky decided later that a (simple and unsophisticated) prime number is not worth being a Smith number, so he excluded them from the definition. 
Wilansky published an article about Smith numbers in the Two Year College Mathematics Journal and was able to present a whole collection of different Smith numbers: For example, 9985 is a Smith number and so is 6036. However,Wilansky was not able to find a Smith number that was larger than the telephone number of his brother-in-law. It is your task to find Smith numbers that are larger than 4937775!

Input

The input file consists of a sequence of positive integers, one integer per line. Each integer will have at most 8 digits. The input is terminated by a line containing the number 0.

Output

For every number n > 0 in the input, you are to compute the smallest Smith number which is larger than n,and print it on a line by itself. You can assume that such a number exists.

Sample Input

4937774
0

Sample Output

4937775
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std; int distsum(int n)
{
int ans=;
while(n)
{
ans+=n%;
n=n/;
}
return ans;
} bool isprime(int n)
{
if(n==) return false;
if(n==) return true;
for(int i=;i<=(int)sqrt(n+0.5)+;i++)
{
if(n%i==)
return false;
}
return true;
} int prime_factor(int n)
{
int i=;
queue <int> q;
while(n!=||n!=)
{
if(n%i==&&isprime(i))
{
q.push(i);
n/=i;
if(isprime(n))
{
q.push(n);break;
}
}
else i++;
} while(!q.empty())
{
int k=q.front();
q.pop();
cout<<k<<endl;
}
return ;
} int main()
{
int n;
while(cin>>n)
{
if(n==) break;
for(int i=n+;;i++)
{
if(isprime(i)) continue;
if(prime_factor_sum(i)==distsum(i))
{
cout<<i<<endl;break;
}
}
}
return ;
}

poj 1142 Smith Numbers的更多相关文章

  1. POJ 1142 Smith Numbers(史密斯数)

    Description 题目描述 While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Leh ...

  2. POJ 1142 Smith Numbers(分治法+质因数分解)

    http://poj.org/problem?id=1142 题意: 给出一个数n,求大于n的最小数,它满足各位数相加等于该数分解质因数的各位相加. 思路:直接暴力. #include <ios ...

  3. Smith Numbers POJ - 1142 (暴力+分治)

    题意:给定一个N,求一个大于N的最小的Smith Numbers,Smith Numbers是一个合数,且分解质因数之后上质因子每一位上的数字之和 等于 其本身每一位数字之和(别的博客偷的题意) 思路 ...

  4. POJ 1142:Smith Numbers(分解质因数)

                                   Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submiss ...

  5. A - Smith Numbers POJ

    While skimming his phone directory in 1982, Albert Wilansky, a mathematician of Lehigh University,no ...

  6. Smith Numbers - PC110706

    欢迎访问我的新博客:http://www.milkcu.com/blog/ 原文地址:http://www.milkcu.com/blog/archives/uva10042.html 原创:Smit ...

  7. Poj 2247 Humble Numbers(求只能被2,3,5, 7 整除的数)

    一.题目大意 本题要求写出前5482个仅能被2,3,5, 7 整除的数. 二.题解 这道题从本质上和Poj 1338 Ugly Numbers(数学推导)是一样的原理,只需要在原来的基础上加上7的运算 ...

  8. poj1142 Smith Numbers

    Poj1142 Smith Numbers Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 13854 ...

  9. UVA 10042 Smith Numbers(数论)

    Smith Numbers Background While skimming his phone directory in 1982, Albert Wilansky, a mathematicia ...

随机推荐

  1. 破译情报-NOIP2016提高组复赛模拟试题

    [题目描述] 最近国安人员截获了一份 RB 国的秘密情报, 全文都是经过加密的,每个单 词都很长.破译人员想到先把单词化简一下,方法是把每个单词尽量取短些的前 缀,但所取的前缀不能是其他单词的前缀. ...

  2. NEUQ1038: 谭浩强C语言(第三版)习题4.8

    之前没做对的一道题,今天集中清理一下. //------------------- 很水的题,主要是 %.2lf 不能四舍五入,需要仅保留两位小数,用了丑陋的强制类型转换... //--------- ...

  3. 仿qq的条目抽屉动画效果_ViewDragHelper

    GitHub地址: https://github.com/OOOOOldZhu/DrawerItemView import android.content.Context; import androi ...

  4. [ An Ac a Day ^_^ ] [kuangbin带你飞]专题十二 HDU 1176 免费馅饼

    题意: 中文题意不解释…… 思路: 先把x,T存到矩阵里 然后像数塔一样从最底层走一边就行了 dp[i][j]代表在时间为j时 第i个位置最多能吃到多少个馅饼 最后输出第0时刻的5位置的馅饼数量就好了 ...

  5. LeetCode #3. Longest Substring Without Repeating Characters C#

    Given a string, find the length of the longest substring without repeating characters. Examples: Giv ...

  6. Javascript 数字保留2位小数

    整理使用Javascript函数将数值保留两位小数: 1.num.toFixed(2) //进位 2.(Math.round(num * 100) / 100).toFixed(2) //进位 3.( ...

  7. sql 函数 DATEADD 使用

    DATEADD ( datepart , number, date ) 在日期中添加或减去指定的时间间隔.   datepart 是规定应向日期的哪一部分返回新值的参数. number 为要增加或减去 ...

  8. 关于angularjs依赖注入的整理

    初学angularjs阶段,刚刚看到菜鸟教程的angularjs依赖注入.现在整理一下: 1.含义:一个或更多的依赖(可以理解为模块关系依赖)或服务(分为内建服务[例如$http,$tiomeout等 ...

  9. 在cmd中设置字体

    1.首先在cmd中输入chcp 65001 回车(通过 chcp命令改变代码页,UTF-8的代码页为65001) 2.右击命令提示符的标题栏点击属性. 3.在属性中选择字体后点击确认即可.

  10. ubuntu 把终端信息输出到文本文件中的方法

    方法一:把终端中所有信息都写到文本文件中 在终端的命令行中输入以下命令: $   script   -f    output.txt 这样就会在当前目录下创建一个output.txt文件 接下来,在按 ...