13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci 173af6ab5fSopenharmony_ciexport class ControlFlowRecursive { 183af6ab5fSopenharmony_ci static readonly n1: int = 3; 193af6ab5fSopenharmony_ci static readonly n2: int = 5; 203af6ab5fSopenharmony_ci static readonly expected: int = 57775; 213af6ab5fSopenharmony_ci 223af6ab5fSopenharmony_ci private static ack(m: int, n: int): int { 233af6ab5fSopenharmony_ci if (m == 0) { 243af6ab5fSopenharmony_ci return n + 1; 253af6ab5fSopenharmony_ci } 263af6ab5fSopenharmony_ci if (n == 0) { 273af6ab5fSopenharmony_ci return ControlFlowRecursive.ack(m - 1, 1); 283af6ab5fSopenharmony_ci } 293af6ab5fSopenharmony_ci return ControlFlowRecursive.ack(m - 1, ControlFlowRecursive.ack(m, n - 1)); 303af6ab5fSopenharmony_ci } 313af6ab5fSopenharmony_ci 323af6ab5fSopenharmony_ci private static fib(n: int): int { 333af6ab5fSopenharmony_ci if (n < 2) { 343af6ab5fSopenharmony_ci return 1; 353af6ab5fSopenharmony_ci } 363af6ab5fSopenharmony_ci return ControlFlowRecursive.fib(n - 2) + ControlFlowRecursive.fib(n - 1); 373af6ab5fSopenharmony_ci } 383af6ab5fSopenharmony_ci 393af6ab5fSopenharmony_ci private static tak(x: int, y: int, z: int): int { 403af6ab5fSopenharmony_ci if (y >= x) { 413af6ab5fSopenharmony_ci return z; 423af6ab5fSopenharmony_ci } 433af6ab5fSopenharmony_ci return ControlFlowRecursive.tak(ControlFlowRecursive.tak(x - 1, y, z), ControlFlowRecursive.tak(y - 1, z, x), ControlFlowRecursive.tak(z - 1, x, y)); 443af6ab5fSopenharmony_ci } 453af6ab5fSopenharmony_ci 463af6ab5fSopenharmony_ci public static run(): void { 473af6ab5fSopenharmony_ci let result: int = 0; 483af6ab5fSopenharmony_ci for (let j: int = ControlFlowRecursive.n1; j <= ControlFlowRecursive.n2; ++j) { 493af6ab5fSopenharmony_ci result += ControlFlowRecursive.ack(3, j); 503af6ab5fSopenharmony_ci result += ControlFlowRecursive.fib(17 + j); 513af6ab5fSopenharmony_ci result += ControlFlowRecursive.tak(3 * j + 3, 2 * j + 2, j + 1); 523af6ab5fSopenharmony_ci } 533af6ab5fSopenharmony_ci 543af6ab5fSopenharmony_ci assert result == ControlFlowRecursive.expected: "Incorrect result"; 553af6ab5fSopenharmony_ci } 563af6ab5fSopenharmony_ci} 573af6ab5fSopenharmony_ci 583af6ab5fSopenharmony_cifunction main(): void { 593af6ab5fSopenharmony_ci ControlFlowRecursive.run(); 603af6ab5fSopenharmony_ci} 61