Smith Number

Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 825  Solved: 366

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<stdio.h>
int n ; int main ()
{
//freopen ("a.txt" , "r" , stdin ) ;
while (~ scanf ("%d" , &n) ) {
if (n == )
break ;
while () {
n ++ ;
int x ;
int sum = ;
int m = n ;
while (m) {
sum += (m % ) ;
m /= ;
}
int sum2 = ;
m = n ;
for (int i = ; i * i <= n ; i++) {
if (m % i == ) {
int y = i ;
int k = ;
while (y) {
k += y % ;
y /= ;
}
while (m % i == ) {
sum2 += k ;
m /= i ;
}
}
}
if (m == n)
continue ;
if (m != ) {
int y = m ;
int k = ;
while (y) {
k += y % ;
y /= ;
}
sum2 += k ;
} if (sum2 == sum) {
break ;
}
}
printf ("%d\n" , n ) ;
}
}

在O(sqrt(N))时间内求得n的所有质因数。
注意1.若跳出循环后n除尽,则cnt 不加1 ; 反之 加一。

2.跳出若为本身,则cnt = 0 ;

poj1142.Smith Number(数学推导)的更多相关文章

  1. ZOJ 3702 Gibonacci number(数学推导)

    公式推导题,G(0) = 1,G(1) = t,给出一个 i 和 G(i),要求求出G(j)的值: G(0) = 0*t + 1 G(1) = 1*t + 0; 观察t的系数和常数值可以知道二者都遵循 ...

  2. 关于不同进制数之间转换的数学推导【Written By KillerLegend】

    关于不同进制数之间转换的数学推导 涉及范围:正整数范围内二进制(Binary),八进制(Octonary),十进制(Decimal),十六进制(hexadecimal)之间的转换 数的进制有多种,比如 ...

  3. UVA - 10014 - Simple calculations (经典的数学推导题!!)

    UVA - 10014 Simple calculations Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & ...

  4. poj1142 Smith Numbers

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

  5. HDU 5073 Galaxy(Anshan 2014)(数学推导,贪婪)

    Galaxy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total S ...

  6. HDU-1719 Friend 数学推导

    Friend HDU - 1719 Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend n ...

  7. 借One-Class-SVM回顾SMO在SVM中的数学推导--记录毕业论文5

    上篇记录了一些决策树算法,这篇是借OC-SVM填回SMO在SVM中的数学推导这个坑. 参考文献: http://research.microsoft.com/pubs/69644/tr-98-14.p ...

  8. 『sumdiv 数学推导 分治』

    sumdiv(POJ 1845) Description 给定两个自然数A和B,S为A^B的所有正整数约数和,编程输出S mod 9901的结果. Input Format 只有一行,两个用空格隔开的 ...

  9. LDA-线性判别分析(二)Two-classes 情形的数学推导

    本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...

随机推荐

  1. 深入探究javascript的 {} 语句块

    今日学习解析json字符串,用到了一个eval()方法,解析字符串的时候为什么需要加上括号呢?摸不着头脑.原来javascript中{}语句块具有二义性,不加括号会出错,理解这种二义性对我们理解jav ...

  2. mysql慢查询分析工作pt-query-digest的使用

    一.简单安装 wget percona.com/get/pt-query-digest chmoe u+x pt-query-digest 二.简单使用 ./pt-query-digest /var/ ...

  3. 深入理解jQuery中$.get、$.post、$.getJSON和$.ajax的用法

    当我们用javascript写ajax程序写得很“开心”的时候,突然有人告诉你有一种东西叫jquery,它会告诉你不直接和HttpRequest是多么的快乐,同时你再也不需要再烦恼纠结的ajax乱码问 ...

  4. asp.net 预编译和动态编译

    在asp.net中,编译可以分为:动态编译Dynamical Compilation和预编译(Precompilation). 动态编译 深入剖析ASP.NET的编译原理之一:动态编译(Dynamic ...

  5. 第四次个人作业——关于微软必应词典android客户端的案例分析

    [前言] 第一次搞测评这种东西,如果有什么疏漏,请多多谅解.测评内容如题. 第一部分 调研,评测 评测:(设备:Lenovo A806) 软件的bug,功能评测,黑箱测试 bug等级划分方式 5级分类 ...

  6. iOS开发中的错误整理,关于用绑定Tag取控件的注意事项,有时候不绑定也是个错!

    如图:红色框中是个自定义的导航工具条titlesView(没有绑定Tag),工具条中有五个按钮(按钮绑定了Tag)以及一个红色的指示器indicatorView(没有绑定Tag),下面的蓝色是可以滚动 ...

  7. WPF--调用线程必须为 STA,因为许多 UI 组件都需要(转载)

    自VS2005开始,UI元素在不同线程中访问就受到限制了,当然你也可以解除这种限制 以下提供Framework3.0的解决方案发: public partial class Window1 : Win ...

  8. UVA 10391 stl

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. Hibernate-入门教程

    首先了解hibernate的目录结构 . +lib antlr.jar cglib-full.jar asm.jar asm-attrs.jars commons-collections.jar co ...

  10. 【CodeForces 605A】BUPT 2015 newbie practice #2 div2-E - Sorting Railway Cars

    http://acm.hust.edu.cn/vjudge/contest/view.action?cid=102419#problem/E Description An infinitely lon ...