Files
hello-algo/ru/codes/zig/chapter_computational_complexity/recursion.zig
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

89 lines
2.5 KiB
Zig
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// File: recursion.zig
// Created Time: 2023-09-27
// Author: QiLOL (pikaqqpika@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
const std = @import("std");
// Рекурсивная функция
fn recur(n: i32) i32 {
// Условие завершения
if (n == 1) {
return 1;
}
// Рекурсия: рекурсивный вызов
const res = recur(n - 1);
// Возврат: вернуть результат
return n + res;
}
// Имитация рекурсии итерацией
fn forLoopRecur(comptime n: i32) i32 {
// Использовать явный стек для имитации системного стека вызовов
var stack: [n]i32 = undefined;
var res: i32 = 0;
// Рекурсия: рекурсивный вызов
var i: usize = n;
while (i > 0) {
stack[i - 1] = @intCast(i);
i -= 1;
}
// Возврат: вернуть результат
var index: usize = n;
while (index > 0) {
index -= 1;
res += stack[index];
}
// res = 1+2+3+...+n
return res;
}
// Хвосторекурсивная функция
fn tailRecur(n: i32, res: i32) i32 {
// Условие завершения
if (n == 0) {
return res;
}
// Хвостовой рекурсивный вызов
return tailRecur(n - 1, res + n);
}
// Числа Фибоначчи
fn fib(n: i32) i32 {
// Условие завершения: f(1) = 0, f(2) = 1
if (n == 1 or n == 2) {
return n - 1;
}
// Рекурсивный вызов f(n) = f(n-1) + f(n-2)
const res: i32 = fib(n - 1) + fib(n - 2);
// Вернуть результат f(n)
return res;
}
// Driver Code
pub fn run() void {
const n: i32 = 5;
var res: i32 = 0;
res = recur(n);
std.debug.print("Результат суммирования в рекурсивной функции res = {}\n", .{recur(n)});
res = forLoopRecur(n);
std.debug.print("Результат суммирования при имитации рекурсии итерацией res = {}\n", .{forLoopRecur(n)});
res = tailRecur(n, 0);
std.debug.print("Результат суммирования в хвостовой рекурсии res = {}\n", .{tailRecur(n, 0)});
res = fib(n);
std.debug.print("Член последовательности Фибоначчи с номером {} = {}\n", .{ n, fib(n) });
std.debug.print("\n", .{});
}
pub fn main() void {
run();
}
test "recursion" {
run();
}