← Back to challenges

Natural Emptiness

JavaScriptHardarraysmathnumbersrecursion

Instructions

In abstract set theory, a construction due to von Neumann represents the natural numbers by sets, as follows:

  • 0 = [ ] is the empty set
  • 1 = 0 βˆͺ [ 0 ] = [ 0 ] = [ [ ] ]
  • 2 = 1 βˆͺ [ 1 ] = [ 0, 1 ] = [ [ ], [ [ ] ] ]
  • n = nβˆ’1 βˆͺ [ nβˆ’1 ] = ...

Write a function that receives an integer n and produces the representing set.

Examples

repSet(0) ➞ []

repSet(1) ➞ [[]]

repSet(2) ➞ [[], [[]]]

repSet(3) ➞ [[], [[]], [[], [[ ]]]]

Notes

  • Make sure to use array brackets [,].
  • A neat feature of this representation is that n < m precisely if the set representing n is contained in the set representing m.
javascript
Loading editor…
⌘ ↡ to run
Walks through the solution with reasoning and edge cases.