Skip to content

Commit 428ef55

Browse files
Mohit JainMohit Jain
Mohit Jain
authored and
Mohit Jain
committed
Number theory concept
1 parent 58adeab commit 428ef55

13 files changed

+411
-3
lines changed

FastExponentiation

34.5 KB
Binary file not shown.

FastExponentiation.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
6+
int power_optimised(int a,int n) {
7+
int ans = 1;
8+
9+
while (n > 0) {
10+
11+
int lastBit = n&1;
12+
13+
if(lastBit) {
14+
ans = ans*a;
15+
16+
}
17+
a = a*a;
18+
19+
n = n>>1;
20+
21+
}
22+
23+
return ans;
24+
25+
}
26+
27+
int main() {
28+
29+
#ifndef ONLINE_JUDGE
30+
// for getting input from input.txt
31+
freopen("input.txt", "r", stdin);
32+
// for writing output to output.txt
33+
freopen("output.txt", "w", stdout);
34+
#endif
35+
36+
int a,n;
37+
38+
cin>>a>>n;
39+
40+
cout<<power_optimised(a,n)<<endl;
41+
42+
43+
44+
45+
return 0;
46+
}

PrimeFactorizationRoot

50.5 KB
Binary file not shown.

PrimeFactorizationRoot.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
6+
void printFactors(int n) {
7+
8+
vector<pair<int, int> > factors;
9+
int cnt;
10+
11+
for(int i =2;i*i <=n; i++) {
12+
13+
14+
if(n % i == 0) {
15+
cnt = 0;
16+
17+
while( n % i == 0) {
18+
cnt++;
19+
20+
n = n/i;
21+
}
22+
factors.push_back(make_pair(i,cnt));
23+
}
24+
}
25+
if(n!= 1) {
26+
factors.push_back(make_pair(n,1));
27+
28+
}
29+
30+
for(auto p : factors) {
31+
32+
cout<< p.first << "^" << p.second << endl;
33+
}
34+
35+
return;
36+
}
37+
int main() {
38+
39+
40+
#ifndef ONLINE_JUDGE
41+
// for getting input from input.txt
42+
freopen("input.txt", "r", stdin);
43+
// for writing output to output.txt
44+
freopen("output.txt", "w", stdout);
45+
#endif
46+
47+
int n;
48+
49+
cin>>n;
50+
51+
printFactors(n);
52+
53+
54+
55+
56+
57+
58+
59+
60+
61+
return 0;
62+
}

PrimeSieve

39.1 KB
Binary file not shown.

PrimeSieve.cpp

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Sieve Approach - generate an array
2+
//containing prime numbers
3+
4+
#include <bits/stdc++.h>
5+
6+
using namespace std;
7+
8+
void prime_sieve(int *p) {
9+
10+
// first mark all odd numbers as prime
11+
for(int i = 3; i < 1000000; i+=2 ) {
12+
p[i] = 1;
13+
}
14+
15+
for(long long i =3; i <= 1000000; i +=2) {
16+
17+
if(p[i] == 1) {
18+
19+
20+
for(long long j = i*i; j <= 1000000 ; j += i) {
21+
p[j] = 0;
22+
23+
}
24+
}
25+
26+
27+
28+
}
29+
30+
// special case
31+
32+
33+
p[1] = p[0] = 0;
34+
p[2] = 1;
35+
36+
}
37+
38+
39+
40+
int main() {
41+
42+
43+
#ifndef ONLINE_JUDGE
44+
// for getting input from input.txt
45+
freopen("input.txt", "r", stdin);
46+
// for writing output to output.txt
47+
freopen("output.txt", "w", stdout);
48+
#endif
49+
50+
int n;
51+
52+
cin>>n;
53+
54+
int p[1000005] = {0};
55+
56+
prime_sieve(p);
57+
for(int i =0 ; i < n ; i ++) {
58+
59+
if(p[i] == 1) {
60+
cout<<i<<" ";
61+
}
62+
}
63+
64+
65+
return 0;
66+
}

SubSequencesBit

40.6 KB
Binary file not shown.

SubSequencesBit.cpp

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// find subsequencof string
2+
3+
4+
#include <iostream>
5+
6+
using namespace std;
7+
8+
9+
void filterChars(int n, char a[]) {
10+
11+
int j =0;
12+
13+
while(n >0) {
14+
int lastBit = (n &1);
15+
if (lastBit) {
16+
cout<<a[j];
17+
}
18+
19+
j++;
20+
n = n>>1;
21+
}
22+
23+
cout<<endl;
24+
}
25+
void printSubsets(char a[]) {
26+
27+
28+
int n = strlen(a);
29+
30+
for(int i =1; i < (1<<n) ; i ++) {
31+
filterChars(i,a);
32+
}
33+
}
34+
35+
int main() {
36+
#ifndef ONLINE_JUDGE
37+
// for getting input from input.txt
38+
freopen("input.txt", "r", stdin);
39+
// for writing output to output.txt
40+
freopen("output.txt", "w", stdout);
41+
#endif
42+
43+
char a[100];
44+
45+
cin>>a;
46+
47+
printSubsets(a);
48+
49+
return 0;
50+
}

input.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
7
2-
1 1 1 2 2 2 3
1+
1
2+
132

output.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
3
1+
2
2+
3
3+
11

primeFactorisationSieve

49.8 KB
Binary file not shown.

primeFactorisationSieve.cpp

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// useful when we have no of queries to solve
2+
3+
#include <bits/stdc++.h>
4+
5+
using namespace std;
6+
vector<int> prime_sieve(int *p) {
7+
8+
// first mark all odd numbers as prime
9+
for(int i = 3; i < 1000000; i+=2 ) {
10+
p[i] = 1;
11+
}
12+
13+
for(long long i =3; i <= 1000000; i +=2) {
14+
15+
if(p[i] == 1) {
16+
17+
18+
for(long long j = i*i; j <= 1000000 ; j += i) {
19+
p[j] = 0;
20+
21+
}
22+
}
23+
24+
25+
26+
}
27+
28+
// special case
29+
30+
31+
p[1] = p[0] = 0;
32+
p[2] = 1;
33+
vector<int> primes;
34+
35+
primes.push_back(2);
36+
37+
for(int i =3; i <= 1000000; i +=2) {
38+
if(p[i] == 1) {
39+
primes.push_back(i);
40+
}
41+
}
42+
return primes;
43+
}
44+
45+
vector<int> factiorize(int m, vector<int> &primes) {
46+
47+
vector<int> factors;
48+
factors.clear();
49+
50+
int i =0;
51+
int p = primes[0];
52+
53+
while(p*p <= m) {
54+
if(m%p == 0) {
55+
factors.push_back(p);
56+
while(m%p == 0) {
57+
m = m/p;
58+
}
59+
}
60+
i++;
61+
p = primes[i];
62+
}
63+
if(m != 1) {
64+
factors.push_back(m);
65+
}
66+
return factors;
67+
}
68+
69+
int main() {
70+
71+
72+
#ifndef ONLINE_JUDGE
73+
// for getting input from input.txt
74+
freopen("input.txt", "r", stdin);
75+
// for writing output to output.txt
76+
freopen("output.txt", "w", stdout);
77+
#endif
78+
79+
int p[1000005] = {0};
80+
81+
vector<int> primes = prime_sieve(p);
82+
83+
int t;
84+
85+
cin >> t;
86+
87+
while(t--) {
88+
89+
int no;
90+
91+
cin>>no;
92+
93+
vector<int> factors = factiorize(no, primes);
94+
95+
for(auto f : factors) {
96+
cout<< f << " " << endl;
97+
}
98+
}
99+
100+
101+
102+
103+
return 0;
104+
}

0 commit comments

Comments
 (0)