Hackerrank Day 9: Recursion

Objective
Today, we’re learning and practicing an algorithmic concept called Recursion. Check out the Tutorial tab for learning materials and an instructional video!

Recursive Method for Calculating Factorial

screen-shot-2016-12-17-at-12-44-52-am

Task
Write a factorial function that takes a positive integer, as a parameter and prints the result of ( factorial).

Input Format

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

Output Format

Print a single integer denoting N!.

Sample Input

3

Sample Output

6

Explanation: 

  1. factorial(3) = 3 x factorial(2)
  2. factorial(2) = 2 x factorial(1)
  3. factorial(1) = 1

My solution:

Recursion is basically defining a base case and applying that base case to the function again to find results for other cases.

This example was easy because the explanation basically tells you how to code it, but once again, I struggled with reading and printing input in Hacker Rank..

Don’t forget to explicitly tell the code to print out the result of the function at the end!

screen-shot-2016-12-17-at-12-39-03-am

Advertisements
Hackerrank Day 9: Recursion

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s