Just another WordPress.com site

 সুলতান আহমেদ সাগর এই সমাধানটি করেছেন ।
Theory Difficulty easy
Coding Difficulty: easy
Algorithms Used: math
Solution Description: Use the Sieve of Eratosthenes to generate all the primes up to 1,000,000. For each input, check whether each prime divides it.

You don’t need to do an O(sqrt(n)) check. O(n) will do fine.

#include<iostream>
#include<cmath>
using namespace std;

#define max 1000000
int a[max];

void seieve()
{
long long int i,j,imax,jmax;
for(i=0;i<=max;i++) a[i]=1;
imax=sqrt(max)+1;
for(i=2;i<=imax;i++)
{
jmax=(max/i)+1;
if(a[i]==1)
{
for(j=2;j<=jmax;j++)
{
if(i*j<=max) a[i*j]=0;
}
}
}
}

int main()
{
seieve();
long long int i,N,temp,prime=0;
while(cin>>N && N!=0)
{
temp=N;
for(i=2;i<=N;i++)
if(N%i==0 && a[i])
{
++prime;
N/=i;
}

cout<<temp<<” : “<<prime<<endl;
prime=0;
}

return 0;
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: