1/*
2 * Copyright (c) 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
16export class ControlFlowRecursive {
17  static readonly n1: int = 3;
18  static readonly n2: int = 5;
19  static readonly expected: int = 57775;
20
21  private static ack(m: int, n: int): int {
22    if (m == 0) {
23      return n + 1;
24    }
25    if (n == 0) {
26      return ControlFlowRecursive.ack(m - 1, 1);
27    }
28    return ControlFlowRecursive.ack(m - 1, ControlFlowRecursive.ack(m, n - 1));
29  }
30
31  private static fib(n: int): int {
32    if (n < 2) {
33      return 1;
34    }
35    return ControlFlowRecursive.fib(n - 2) + ControlFlowRecursive.fib(n - 1);
36  }
37
38  private static tak(x: int, y: int, z: int): int {
39    if (y >= x) {
40      return z;
41    }
42    return ControlFlowRecursive.tak(ControlFlowRecursive.tak(x - 1, y, z), ControlFlowRecursive.tak(y - 1, z, x), ControlFlowRecursive.tak(z - 1, x, y));
43  }
44
45  public static run(): void {
46    let result: int = 0;
47    for (let j: int = ControlFlowRecursive.n1; j <= ControlFlowRecursive.n2; ++j) {
48      result += ControlFlowRecursive.ack(3, j);
49      result += ControlFlowRecursive.fib(17 + j);
50      result += ControlFlowRecursive.tak(3 * j + 3, 2 * j + 2, j + 1);
51    }
52
53    assert result == ControlFlowRecursive.expected: "Incorrect result";
54  }
55}
56
57function main(): void {
58  ControlFlowRecursive.run();
59}
60