Fuzion Logo
flang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

c_man_or_boy/man_or_boy.fz


// using man_or_boy as a simple performance benchmark
//
// This calculates Knuth's man-or-boy function for the values 0..14
// repeatedly 200 times.
//
// Note that this is heavily recursive, it requires a large stack.

man_or_boy is

  a(k i32, x1, x2, x3, x4, x5 () -> i32) i32 is
    b => set k := k - 1; a k (fun b) x1 x2 x3 x4
    if k <= 0 x4() + x5() else b

  K(n i32) => fun n

  test(num, max, expected i32) i32
    post
      result == expected // NYI: misusing postcondition, check does not work yet
  is
    res := 0
    for i in 1..num do
      (0..max) | n ->
        set res := res + (a n (K 1) (K -1) (K -1) (K 1) (K 0))
    say "$num * sum (n in 0..$max: man_or_boy(n)) == $res"
    res

//  test(200, 12, -107200)      -- about 16s
//  test(200, 13, -235600)      -- about 40s
  _ := test(200, 14, -524800)     // -- about 10min
//  test(200, 16, -2623600)     -- about 5h, swapping memory