Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

## 1. Информация о студенте

**Номер группы**: 00-000
**Номер группы**: 11-104

**Фамилия и Имя**: Иванов Иван
**Фамилия и Имя**: Камалов Нияз

## 2. Описание задания

Expand Down
41 changes: 23 additions & 18 deletions src/fibonacci.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace assignment {
return n;
}

// Напишите здесь свой код ...

return 0;
double double_n = static_cast<double>(n);
double num = pow((1 + sqrt(5.0))/2.0, double_n) - pow(((1 - sqrt(5.0))/2.0), double_n);
return static_cast<int64_t>(num / sqrt(5.0));
}

int64_t fib_iterative(int n) {
Expand All @@ -27,15 +27,20 @@ namespace assignment {
int64_t fib_curr = 1;

// Напишите здесь свой код ...

return 0;
for (int i = 1; i < n; i++) {
int64_t next = fib_prev + fib_curr;
fib_prev = fib_curr;
fib_curr = next;
}
return fib_curr;
}

int64_t fib_recursive(int n) {

if (n <= 1) {
return n;
}
// Напишите здесь свой код ...

return 0;
return fib_recursive(n-1) + fib_recursive(n-2);
}

int64_t fib_recursive_memoization(int n, std::vector<int64_t>& cache) {
Expand All @@ -45,13 +50,14 @@ namespace assignment {
}

if (cache[n] != -1) {
// Напишите здесь свой код ...
return 0;
return cache[n];
}

// Напишите здесь свой код ...

return 0;
int64_t prev1 = fib_recursive_memoization(n-1, cache);
cache[n-1] = prev1;
int64_t prev2 = fib_recursive_memoization(n-2, cache);
cache[n-2] = prev2;
return prev1 + prev2;
}

int64_t fib_matrix(int n) {
Expand All @@ -60,11 +66,10 @@ namespace assignment {
return n;
}

// Напишите здесь свой код ...

Matrix2x2 P = {{{0, 1}, {1, 1}}};
P = matrix_power(P, n-1);
// Tip: используйте реализованную функцию matrix_pow

return 0;
return P[0][0] + P[1][0];
}

} // namespace assignment
} // namespace assignment
11 changes: 9 additions & 2 deletions src/matrix2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,15 @@ namespace assignment {
Matrix2x2 res = IdentityMatrix; // единичная матрица

while (power != 0) {

// Напишите здесь свой код ...
// res = matrix_multiply(res, matrix);
// power -= 1;
if (power & 1) {
res = matrix_multiply(res, matrix);
power -= 1;
} else {
matrix = matrix_multiply(matrix, matrix);
power /= 2;
}
}

return res;
Expand Down