Skip to content

Commit 29ef0f5

Browse files
author
Karan Goel
committed
Factorial done, README updated
1 parent 7dc0e08 commit 29ef0f5

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

Numbers/factorial.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Factorial Finder - The Factorial of a positive integer, n,
3+
is defined as the product of the sequence n, n-1, n-2, ...1
4+
and the factorial of zero, 0, is defined as being 1. Solve
5+
this using both loops and recursion.
6+
"""
7+
8+
def fact_loop(n):
9+
"""
10+
Returns the factorial of a given positive, non-zero integer
11+
using loops.
12+
"""
13+
fact = 1
14+
while n > 0:
15+
fact *= n
16+
n -= 1
17+
return fact
18+
19+
def fact_recursion(n):
20+
"""
21+
Returns the factorial of a given positive, non-zero integer
22+
using recursion.
23+
"""
24+
return 1 if n == 0 else n * fact_recursion(n - 1) # if user as ternary operator
25+
26+
if __name__ == '__main__':
27+
n = input('Enter a positive number: ')
28+
29+
if n >= 0:
30+
print 'Factorial of %d by loops is %d' % (n, fact_loop(n))
31+
print 'Factorial of %d by recursion is %d' % (n, fact_recursion(n))
32+
else:
33+
print 'Not a valid number'

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ Mega Project List
33

44
#### [RECOGNITION](https://github.com/thekarangoel/Projects/tree/master/RECOGNITION)
55

6-
This repo is in the top 5 on GitHub on [July 14 2013](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-2013-07-14.png). (And again on [July 22, 2013](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-2013-07-22%2013_10_30.png), and again on [July 23, 2013](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-2013-07-23.png).) And on [weekly](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-weekly-2013-07-22.png) [list](https://github.com/thekarangoel/Projects/blob/master/RECOGNITION/top5-weekly-2013-07-23.png) during the week of July 2013.
6+
Ever since this repo was created, it has been in the top list on GH. Be it the daily or weekly list! This repo is in the top 5 on GitHub on [July 14 2013](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-2013-07-14.png). (And again on [July 22, 2013](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-2013-07-22%2013_10_30.png), and again on [July 23, 2013](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-2013-07-23.png).) And on [weekly](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-weekly-2013-07-22.png) [list](https://github.com/thekarangoel/Projects/blob/master/RECOGNITION/top5-weekly-2013-07-23.png) during the week of July 2013. In the last week of July, *Projects* was in the monthly top list on GH.
77

8-
![July 22, 2013](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-2013-07-22%2013_10_30.png)
8+
![July 25, 2013](https://raw.github.com/thekarangoel/Projects/master/RECOGNITION/top5-monthly-2013-07-25.png)
99

1010
===============================
1111

12-
### [CONTRIBUTING] (https://github.com/thekarangoel/Projects/blob/master/CONTRIBUTING.md)
12+
### [CONTRIBUTING](https://github.com/thekarangoel/Projects/blob/master/CONTRIBUTING.md)
1313

1414
See ways of [contributing](https://github.com/thekarangoel/Projects/blob/master/CONTRIBUTING.md) to this repo. You can contribute solutions to existing problems, add new projects or remove existing ones. Make sure you follow all instructions properly.
1515

@@ -63,7 +63,7 @@ Numbers
6363

6464
**Dijkstra’s Algorithm** - Create a program that finds the shortest path through a graph using its edges.
6565

66-
**Factorial Finder** - The Factorial of a positive integer, n, is defined as the product of the sequence n, n-1, n-2, ...1 and the factorial of zero, 0, is defined as being 1. Solve this using both loops and recursion.
66+
[**Factorial Finder**](https://github.com/thekarangoel/Projects/blob/master/Numbers/factorial.py) - The Factorial of a positive integer, n, is defined as the product of the sequence n, n-1, n-2, ...1 and the factorial of zero, 0, is defined as being 1. Solve this using both loops and recursion.
6767

6868
**Complex Number Algebra** - Show addition, multiplication, negation, and inversion of complex numbers in separate functions. (Subtraction and division operations can be made with pairs of these operations.) Print the results for each operation tested.
6969

0 commit comments

Comments
 (0)