Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km.

Input Specification:

Each input file contains one test case which gives a positive integer N in the range of long int.

Output Specification:

Factor N in the format N = p1^k1 * p2^k2 *…*pm^km, where pi's are prime factors of N in increasing order, and the exponent ki is the number of pi -- hence when there is only one pi, ki is 1 and must NOT be printed out.

Sample Input:

97532468

Sample Output:

97532468=2^2*11*17*101*1291
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long ll;
typedef struct pt{
int base, p;
pt(){
base = ;
p = ;
}
}info;
ll isPrime(ll N){
ll sqr = (ll)sqrt(N * 1.0);
if(N == )
return ;
for(int i = ; i <= sqr; i++){
if(N % i == )
return ;
}
return ;
}
ll primeTB[];
info num[];
ll findPrime(ll tb[], ll maxNum){
int index = ;
for(ll i = ; i <= maxNum; i++){
if(isPrime(i)){
tb[index++] = i;
}
}
return index;
}
int main(){
ll N, sqr, N2;
scanf("%lld", &N);
N2 = N;
sqr = (int)sqrt(1.0 * N) + ;
ll len = findPrime(primeTB, sqr);
int pi = , index = , tag = ;
for(ll i = ; N != && i < len; i++){
tag = ;
while(N % primeTB[i] == ){
N = N / primeTB[i];
num[index].base = primeTB[i];
num[index].p++;
tag = ;
}
if(tag == )
index++;
}
if(N != ){
num[index].base = N;
num[index++].p = ;
}
if(index == ){
num[].base = N;
num[].p = ;
}
printf("%lld=", N2);
printf("%d", num[].base);
if(num[].p > ){
printf("^%d", num[].p);
}
for(int i = ; i < index; i++){
printf("*%d", num[i].base);
if(num[i].p > ){
printf("^%d", num[i].p);
}
}
cin >> N;
return ;
}

总结:

1、判断素数的时候,循环条件为 i <= sqr。

2、生成的质数表可以范围到10^5, 也可以生成到 根号N的范围。

3、15 = 3 * 5,找因子只用找到根号N的范围,如果循环结束N仍然不等于1时,说明它就是大于根号N的一个因子,或者是N本身。

最新文章

  1. SEO:避免关键词内部竞争带来的无法收录问题,
  2. 教你轻松计算AOE网关键路径(转)
  3. Windows下pry安装和配置
  4. STORM_0001_用vmware拷贝出三个相同的ubuntu搭建小的zookeeper集群
  5. hdu 4288 Coder
  6. SQLServer怎样导入excel
  7. CFGYM 2013-2014 CT S01E03 D题 费用流模版题
  8. UCI
  9. json-lib之复杂数据类型的转换
  10. 4月18开始看《C++Primer Plus》
  11. [Swift]LeetCode671. 二叉树中第二小的节点 | Second Minimum Node In a Binary Tree
  12. 深度学习网络中numpy多维数组的说明
  13. Oracle 表操作(转)
  14. maven执行单元测试失败后,继续生成Jacoco&amp;Sonar报告
  15. VxWorks笔记
  16. 阿里云centos 开启ipv6
  17. JS实现集合和ECMA6集合
  18. [HNOI2002]营业额统计(splay基础)
  19. android 搜索自动匹配关键字并且标红
  20. Linux性能监控(程序篇)

热门文章

  1. 分布式监控系统Zabbix--完整安装记录(7)-使用percona监控MySQL
  2. 《移山之道》Reading Task
  3. Linux内核分析— —构造一个简单的Linux系统MenuOS(20135213林涵锦)
  4. 第三个spring冲刺第5、6、7天(三天汇总)
  5. shell之重定向
  6. Fortify Scan - Static Code Analyzer
  7. 关于Tomcat性能监控的第三方工具Probe的简介
  8. PHP hexdec() 函数
  9. 微信内置浏览器在使用video标签时(安卓)默认全屏的原因及解决办法
  10. 日志那点事儿——slf4j源码剖析