forked from DEEZZU/Traditional-Cpp-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9_DIV.CPP
72 lines (67 loc) · 1.41 KB
/
9_DIV.CPP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* Question: find the numbers upto N which have 9 divisiors
*Concept: the product of sqaures of 2 prime numbers have exact 9 divisors
*Input: N: the number limit upto which such 9 divisors number are to be generated
*Output: as desired
*Constraint: 1<=n<=1000
*Algorithm: use sieve of eranthoses and generate prime numbers
test p*p<=n (p is prime)
generate p*p*q*q (q is a prime number greater than p ALWAYS ) <=N
print the product of squares if condition satisfied
*TAGS: PRIME NUMBER, SIEVE , SEARCHING
*/
#include<iostream.h>
#include<conio.h>
#define max 200
int prime[max];
void sieve(int n);
void div_3(int n);
void main()
{
clrscr();
int n;
cout<<"\n..... Programing for generating numbers with exactly 3 divisors.....";
cout<<"\n Enter N:";
//N: Max limit upto which numbers are to be generated
cin>>n;
div_3(n);
//sieve(n);
getch();
}
void sieve(int n)
{
prime[0]=prime[1]=1;
for(int i=2;i<=n;i++)
{
if(prime[i]!=1)
//query : it doesmt work on 0 prime[i]!=0 ???
prime[i]=0;
for(int p=i*2;p<=n;p+=i)
prime[p]=1;
}
/*
for(i=0;i<=n;i++)
if(!prime[i])
cout<<" "<<i;
*/
}
void div_3(int n)
{
int i;
//generate all prime numbers first
sieve(n);
for(i=2;i*i<=n;i++)
{
if(!prime[i] )
{
cout<<i<<" ";
for(int j=i+1;i*i*j*j<=n;j++)
{
if(!prime[j])
{cout<<j<<" ";
cout<<i*i*j*j<<" "<<endl;
}
}
}
}
cout<<"end";
}