-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPowerOperation.java
More file actions
47 lines (42 loc) · 1021 Bytes
/
PowerOperation.java
File metadata and controls
47 lines (42 loc) · 1021 Bytes
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
package math;
/**
* Write power operation without using multiplication or division.
*
* Link:
* http://www.geeksforgeeks.org/write-you-own-power-without-using-multiplication
* -and-division/
*
* @author shivam.maharshi
*/
public class PowerOperation {
public static int get(int n, int pow) {
int a = n;
int b = n;
for (int i = 1; i < pow; i++) {
for (int j = 1; j < n; j++) {
a += b;
}
b = a;
}
System.out.println(a);
return a;
}
/*
* Converges logarithmically. To avoid using multiplication operator, just run
* a for loop with addition.
*/
public static int pow(int n, int pow) {
if (n == 1 || pow == 0)
return 1;
if (pow == 1)
return n;
else if (pow % 2 == 0)
return pow(n * n, pow / 2);
else
return n * pow(n * n, (pow - 1) / 2);
}
public static void main(String[] args) {
PowerOperation.get(5, 6);
System.out.println(pow(5, 6));
}
}