Hackerrank “30 Days of Code” Çözümleri – Day 9: Recursion 3

Hackerrank’in 30 Days of Code içerisinde bulunan “Day 9: Recursion 3” sorusunun açıklaması ve çözümü. Bu soruda rekürsif fonksiyonlara giriş yapıyoruz. Derler ki; “Rekürsif fonksiyonları anlamak için önce rekürsif fonksiyonları anlamanız gerekir“.

► Hackerrank 30 Days of Code Çözümleri – Day 9: Recursion 3: https://www.hackerrank.com/challenges/30-recursion/problem

► Problem açıklaması:

Objective

Today, we are learning about an algorithmic concept called recursion. Check out the Tutorial tab for learning materials and an instructional video.

Recursive Method for Calculating Factorial Function Description Complete the factorial function in the editor below. Be sure to use recursion.

factorial has the following paramter:

int n: an integer

Returns

int: the factorial of n

Note: If you fail to use recursion or fail to name your recursive function factorial or Factorial, you will get a score of 0.

Input Format

A single integer, n (the argument to pass to factorial).

Constraints

– 2 lessEqual n lessEqual 12

– Your submission must contain a recursive function named factorial.

Sample Input

3

Sample Output

6

Explanation

Consider the following steps. After the recursive calls from step 1 to 3, results are accumulated from step 3 to 1.

factorial(3) = 3 x factorial(2) = 3 x 2 = 6

factorial(2) = 2 x factorial(1) = 2 x 1 = 2

factorial(1) = 1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.