Hausaufgabe
This commit is contained in:
parent
cf4af5fc8d
commit
cc0e9bfb77
38
Algorithms
Normal file
38
Algorithms
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
public class Algorithm {
|
||||||
|
// Nr 2
|
||||||
|
public int binarySearch(int[] arr, int left, int right, int target) {
|
||||||
|
if (left > right) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid = left + (right - left) / 2;
|
||||||
|
|
||||||
|
if (arr[mid] == target) {
|
||||||
|
return mid;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (target < arr[mid]) {
|
||||||
|
return binarySearch(arr, left, mid - 1, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
return binarySearch(arr, mid + 1, right, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
// NR 1
|
||||||
|
public int ggt(int m, int n) {
|
||||||
|
while (n != 0) {
|
||||||
|
int rest = m % n;
|
||||||
|
m = n;
|
||||||
|
n = rest;
|
||||||
|
}
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
// NR 3
|
||||||
|
public long potenz(long a, int b) {
|
||||||
|
if (b == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return a * potenz(a, b - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user