4
\$\begingroup\$

Introduction

In mathematics, the hyperoperation sequence is an infinite sequence of arithmetic operations (called hyperoperations) that starts with the unary operation of successor (n = 0), then continues with the binary operations of addition (n = 1), multiplication (n = 2), and exponentiation (n = 3), after which the sequence proceeds with further binary operations extending beyond exponentiation, using right-associativity.

(Source)


Challenge

Your challenge is code this sequence, given 3 inputs, n, a, and b, code a function such that $${\displaystyle H_{n}(a,b)={\begin{cases}b+1&{\text{if }}n=0\\a&{\text{if }}n=1{\text{ and }}b=0\\0&{\text{if }}n=2{\text{ and }}b=0\\1&{\text{if }}n\geq 3{\text{ and }}b=0\\H_{n-1}(a,H_{n}(a,b-1))&{\text{otherwise}}\end{cases}}}$$ (Also from Wikipedia.)


Input

3 positive decimal integers, n, a, and b, taken from STDIN, function or command line arguments, in any order. Make sure to specify this in your answer


Output

The result of applying \$H_{n}(a,b)\$ with the inputs


Example inputs and outputs

Input: 0, 6, 3 Output: 4


Input: 4 5 2 Output: 3125


Restrictions

  • Your program/function should take input in base 10
  • Don't use any built in function that already provides H(n, a, b)
  • Standard loopholes apply

This is , so shortest code in bytes wins!

\$\endgroup\$
4
  • \$\begingroup\$ @JoKing Does it? I'll have a read \$\endgroup\$
    – kepe
    Sep 15, 2018 at 14:57
  • \$\begingroup\$ @Arnauld will do. \$\endgroup\$
    – kepe
    Sep 15, 2018 at 15:05
  • \$\begingroup\$ (not really. That one "bans builtins" because it's from '13; plus, cumbersome input/output format) \$\endgroup\$
    – DELETE_ME
    Sep 15, 2018 at 15:08
  • \$\begingroup\$ @dzaima fixed it. \$\endgroup\$
    – kepe
    Sep 15, 2018 at 15:19

3 Answers 3

3
\$\begingroup\$

Dyalog APL, 35 bytes

{⍺=0:⍵+1⋄⍵=0:⍺⍺⍵1⊃⍨3⌊⍺⋄(⍺-1)∇⍺∇⍵-1}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ you can save a byte by inverting the second guard: {⍺=0:⍵+1⋄×⍵:(⍺-1)∇⍺∇⍵-1⋄⍺⍺⍵1⊃⍨3⌊⍺} \$\endgroup\$
    – ngn
    Sep 15, 2018 at 21:58
3
\$\begingroup\$

JavaScript (ES6),  49  47 bytes

Takes input as (a)(b,n).

a=>g=(b,n)=>n*b?g(g(b-1,n),n-1):-~[b,a-1,-1][n]

Try it online!

Commented

a =>                  // main function taking a
  g = (b, n) =>       // g = recursive function taking b and n
    n * b ?           // if neither n nor b is equal to 0:
      g(              //   go recursive:
        g(b - 1, n),  //     b = g(b - 1, n)
        n - 1         //     decrement n
      )               //   end of recursive call
    :                 // else:
      -~[             //   this is a leaf node:
        b,            //     if n = 0, return b + 1
        a - 1,        //     if n = 1, return (a - 1) + 1 = a
        -1            //     if n = 2, return -1 + 1 = 0
      ][n]            //     if n > 2, return -~undefined = 1
\$\endgroup\$
0
1
\$\begingroup\$

Elixir, 86 bytes

(assumes functions can take a reference to itself as an argument)

fn 0,_,b,_->b+1
1,a,0,_->a
2,_,0,_->0
_,_,0,_->1
n,a,b,f->f.(n-1,a,f.(n,a,b-1,f),f)end

Try it online!

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.