← Projects

Updated pi.py, did fibonacci series

da57890ac285ca27692f91a49c72600b9c914366 · Karan Goel · Jul 5, 2013 09:34

+16 −1 · 2 files changed

  • Numbers/fibonacci.py +15 −0
  • Numbers/pi.py +1 −1
diff --git a/Numbers/fibonacci.py b/Numbers/fibonacci.py
new file mode 100644
index 0000000..4e20a55
--- /dev/null
+++ b/Numbers/fibonacci.py
@@ -0,0 +1,15 @@
+# -*- coding: cp1252 -*-
+# Fibonacci Sequence  Enter a number and have the
+# program generate the Fibonacci sequence to that number
+# or to the Nth number
+
+n = int(raw_input('How many numbers do you need? '))
+series = [1]
+
+while len(series) < n:
+    if len(series) == 1:
+        series.append(1)
+    else:
+        series.append(series[-1] + series[-2])
+
+print series
diff --git a/Numbers/pi.py b/Numbers/pi.py
index 198a194..2aff429 100644
--- a/Numbers/pi.py
+++ b/Numbers/pi.py
@@ -4,4 +4,4 @@ import math
 
 digits = raw_input('Enter number of digits to round PI to: ')
 
-print round(math.pi, int(digits))
+print ('{0:.%df}' % min(20, int(digits))).format(math.pi) # nested string formatting