-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfactorisation_range_based.cpp
More file actions
91 lines (57 loc) · 1.55 KB
/
factorisation_range_based.cpp
File metadata and controls
91 lines (57 loc) · 1.55 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include<bits/stdc++.h>
using namespace std;
#include<bitset> // for memory efficient
#define max 100 // till 10 ^ 7
bitset<1000000> isprime ={0};
vector<int> prime; // it will store all prime no.
void seive()
{
// assumtion no no is prime.
// 2 is prime
isprime.set(2);
prime.push_back(2);
// assumption all odd are prime.
for(int i=3;i<=max;i+=2)
{
isprime.set(i);
}
// now some odd are not prime
for(int i=3;i<=max;i+=2)
{
if(isprime[i]==1) {
prime.push_back(i);
// all multiple of this prime are not prime
for(int j=i*i;j<=max;j+=i)
{
isprime[j]=0;
}
}
}
}
int32_t main(){
// precomputation of prime - no
seive();
// for range factorisation.
// time complexity is nloglog(root(n)) + log(n) <= nlog(n)
// in this i simply divide all by prime factor insted of all factors where all prime number are stored in vector prime.
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int prime_pos=0;
int val=prime[prime_pos];
for(int i=0;val*val<=n;i++)
{
val=prime[i];
while(n%val==0)
{
cout<<val<<" "; // change code according to you i am simply printing factor ..here val is prime factor.
n=n/val;
}
cout<<endl;
}
if(n!=1) cout<<n<<endl;
}
}