Deret Fibonacci python

menghitung deret fibonacci menggunakan python

#!/usr/bin/env python
import os, sys
if sys.platform == 'linux-i386' or sys.platform == 'linux2':
hapus = "clear"
if sys.platform == 'win32':
hapus = "cls"
print "deret fibonacci in python"
jumlah = raw_input("masukkan jumlah anggota deret yang diinginkan: ")
a=int(jumlah)
if a == 0:
print "anda salah memasukkan angka!!"
elif a == 1:
print "-----------deret fibonaccinya adalah-------------------"
print "[0]"
print "-------------------------------------------------------"
elif a == 2:
print "-----------deret fibonaccinya adalah-------------------"
print "[0,1]"
print "-------------------------------------------------------"
else:
fibonacci = [0,1]
#x = int(a)
q = (a - 1)
for i in range(1,q):
x = int(fibonacci[-1])
y = int(fibonacci[-2])
z = x+y
fibonacci.append(z)
os.system(hapus)
print "-----------deret fibonaccinya adalah-------------------"
print fibonacci
print "-------------------------------------------------------"

Advertisements
  1. No trackbacks yet.

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

%d bloggers like this: