|
| 1 | +**n factorial** (written as **n!**) is the number we get when we multiply every positive number from 1 up to n together. In this problem, *your code* will do the grunt work of computing the factorial. |
| 2 | + |
| 3 | +# Problem Statement |
| 4 | + |
| 5 | +Write a program that, given a number `n` from STDIN, prints out the factorial of `n` to STDOUT: |
| 6 | + |
| 7 | +* If `n` is `0`, `n` factorial is `1` |
| 8 | +* `n!` is not defined for negative numbers. |
| 9 | + |
| 10 | +Example 1: |
| 11 | + |
| 12 | +``` |
| 13 | +3! = 3 × 2 × 1 = 6 |
| 14 | +``` |
| 15 | + |
| 16 | +So given the input **`3`**, your program should output: |
| 17 | + |
| 18 | +``` |
| 19 | +6 |
| 20 | +``` |
| 21 | + |
| 22 | +Example 2: |
| 23 | + |
| 24 | +``` |
| 25 | +7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040 |
| 26 | +``` |
| 27 | + |
| 28 | +So given the input **`7`**, your program should output: |
| 29 | + |
| 30 | +``` |
| 31 | +5040 |
| 32 | +``` |
| 33 | + |
| 34 | +# What are factorials good for? |
| 35 | + |
| 36 | +Factorials can be used to calculate the number of permutations of a given set. Think—if there are 3 letters—**A, B, and C**, they can be arranged as: **ABC, ACB, BAC, BCA, CAB, or CBA**. That's *6* options because A can be put in 3 different slots, B has 2 choices left after A is placed, and C has only one option left after A and B have been placed. That is 3×2×1 = 6 choices. |
| 37 | + |
| 38 | +More generally, if there are three objects, and we want to find out how many different ways there are to arrange (or select them), for the first object, there are 3 choices, for the second object, there are only two choices left as the first object has already been chosen, and finally, for the third object, there is only one position left. |
| 39 | + |
| 40 | +So using what we know about factorials, we know that there are 6 options for arranging 3 items. **`3!`** is equivalent to 3×2×1, or 6. |
| 41 | + |
| 42 | +To dig a bit deeper in to factorials and their role in permutations and combinations (which occasionally come up in interviews), check out [this article and its practice problems](http://www.wyzant.com/resources/lessons/math/precalculus/factorials_permutations_and_combinations). |
| 43 | + |
| 44 | +# Hints |
| 45 | + |
| 46 | +* The factorial function grows very fast. There are **3,628,800** ways to arrange **10** items. |
| 47 | + |
| 48 | +* This problem can be solved in an [imperative style](http://en.wikipedia.org/wiki/Functional_programming#Comparison_to_imperative_programming) using loops—or in a functional style, using recursion. If you write a recursive solution, its worth reading up on [tail-call optimization](http://en.wikipedia.org/wiki/Tail_call) or watching the Coding for Interviews course lecture on recursion before if your solution's stack level goes too deep |
| 49 | + |
| 50 | +* For help on reading from STDIN, see the [HackerRank environment help page](https://www.hackerrank.com/environment) under the "Sample Problem Statement" section. |
| 51 | + |
| 52 | + |
| 53 | + |
0 commit comments