Skip to content

Commit 428ef55

Browse files
Mohit JainMohit Jain
authored andcommitted
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

Lines changed: 46 additions & 0 deletions
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

Lines changed: 62 additions & 0 deletions
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

Lines changed: 66 additions & 0 deletions
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

Lines changed: 50 additions & 0 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
3
1+
2
2+
3
3+
11

0 commit comments

Comments
 (0)