Skip to content

Commit 09d6371

Browse files
committed
source code attached
0 parents  commit 09d6371

28 files changed

+640
-0
lines changed

Bit_Magic/Check_kth_bit.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Bit_Magic;
2+
3+
import java.net.SocketPermission;
4+
5+
public class Check_kth_bit {
6+
public void isBitSet(int n, int k){
7+
if((n & (1 << k)) != 0) {
8+
System.out.println("The bit is set");
9+
}
10+
else{
11+
System.out.println("The bit is not set");
12+
}
13+
}
14+
public static void main(String[] args) {
15+
Check_kth_bit obj = new Check_kth_bit();
16+
obj.isBitSet(45, 2);
17+
}
18+
}

Bit_Magic/Count_Bits.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package Bit_Magic;
2+
3+
public class Count_Bits {
4+
// Brian kerningams algorithm
5+
int countSetBits(int n) {
6+
int res= 0;
7+
while( n > 0) {
8+
n = n & (n-1);
9+
res++;
10+
}
11+
return res;
12+
}
13+
public static void main(String[] args) {
14+
Count_Bits obj = new Count_Bits();
15+
System.out.println(obj.countSetBits(5));
16+
}
17+
}

Bit_Magic/Odd_element.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Bit_Magic;
2+
3+
public class Odd_element {
4+
//naive solution
5+
// int findOdd(int arr[], int n) {
6+
// for(int i= 0; i<n; i++) {
7+
// int count = 0;
8+
// for(int j = 0; j<n; j++){
9+
// if(arr[i] == arr[j]){
10+
// count++;
11+
// }
12+
// }
13+
// if(count %2 != 0){
14+
// return arr[i];
15+
// }
16+
// }
17+
// return 0;
18+
// }
19+
20+
//efficient solution
21+
int findOdd(int arr[], int n){
22+
int res = arr[0];
23+
for(int i=1; i<n; i++) {
24+
res = res ^ arr[i];
25+
}
26+
return res;
27+
}
28+
public static void main(String[] args) {
29+
Odd_element obj = new Odd_element();
30+
// obj.findOdd([4,4,7,4,8,7,7,7,8], 9)
31+
}
32+
}

Bit_Magic/PowerSet.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Bit_Magic;
2+
3+
public class PowerSet {
4+
public void printPowerSet(String s) {
5+
int n = s.length();
6+
int psize = (1 << n);
7+
for(int i=0; i<psize; i++){
8+
for(int j=0; j<n; j++) {
9+
if( (i & (1 << j)) != 0) {
10+
System.out.println(s.charAt(j));
11+
}
12+
}
13+
}
14+
}
15+
public static void main(String[] args) {
16+
PowerSet obj = new PowerSet();
17+
}
18+
}

Bit_Magic/Power_Two.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package Bit_Magic;
2+
3+
public class Power_Two {
4+
// naive solution
5+
// boolean isPow2(int n) {
6+
// if(n==0){
7+
// return false;
8+
// }
9+
// while(n != 1){
10+
// if(n%2 != 0){
11+
// return false;
12+
// }
13+
// n = n/2;
14+
// }
15+
// return true;
16+
// }
17+
18+
//brian kerningams
19+
boolean isPow2(int n){
20+
if(n==0){
21+
return false;
22+
}
23+
return ((n & (n-1)) == 0);
24+
}
25+
public static void main(String[] args) {
26+
Power_Two obj = new Power_Two();
27+
System.out.println(obj.isPow2(6));
28+
System.out.println(obj.isPow2(4));
29+
}
30+
}

Bit_Magic/Two_Odd.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Bit_Magic;
2+
3+
public class Two_Odd {
4+
public void oddAppearing(int arr[], int n) {
5+
int x = arr[0];
6+
for(int i=1; i<n; i++){
7+
x = x ^ arr[i];
8+
}
9+
int k = (x & (~(x - 1)));
10+
int res1 = 0, res2 = 0;
11+
for(int i=0; i<n; i++) {
12+
if((arr[i] & k) != 0) {
13+
res1 = res1 ^ arr[i];
14+
}
15+
else{
16+
res2 = res2 ^ arr[i];
17+
}
18+
}
19+
System.out.println(res1);
20+
System.out.println(res2);
21+
}
22+
public static void main(String[] args) {
23+
Two_Odd obj = new Two_Odd();
24+
// obj.oddAppearing({}, 5);
25+
}
26+
}

Mathematics/Computing_power.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package Mathematics;
2+
3+
public class Computing_power {
4+
int power(int x, int n) {
5+
int res = 1;
6+
for(int i=0; i<n; i++) {
7+
res = res * x;
8+
}
9+
return res;
10+
}
11+
12+
int pow(int x, int n) {
13+
if(n == 0) {
14+
return 1;
15+
}
16+
int temp = pow(x, n/2);
17+
temp = temp * temp;
18+
if(n%2 == 0) {
19+
return temp;
20+
}
21+
else{
22+
return temp * x;
23+
}
24+
}
25+
public static void main(String[] args) {
26+
Computing_power obj = new Computing_power();
27+
System.out.println(obj.power(2, 3));
28+
System.out.println(obj.pow(3, 5));
29+
}
30+
}

Mathematics/Divisors.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Mathematics;
2+
3+
public class Divisors {
4+
void printDivisors(int n) {
5+
for(int i=1; i<=n; i++) {
6+
if(n%i == 0) {
7+
System.out.println(i);
8+
}
9+
}
10+
}
11+
12+
void printDivisor(int n) {
13+
for(int i=1; i*i <= n; i++) {
14+
if(n%i == 0) {
15+
System.out.println(i);
16+
if(i != n/i) {
17+
System.out.println(n/i);
18+
}
19+
}
20+
}
21+
} // print divisors but not in sorted order
22+
23+
void print(int n) {
24+
int i;
25+
for(i =1; i*i < n; i++) {
26+
if(n%i == 0) {
27+
System.out.println(i);
28+
}
29+
}
30+
for(; i>=1; i--) {
31+
if(n%i == 0) {
32+
System.out.println(n/i);
33+
}
34+
}
35+
}
36+
public static void main(String[] args) {
37+
Divisors obj = new Divisors();
38+
obj.printDivisors(6);
39+
obj.printDivisor(15);
40+
obj.print(15);
41+
}
42+
}

Mathematics/Euclidean_gcd.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package Mathematics;
2+
3+
public class Euclidean_gcd {
4+
int gcd(int a, int b){
5+
while(a != b) {
6+
if (a > b){
7+
a = a-b;
8+
}
9+
else{
10+
b = b-a;
11+
}
12+
}
13+
return a;
14+
}
15+
public static void main(String[] args) {
16+
Euclidean_gcd obj = new Euclidean_gcd();
17+
System.out.println(obj.gcd(12, 15));
18+
System.out.println(obj.gcd(7, 13));
19+
}
20+
}
21+

Mathematics/IterativePower.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Mathematics;
2+
3+
public class IterativePower {
4+
int power(int x, int n) {
5+
int res = 1;
6+
while(n > 0) {
7+
if(n % 2 != 0) {
8+
res = res * x;
9+
}
10+
x = x * x;
11+
n = n/2;
12+
}
13+
return res;
14+
}
15+
public static void main(String[] args) {
16+
IterativePower obj = new IterativePower();
17+
System.out.println(obj.power(4, 5));
18+
}
19+
}

0 commit comments

Comments
 (0)