6 kyu

Mutual Recursion

4,341 of 8,705dnolan

Description:

Mutual Recursion allows us to take the fun of regular recursion (where a function calls itself until a terminating condition) and apply it to multiple functions calling each other!

Let's use the Hofstadter Female and Male sequences to demonstrate this technique. You'll want to create two functions F and M such that the following equations are true:

F(0) = 1
M(0) = 0
F(n) = n - M(F(n - 1))
M(n) = n - F(M(n - 1))

Don't worry about negative numbers, n will always be greater than or equal to zero.

Hofstadter Wikipedia Reference http://en.wikipedia.org/wiki/Hofstadter_sequence#Hofstadter_Female_and_Male_sequences

Mathematics
Algorithms
Recursion

Stats:

CreatedJun 18, 2014
PublishedJun 18, 2014
Warriors Trained14432
Total Skips744
Total Code Submissions18838
Total Times Completed8705
JavaScript Completions4341
TypeScript Completions214
C# Completions154
C++ Completions848
C Completions517
PHP Completions31
Python Completions2240
R Completions58
Ruby Completions140
NASM Completions39
Kotlin Completions213
Clojure Completions43
Haskell Completions57
Julia Completions48
CoffeeScript Completions18
Crystal Completions11
COBOL Completions4
λ Calculus Completions14
Go Completions85
Rust Completions80
OCaml Completions31
Prolog Completions10
Java Completions86
Total Stars164
% of votes with a positive feedback rating79% of 877
Total "Very Satisfied" Votes595
Total "Somewhat Satisfied" Votes201
Total "Not Satisfied" Votes81
Ad
Contributors
  • dnolan Avatar
  • jhoffner Avatar
  • glebec Avatar
  • prestidigitation Avatar
  • donaldsebleung Avatar
  • user5036852 Avatar
  • JohanWiltink Avatar
  • Torkel Avatar
  • Madjosz Avatar
  • rowcased Avatar
  • KenKamau Avatar
  • mentalplex Avatar
  • Egorka11 Avatar
  • tebek Avatar
  • monadius Avatar
  • HoshinoTented Avatar
  • hobovsky Avatar
  • cliffstamp Avatar
  • stellartux Avatar
  • induhiu Avatar
  • trashy_incel Avatar
  • ejini战神 Avatar
  • akar-0 Avatar
  • Kacarott Avatar
  • Just4FunCoder Avatar
  • saudiGuy Avatar
Ad