update README.md for tasks 1 & 2

This commit is contained in:
2026-01-29 21:11:51 +05:00
parent 49f73eed07
commit b34d65a56b
3 changed files with 116 additions and 30 deletions

View File

@@ -1,4 +1,4 @@
# Problem RL -- рекурсия в цикл
# Problem RL - рекурсия в цикл
Ниже приведён исходный код программы для рекурсивного алгоритма Евклида.
@@ -54,3 +54,55 @@ int main() {
return 0;
}
```
# Решение
```c
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define ull unsigned long long
#define ll long long
ull gcd_cycle(ll x, ll y);
ull gcd_recursive(ull x, ull y);
int main() {
ull x = 0, y = 0, g;
int res;
res = scanf("%llu %llu", &x, &y);
assert(res == 2);
g = gcd_cycle(x, y);
printf("%llu\n", g);
return 0;
}
ull gcd_recursive(ull x, ull y) {
ull q;
if (y > x)
return gcd_recursive(y, x);
assert (y > 0);
q = x % y;
if (q == 0)
return y;
return gcd_recursive(y, q);
}
ull gcd_cycle(ll x, ll y) {
x = llabs(x);
y = llabs(y);
if (x == 0) return y;
if (y == 0) return x;
while (y != 0) {
ll r = x % y;
x = y;
y = r;
}
return x;
}
```