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. Python网络编程学习_Day9

    一.socketserver实现多并发 socket只能实现单进程通讯,要实现多进程同时和服务端通讯就要使用socketserver. 代码如下: import socket client = soc ...

  2. SQL语句获取数据库中的表主键,自增列,所有列

    SQL语句获取数据库中的表主键,自增列,所有列   获取表主键 1:SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_U ...

  3. HDU 5877 Weak Pair

    $dfs$序,线段树. 可以统计每一个节点作为$root$的子树上对答案的贡献,可以将树转换成序列.问题就变成了一段区间上求小于等于某个值的数有几个.用线段树记录排好序之后的区间序列,询问的时候,属于 ...

  4. 整理一些css在使用中的小技巧(进行中)

    1. 消除li排列display:inline-block的间隙 ul{ font-size:; } ul li{ display:inline-block; }

  5. CSS样式类型

    CSS样式可以写在哪些地方呢?从CSS 样式代码插入的形式来看基本可以分为以下3种:内联式.嵌入式和外部式三种.这一小节先来讲解内联式 (一)内联式样式 内联式css样式表就是把css代码直接写在现有 ...

  6. 转:iOS程序main函数之前发生了什么

    原文地址:http://blog.sunnyxx.com/2014/08/30/objc-pre-main/ 我是前言 一个iOS app的main()函数位于main.m中,这是我们熟知的程序入口. ...

  7. shell-改变分隔符

    转化为换行符: oldIFS=${IFS}; IFS=$'\n'; 命令; IFS=${oldIFS};

  8. 使用recordmydesktop进行屏幕录像

    屏幕录像的功能对于分享游戏攻略.演示电脑软件的操作是必不可少的.在Windows下可能一般的用户就下载盗版的商业软件来做了.而在GNU/Linux操作系统下,则有现成的自由软件可供使用,只不过没有图形 ...

  9. Chapter 19_0 位操作库

    位操作库是Lua5.2版本里添加的库,所有函数放在bit32 table里.(bit32只能针对32位整数运算) 在Lua5.3版本里,bit32库被废弃掉.不过可以使用一个外部兼容库,但是最好直接用 ...

  10. juit测试中报错:org.hibernate.HibernateException: Unable to get the default Bean Validation factory

    org.hibernate.HibernateException: Unable to get the default Bean Validation factory 解决方法: 解决方案: 在hib ...