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. 【Map】Double Queue

    Double Queue Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13258   Accepted: 5974 Des ...

  2. Ubuntu下安装python相关数据处理

    01. Ubuntu下安装ipython sudo apt-get install ipython 02. Ubuntu下安装pip $ sudo apt-get install python-pip ...

  3. UIDatePicker 时间滚动表

    UIDatePicker *datapicker; //时间滚动表 datapicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(50, 200 ...

  4. Java的设计模式----strategy(策略模式)

    设计模式: 一个程序员对设计模式的理解: “不懂”为什么要把很简单的东西搞得那么复杂.后来随着软件开发经验的增加才开始明白我所看到的“复杂”恰恰就是设计模式的精髓所在,我所理解的“简单”就是一把钥匙开 ...

  5. 初始go语言

    一.创建第一个go语言程序:打印hello world! package main import "fmt" func main() { fmt.Println("Hel ...

  6. Linux 查公网出口IP

    wget http://members.3322.org/dyndns/getipcat getip

  7. Android使用Apache的httpmime包post提交数据

    /** * 启动线程向服务器post提交数据 */ public void upLoadPic(final Intent data) { ToastUtils.toastWithMessage(get ...

  8. Sql Server尝试读取或写入受保护的内存。这通常指示其他内存已损坏

    今日遇到这样一个问题,用vs2010调试C#代码时,只要代码一运行到跟数据库关联的地方时,编译器就报错误,给的提示如:调试器已附加,要继续需要分离什么的,咋一看还以为是vs中调试器设置的问题,可后来仔 ...

  9. 写Java程序的三十个基本规则【新手必读】

    (1) 类名首字母应该大写.字段.方法以及对象(句柄)的首字母应小写.对于所有标识符,其中包含的所有单词都应紧靠在一起,而且大写中间单词的首字母.例如:  ThisIsAClassName  this ...

  10. hexo常用命令笔记

    hexo npm install -g hexo npm update -g hexo hexo init 常用 hexo n == hexo new "a new post" 新 ...