Randell Dawson 04f18e43f6 fix(curriculum): Remove unnecessary assert message argument from English Coding Interview Prep challenges - 02 (#36412)
* fix: removed assert msg argument

* fix: removed msgs surrounded by 2 single quotes

* fix: removed missing 2 assert msg arguments

* fix: remove msg surrounded by two single quotes

* fix: removed unnecessary assert msg args

* fix; remove msgs surrounded by double quotes

* fix: removed unnecessary assert msg args

* fix: remove unnecessary assert msg args

* fix: removed unnecessary assert msg arg

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg arg

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg args

* fix: removed unnecessary assert msg arg

* fix: removed unnecessary assert msg args

* fix: Restore expected values to assertions

* fix: remove assertion message

Co-authored-by: Vivek Agrawal <vivekmittalagrawal@gmail.com>
2019-07-26 14:24:52 +02:00

1.6 KiB

title, id, challengeType
title id challengeType
Ackermann function 594810f028c0303b75339acf 5

Description

The Ackermann function is a classic example of a recursive function, notable especially because it is not a primitive recursive function. It grows very quickly in value, as does the size of its call tree. The Ackermann function is usually defined as follows: $A(m, n) = \begin{cases} n+1 & \mbox{if } m = 0 \\ A(m-1, 1) & \mbox{if } m > 0 \mbox{ and } n = 0 \\ A(m-1, A(m, n-1)) & \mbox{if } m > 0 \mbox{ and } n > 0. \end{cases}$ Its arguments are never negative and it always terminates.

Instructions

Write a function which returns the value of $A(m, n)$. Arbitrary precision is preferred (since the function grows so quickly), but not required.

Tests

tests:
  - text: <code>ack</code> is a function.
    testString: assert(typeof ack === 'function');
  - text: <code>ack(0, 0)</code> should return 1.
    testString: assert(ack(0, 0) === 1);
  - text: <code>ack(1, 1)</code> should return 3.
    testString: assert(ack(1, 1) === 3);
  - text: <code>ack(2, 5)</code> should return 13.
    testString: assert(ack(2, 5) === 13);
  - text: <code>ack(3, 3)</code> should return 61.
    testString: assert(ack(3, 3) === 61);

Challenge Seed

function ack(m, n) {
  // Good luck!
}

Solution

function ack(m, n) {
  return m === 0 ? n + 1 : ack(m - 1, n === 0 ? 1 : ack(m, n - 1));
}