1/*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17export class ControlFlowRecursive {
18  static readonly n1: int = 3;
19  static readonly n2: int = 5;
20  static readonly expected: int = 57775;
21
22  private static ack(m: int, n: int): int {
23    if (m == 0) {
24      return n + 1;
25    }
26    if (n == 0) {
27      return ControlFlowRecursive.ack(m - 1, 1);
28    }
29    return ControlFlowRecursive.ack(m - 1, ControlFlowRecursive.ack(m, n - 1));
30  }
31
32  private static fib(n: int): int {
33    if (n < 2) {
34      return 1;
35    }
36    return ControlFlowRecursive.fib(n - 2) + ControlFlowRecursive.fib(n - 1);
37  }
38
39  private static tak(x: int, y: int, z: int): int {
40    if (y >= x) {
41      return z;
42    }
43    return ControlFlowRecursive.tak(ControlFlowRecursive.tak(x - 1, y, z), ControlFlowRecursive.tak(y - 1, z, x), ControlFlowRecursive.tak(z - 1, x, y));
44  }
45
46  public static run(): void {
47    let result: int = 0;
48    for (let j: int = ControlFlowRecursive.n1; j <= ControlFlowRecursive.n2; ++j) {
49      result += ControlFlowRecursive.ack(3, j);
50      result += ControlFlowRecursive.fib(17 + j);
51      result += ControlFlowRecursive.tak(3 * j + 3, 2 * j + 2, j + 1);
52    }
53
54    assert result == ControlFlowRecursive.expected: "Incorrect result";
55  }
56}
57
58function main(): void {
59  ControlFlowRecursive.run();
60}
61