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. JavaSE中主要的类与方法

    1.instanceof:测试左边的对象是否是右边类的实例;  数组.length:返回的是数组的长度; 2.Scanner类     nextInt():读取键盘录入的整数;  next():读取键 ...

  2. 微信小程序使用场景及取名“潜”规则

    微信小程序使用场景举例: 1.查看公交 2.登记.选座 3.订票 4.K歌.叫代驾 5.快递查询 6.查看天气 7.医院挂号.拿药.缴费 8.加油充电 9.政务服务 微信公众号“数据三观”认为,小程序 ...

  3. HTTP 错误 403.14 - Forbidden的解决办法

    错误: HTTP 错误 403.14 - ForbiddenWeb 服务器被配置为不列出此目录的内容.   原因: 出现这个错误,是因为默认文档中没有增加index.aspx导致的. 解决方法: 打开 ...

  4. 推荐几个在线PDF转化成Word网站

    不想安装专业的pdf转换成word软件,希望大家喜欢!昨天用的https://www.pdftoword.com/# 成功搞定! 1.Free-PDFtoWord 在线转换工具: 地址:http:// ...

  5. jetbrain phpstorm 增加或删除一个 live template

    打开\.PhpStorm2016.2\config\templates 将xml文件放入该文件夹中 重启! 单独: setting -> editor -> Live Template +

  6. echarts中,y轴文本倾斜

    yAxis : [ { type : 'category', data : ['国家公务员','专业技术人员','职员','企业管理人员'], axisLabel:{ interval: 0 , ro ...

  7. read/sysread区别

    use warnings; use strict; my $readbuff; my $sysreadbuff; ); print "read get:$readbuff\n"; ...

  8. APICloud界面间跳转

    UZModule提供属性controller,可通过该控制器对目标控制器进行push或者present操作. // push[self.viewController.navigationControl ...

  9. Mac下配置Maven环境变量

    Mac下配置Maven环境变量 1.先到Apache官网下载maven压缩包.下载地址:http://maven.apache.org/download.cgi 2.Maven是用Java开发的,所以 ...

  10. sql proc触发异常处理回滚

    sql proc触发异常处理回滚 针对proc嵌套proc很有用 begin begin try begin tran --判断错误 BEGIN --ROLLBACK TRAN SET @vcResu ...