素数筛选法

Eddy's research I

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2659    Accepted Submission(s): 1644


Problem Description
Eddy's interest is very extensive, recently he is interested in prime number. Eddy discover the all number owned can be divided into the multiply of prime number, but he can't write program, so Eddy has to ask intelligent you to help him, he asks you to write a program which can do the number to divided into the multiply of prime number factor .

 

Input
The input will contain a number 1 < x<= 65535 per line representing the number of elements of the set.
 

Output
You have to print a line in the output for each entry with the answer to the previous question.
 

Sample Input
   
   
   
   
11 9412
 

Sample Output
   
   
   
   
11 2*2*13*181
 

Author
eddy
 

Recommend
JGShining
 


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int prime[50000],num,is[70000];
void prm(int n)
{
  int i,j,k=0;
  int s;
  int e=(int)(sqrt(0.0+n)+1);
  memset(is,1,sizeof(is));
  prime[k++]=2;
  is[0]=is[1]=0;
  for(i=4;i<n;i+=2) is[i]=0;
  for(i=3;i<e;i+=2)
    {
      if(is[i]){
    prime[k++]=i;
      for(s=i*2,j=i*i;j<n;j+=s)
    {
      is[j]=0;
    }
      }
    }
  for(;i<n;i=i+2)
    if(is[i])
      prime[k++]=i;
}
int main()
{
  int n;
  prm(70000-1);
  /*  for(int i=0;i<=100;i++)
    {
      printf("%d ",prime[i]);
      }*/
  while(scanf("%d",&n)!=EOF)
    {
      int temp=0;
      int k=0;
      while(n!=1)
    {
      if(n%prime[k]==0)
        {
          n=n/prime[k];
          if(temp==0)
        {
          temp++;
          printf("%d",prime[k]);
        }
          else
        {
          printf("*%d",prime[k]);
        }
        }
      else k++;
    }
      printf("\n");
    }
  return 0;
}

你可能感兴趣的:(素数筛选法)