Factoring Large Numbers

Uva 10392 – Factoring Large Numbers

Problem F: Factoring Large Numbers

One of the central ideas behind much cryptography is that factoring large numbers is computationally intensive. In this context one might use a 100 digit number that was a product of two 50 digit prime numbers. Even with the fastest projected computers this factorization will take hundreds of years.

You don’t have those computers available, but if you are clever you can still factor fairly large numbers.

Input

The input will be a sequence of integer values, one per line, terminated by a negative number. The numbers will fit in gcc’slong long intdatatype. You may assume that there will be at most one factor more than 1000000.

Output

Each positive number from the input must be factored and all factors (other than 1) printed out. The factors must be printed in ascending order with 4 leading spaces preceding a left justified number, and followed by a single blank line.

Sample Input9012345678911899132545313912745267386521023-1Sample Output 2335123456789133131792711381242330971411522630413

#include<stdio.h>#include<string.h>#define MAXN 1000000int prime[MAXN];int flag[MAXN];is_prime(){/*筛不超过1000000的素数(注意题目的提示 ;You may assume that there will be at most one factor more than 1000000. )存储到prime数组中,美国服务器,服务器空间,香港空间,并返回素数的个数 */int i, j, n = 0;memset(flag, 0, sizeof(flag));for(i=2; i<MAXN; ++i){if(!flag[i]){prime[n++] = i;flag[i] = 1;for(j=i+i; j<MAXN; j+=i)flag[j] = 1;}}return n;}int main(){int term, track, i, j;long long n;term = is_prime();, &n) != EOF && n >= 0){if(n == 0 || n == 1){//处理 n = 1 和 n = 0 特殊情况 printf();continue;}for(track=i=0; n != 1 && n != 0 && i<term; ++i){while(n%prime[i] == 0){flag[track++] = prime[i];n /= prime[i];}}, flag[i]);, n););}return 0;}

解题思路:

刚开始肯定会觉得难求(这个看样例就知道了)但再回去看一遍题目的时候,就发现了那句很重要的话

You may assume that there will be at most one factor more than 1000000.

这说明最大的因子(素数)并不会超过10^6次方

posted on

努力爱一个人。付出,不一定会有收获;

Factoring Large Numbers

相关文章:

你感兴趣的文章:

标签云: