分解质因数——Poj 1142 Smith Numbers

Smith Numbers(算术基本定理) Smith Numbers 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 pecul... 阅读详情

Smith Numbers
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 10627 Accepted: 3723

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


思路:
枚举大于给定n的所有合数(题目中说: a prime number is not worth being a Smith number)是否为Smith Number。
根据质因数分解定理,分解质因数时,可以这样做:
设要分解的合数为N,M为试除的质因数,初值为2;
1、如果N被M整除,则M为N的质因数,令N = N / M,M不变;
2、若不能整除,则N不变,M++;
重复上面两步,直到N是一个素数为止。
需要注意的是:
不需要判断当前M是否为素数,因为若为合数则必然不能整除。


#include <iostream>
#include <cstdio>

using namespace std;

bool IsPrime(int n)
{
	int i;
	for (i = 2; i*i <= n; i++)
	{
		if (n % i == 0)
		{
			return false;
		}
	}
	return true;
}

int SumOfDigits(int n)
{
	int sum = 0;
	while (n != 0)
	{
		sum += n%10;
		n /= 10;
	}
	return sum;
}

int SumOfPrimeFactors(int n)
{
	int sum = 0;
	int i = 2;
	while (true) //合数是一定可以被分解质因数的,所以一直循环好了
	{
		if (n % i == 0)
		{
			sum += SumOfDigits(i);
			if (IsPrime(n /= i))
			{
				break;
			}
		}
		else
		{
			i++;
		}
	}
	return sum += SumOfDigits(n);
}

int main(void)
{
	int n;
	while (scanf("%d", &n) != EOF && n != 0)
	{
		bool finished = false;
		int i;
		for (i = n+1; !finished; i++)
		{
			if (!IsPrime(i)) //素数不可能是Smith Number
			{
				if (SumOfDigits(i) == SumOfPrimeFactors(i))
				{
					printf("%d\n", i);
					finished = true;
				}
			}
		}
	}
	return 0;
}


C语言雅瑟夫问题 此是C语言练习时编写的小程序,用于课后作业的,供参考 立即下载

相关推荐

基于verilog的16QAM调制

思路1:正交调幅法——实现16QAM调制,可以理解为时相位相差90度的两个正弦波的4PAM叠加。 4比特输入经过2->4进制转化得到两路2比特输入,然后分别经过4PAM后叠加所得输出即为16QAM调制结果。 具体程序稍后编写。 思路2:复合相移法——可以借助前面编写的QPSK,相当于两路幅度不同的QPSK的叠加,比如幅度分别取1和3 。 思路1正交调幅法的设计方案: 两正弦波相位差...

weixin_30725467的博客 2929

POJ 1142 Smith Numbers

 Smith NumbersTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7141 Accepted: 2505DescriptionWhile skimming his phone directory in 1982, Albert Wilansky,

bobten2008的专栏 1332

WebVTT中的文本轨道(Text Tracks,TT)到底是什么鬼?

WebVTT,全称是The Web Video Text Tracks Format,意思是网络视频文本轨道格式。那么到底什么是文本轨道(Text Tracks)呢?这篇文章希望一探究竟。

码农1479的博客 1985

Smith Numbers (POJ-1142)(素数判定+整数分解)

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: ...

菜鸡的博客 353

基于PCIE8311+FPGA+TMS320F28335 PCIE应用项目开发板PROTEL99设计硬件(原理图+PCB)文件

基于PCIE8311+FPGA+TMS320F28335 PCIE X1应用项目开发板PROTEL99设计硬件(原理图+PCB)文件,Protel 99se 设计的项目工程文件,包括原理图及PCB印制板图,可用Protel或 Altium Designer(AD)软件打开或修改,都已经制板在实际项目中使用,可作为你产品设计的参考。

GJZGRB的博客 576

项目过程记录:ADS-B信号接收并显示(Ubuntu20.04+ANTSDR E310V2)

最近被安排做一个ADS-B信号接收的项目,拿到软件无线电模块(ANTSDR E310V2)后安装环境和软件真的太折磨了。。在此记录以下我走过的弯路,大家可以参考一下。

weixin_43627638的博客 5017

3分钟搞懂行列式因子:从Smith标准型到矩阵分解的实用指南

本文深入浅出地解析了行列式因子的核心概念与计算方法,并阐明其与Smith标准型之间的关键联系。通过具体实例和计算步骤,展示了如何从矩阵子式的最大公因数出发,逐步推导出揭示矩阵深层整除结构的不变因子。文章进一步探讨了该理论在网络分析、编码理论及控制系统等工程与数据科学领域的实际应用,为处理整数或多项式矩阵提供了强大的标准化工具。

brandy的博客 442

K8s私有仓库拉取镜像报错解决:x509 certificate signed by unknown authority

在Kubernetes环境中使用自签名证书的私有Harbor镜像仓库时,常会遇到证书验证失败的问题。本文将详细讲解如何解决这个常见的证书问题。Kubernetes版本:1.28.2容器运行时:containerd 1.6.20私有仓库:Harbor(使用自签名证书)(超简单搭建私有仓库Harbor-亲测无坑解决K8s私有仓库证书问题的关键在于正确配置容器运行时(本例中是containerd)信任私有仓库的证书。

lzz的博客 2140

Problem 1165 - Smith Number

http://acm.whu.edu.cn/land/problem/detail?problem_id=1165 #include #include using namespace std ; #define INT long long INT Sum( INT x ) { INT sum = 0 ; while( x ) { sum += x % 10 ; x /=

bo-jwolf的专栏 2419

QT播放器插件内嵌浏览器配合流媒体实现国标摄像头网页端播放

基本原理在浏览器⽹页中的指定播放窗口的位置和⼤⼩,实现⼀个内嵌到⽹页中显⽰的播放窗⼝,前端还必须可对这个内嵌播放窗⼝进⾏控制,⽽且播放窗⼝必须跟随浏览器窗⼝的移动和缩放、⽹页滚动、标签页切换、关闭等操作进⾏⾃动联动。播放器可以通过QT实现。这个播放窗⼝同时提供Web Socket的服务端和JSON打包命令的解析执⾏模块,前端就可以通过Web Socket连接后发送JSON打包的控制命令实现控制播放窗⼝。

cameravision的博客 562

使用Transformer实现自动调制识别(RML2016a)

image_size = 128 # resize重置输入大小patch_size = 2 # 从输入图片(信号看作图片)中将被提取的块大小# 分成 num_patches 个块num_patches = (image_size // patch_size) #(72/6)**2 横着裁剪),],return x)(x1, x1)# MLP.# Add MLP.x=x_train,y=y_train,filepath,round。

QAQIknow的博客 6276

POJ 1142 Smith Numbers【Euler分解质因数】

Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13574   Accepted: 4626 Description While skimming his phone directory in 1982, Albert Wilansky,

DTL66的博客 582

如何在 Windows 中实时打开字幕

在 Windows 操作系统中,你可以使用编程来实现实时字幕功能,以便在屏幕上显示即时的文字内容。下面是一个示例代码,展示了如何使用Python编程语言在Windows上实现实时字幕功能。这样,字幕窗口将会不断地显示系统当前时间作为字幕。你可以根据自己的需求修改主循环中的内容,以显示你想要的实时字幕。希望以上代码和说明能够帮助你在Windows中实现实时字幕功能。函数创建了一个顶级窗口,并设置窗口的样式、位置和大小。方法,用于在字幕窗口中显示指定的字幕内容。上述代码中,我们使用了Python的。

DevWizard的博客 3270

Smith Numbers(Poj1142)(质因数分解+素数判定)

Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13142   Accepted: 4484 Description While skimming his phone directory in 1982, Albert Wilansky,

My Castle 808

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

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

                                 Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14521   Accepted: 4906 Description While skimming his phone directory in ...

执念 375

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

Smith Numbers POJ - 1142(质因数分解)

Smith Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14060 Accepted: 4799 Description While skimming his phone directory in 1982, Albert Wilansky, a mathematician o

howell 317

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

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

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

weixin_30657999的博客 242

POJ1142 Smith Numbers(数论,分治+暴力,质因数分解)

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 followi

riba2534的博客 916

POJ1142 HDU1333 ZOJ1133 Smith Numbers【质因数分解+素数判定+数位之和】

问题链接:POJ1142 HDU1333 ZOJ1133 Smith Numbers。 题意简述:寻找大于输入数并且最接近的Smith Numbers。 问题分析: Smith Numbers数不是素数,其各位数字之和等于其各个因子的各位数字之和。。 程序说明: 函数digitsum()用于计算数的各位数字之和。 函数fact_digitsum用于计算数的各个因子的数字之和,但

海岛Blog 1971

poj 1142 Smith Numbers

分类:其他 难度:1.5   题意:将一个数各个位的和,与因式分解的每个因数各个位的和,如相等,为smith数,4937775= 3*5*5*65837,4+9+3+7+7+7+5= 42, 3+5+5+6+5+8+3+7=42。求大于给出的n的最小smith数。因式分解,map记录因式分解结果,map[因数]=出现次数。 #include #include using namespac

Maverick 934

【POJ】1142 - Smith Numbers(容斥原理)

点击打开题目 Smith Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13488   Accepted: 4593 Description While skimming his phone directory in 1982, Al

wyg1997 591

POJ_1142_Smith Numbers

分解质因数

edward_108 292
上一篇: 回溯法——Ural 1073 Square Country
下一篇: 第3章 动态规划 矩阵连乘问题
庞老板
博客等级 码龄15年 127粉丝 66原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

庞老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值