Heim > Web-Frontend > js-Tutorial > Hauptteil

Wie funktioniert die Rekursion in der Funktion „FindSequence'?

Susan Sarandon
Freigeben: 2024-10-18 07:55:03
Original
498 Leute haben es durchsucht

How Does Recursion Operate in the \

Delving into the Recursion of "FindSequence"

Eloquent Javascript introduces a challenging problem: writing a function that generates a sequence of additions and multiplications to reach a given goal number. The provided code employs recursion, which raises questions about its operation.

Within the findSequence function resides a nested function find. This function recursively explores two possible transformations from a current number: adding 5 or multiplying by 3. Each transformation produces a new number, and the process continues until either the goal is met or the current number exceeds the goal.

At each step, a textual representation of the expression is passed along. For instance, starting with the number 1, the initial expression is "1". If adding 5 is chosen, the expression becomes "(1 + 5)". By recording these expressions, the function tracks the steps taken to reach each number.

If the goal is reached, the expression from the final step represents a valid sequence. Otherwise, the function returns null, indicating failure. Multiple recursive calls are made, each exploring a different path until a valid sequence is found or all paths have been exhausted.

To illustrate the recursion, let's consider finding a sequence for the goal number 14.

(1, "1")
(5, "1 + 5")
(10, "(1 + 5) + 5")
(15, "((1 + 5) + 5) + 5")  // Discard, exceeds goal
(30, "((1 + 5) + 5) * 3")  // Discard, exceeds goal
(15, "(1 + 5) * 3")  // Discard, exceeds goal
(3, "1 * 3")
(8, "(1 * 3) + 5")
(13, "((1 * 3) + 5) + 5")
(18, "(((1 * 3) + 5) + 5) + 5")  // Discard, exceeds goal
(39, "(((1 * 3) + 5) + 5) * 3")  // Discard, exceeds goal
(24, "((1 * 3) + 5) * 3")  // Discard, exceeds goal
(9, "(1 * 3) * 3")
(14, "((1 * 3) * 3) + 5")  // Success!
Nach dem Login kopieren

Through this stepwise process of recursion, the function eventually finds the sequence: "((1 3) 3) + 5", which represents the addition of 5 to the product of 1 and 3 cubed.

Das obige ist der detaillierte Inhalt vonWie funktioniert die Rekursion in der Funktion „FindSequence'?. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!

Quelle:php
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!