-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathackerman.ts
45 lines (40 loc) · 1.27 KB
/
ackerman.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Tier, Status } from './tier';
/**
* Ackerman tier
* Main resources used: CPU
*/
export default class Ackerman extends Tier {
m: number;
n: number;
/**
*
* @param m First parameter of ackerman function
* @param n Second parameter of ackerman function
* Be aware of the size of the input parameters since it is easy to exceed the stack size.
*/
constructor(m = 3, n=7){
super();
this.m = m;
this.n = n;
}
protected executeTask(): Status {
let result = this.ackerman(this.m, this.n) || -1;
let statusCode = result != -1 ? 200 : 500;
return new Status(`The value of Ackerman for (${this.m}, ${this.n}) is: ${ result }`, statusCode);
}
/**
* ackerman function from https://en.wikipedia.org/wiki/Ackermann_function
* @param m
* @param n
*/
private ackerman(m: number, n: number): number{
try{
if(m === 0) return n+1;
else if(m > 0 && n == 0) return this.ackerman(m-1, 1);
else if(m > 0 && n> 0) return this.ackerman(m-1, this.ackerman(m, n-1));
}catch(exception){
console.error("RangeError: Maximum call stack size exceeded. Try a lower input parameters.")
return -1;
}
}
}