-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathe.java
More file actions
90 lines (80 loc) · 2.06 KB
/
Mathe.java
File metadata and controls
90 lines (80 loc) · 2.06 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
/**
* Mathematical operations: sum, sub, mult, division, mod and pow made with recursion and cosidering negative numbers
* Negative numbers as input are considered for sum, sub, mult and division
*/
public class Mathe {
// add considering negative nums
public static int addRek(int a, int b) {
if (b > -1) {
if (b == 0) {
return a;
} else {
return (1 + addRek(a, b - 1));
}
} else {
return subRek(a, (-1) * b);
}
}
// sub considering negative nums
public static int subRek(int a, int b) {
if (b > -1) {
if (b == 0) {
return a;
} else {
return subRek((a - 1), (b - 1));
}
} else {
return addRek(a, (-1) * b);
}
}
// mult considering negative nums
public static int mulRek(int a, int b) {
if (b > -1) {
if (a < b) {
return mulRek(b, a);
} else if (b != 0) {
return (a + mulRek(a, b - 1));
} else {
return 0;
}
}
else
b = -b;
return -(a + mulRek(a, b - 1));
}
// div considering negative nums
public static int divRek(int a, int b) {
boolean isNeg = false;
if (a < 0 && b < 0) {
a = -a;
b = -b;
} else if (a > 0 && b < 0) {
b = -b;
isNeg = true;
} else if (a < 0 && b > 0) {
a = -a;
isNeg = true;
}
if (a < b) {
return 0;
}
if (!isNeg) {
return 1 + divRek(a - b, b);
} else {
return -(1 + divRek(a - b, b));
}
}
// mod
public static int modRek(int a, int b){
int q = (int)divRek(a,b);
int p = q * b;
return a - p;
}
// pow
public static int powRek(int b, int e){
if (e != 0)
return (b * powRek(b, e - 1));
else
return 1;
}
}