Skip to content

Commit 0d0c63b

Browse files
committed
Add factorial of n problem
1 parent 495c324 commit 0d0c63b

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

factorial/README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
![](http://i.imgur.com/ajyNlBd.png)
53+
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package solutions
2+
3+
import java.io.BufferedReader;
4+
import java.io.InputStreamReader;
5+
import java.math.BigInteger;
6+
7+
8+
public class FactorialSolution {
9+
10+
public static void main(String[] args) throws Exception {
11+
// read STDIN
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
BigInteger n = BigInteger.valueOf(Integer.parseInt(br.readLine()));
14+
// write STDOUT
15+
System.out.println(factorial(n, BigInteger.ONE));
16+
}
17+
18+
/*
19+
* Factorial of n
20+
* @param n input
21+
* @param acc accumulator (tail call optimization)
22+
* @return n!
23+
*/
24+
public static BigInteger factorial(BigInteger n, BigInteger acc) {
25+
26+
if (n.equals(BigInteger.ZERO))
27+
return acc;
28+
else
29+
return factorial(n.subtract(BigInteger.ONE), n.multiply(acc));
30+
}
31+
}
32+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package solutions
2+
3+
object FactorialSolution {
4+
5+
def main(args: Array[String]) = {
6+
val input = io.Source.stdin.bufferedReader.readLine()
7+
println(factorial(input.toInt))
8+
}
9+
10+
def factorial(n: Int): BigInt = {
11+
12+
def calc(acc: BigInt, n: Int): BigInt =
13+
if (n == 0) acc
14+
else calc(acc * n, n - 1)
15+
16+
calc(1, n)
17+
}
18+
19+
}

0 commit comments

Comments
 (0)