Warning: include(): open_basedir restriction in effect. File(/home/matlabsite/public_html/wp-content/plugins/wp-super-cache/wp-cache-base.php) is not within the allowed path(s): (/home/h321874/:/tmp:/var/tmp:/opt/alt/php81/usr/share/pear/:/dev/urandom:/usr/local/lib/php/:/usr/local/php81/lib/php/) in /home/h321874/domains/matlabsite.com/public_html/wp-content/plugins/wp-super-cache/wp-cache.php on line 95

Warning: include(/home/matlabsite/public_html/wp-content/plugins/wp-super-cache/wp-cache-base.php): failed to open stream: Operation not permitted in /home/h321874/domains/matlabsite.com/public_html/wp-content/plugins/wp-super-cache/wp-cache.php on line 95

Warning: include(): open_basedir restriction in effect. File(/home/matlabsite/public_html/wp-content/plugins/wp-super-cache/wp-cache-base.php) is not within the allowed path(s): (/home/h321874/:/tmp:/var/tmp:/opt/alt/php81/usr/share/pear/:/dev/urandom:/usr/local/lib/php/:/usr/local/php81/lib/php/) in /home/h321874/domains/matlabsite.com/public_html/wp-content/plugins/wp-super-cache/wp-cache.php on line 95

Warning: include(/home/matlabsite/public_html/wp-content/plugins/wp-super-cache/wp-cache-base.php): failed to open stream: Operation not permitted in /home/h321874/domains/matlabsite.com/public_html/wp-content/plugins/wp-super-cache/wp-cache.php on line 95

Warning: include(): Failed opening '/home/matlabsite/public_html/wp-content/plugins/wp-super-cache/wp-cache-base.php' for inclusion (include_path='.:/opt/alt/php72/usr/share/pear') in /home/h321874/domains/matlabsite.com/public_html/wp-content/plugins/wp-super-cache/wp-cache.php on line 95

Warning: include_once(): open_basedir restriction in effect. File(/home/matlabsite/public_html/wp-content/plugins/wp-super-cache/ossdl-cdn.php) is not within the allowed path(s): (/home/h321874/:/tmp:/var/tmp:/opt/alt/php81/usr/share/pear/:/dev/urandom:/usr/local/lib/php/:/usr/local/php81/lib/php/) in /home/h321874/domains/matlabsite.com/public_html/wp-content/plugins/wp-super-cache/wp-cache.php on line 122

Warning: include_once(/home/matlabsite/public_html/wp-content/plugins/wp-super-cache/ossdl-cdn.php): failed to open stream: Operation not permitted in /home/h321874/domains/matlabsite.com/public_html/wp-content/plugins/wp-super-cache/wp-cache.php on line 122

Warning: include_once(): Failed opening '/home/matlabsite/public_html/wp-content/plugins/wp-super-cache/ossdl-cdn.php' for inclusion (include_path='.:/opt/alt/php72/usr/share/pear') in /home/h321874/domains/matlabsite.com/public_html/wp-content/plugins/wp-super-cache/wp-cache.php on line 122
برنامه محاسبه اعداد فیبوناچی در پایتون — راهنمای کاربردی | مرجع متلب و هوش مصنوعی.

برنامه محاسبه اعداد فیبوناچی در پایتون — راهنمای کاربردی

در این مطلب، روش نوشتن برنامه محاسبه اعداد فیبوناچی در پایتون مورد بررسی قرار گرفته است. اعداد فیبوناچی، اعداد موجود در سری اعداد صحیج زیر هستند.

۰, ۱, ۱, ۲, ۳, ۵, ۸, ۱۳, ۲۱, ۳۴, ۵۵, ۸۹, ۱۴۴, ……..

به بیان ریاضی، توالی Fn از اعداد فیبوناچی، با یک رابطه بازگشتی به صورت زیر تعریف می‌شود.

Fn = Fn-1 + Fn-2

در این سری، مقادیر دانه اولیه، به صورت زیر هستند.

F0 = 0 و F1 = 1

برنامه محاسبه اعداد فیبوناچی در پایتون به صورت بازگشتی

# Function for nth Fibonacci number 
  
def Fibonacci(n): 
    if n<0: 
        print("Incorrect input") 
    # First Fibonacci number is 0 
    elif n==1: 
        return 0
    # Second Fibonacci number is 1 
    elif n==2: 
        return 1
    else: 
        return Fibonacci(n-1)+Fibonacci(n-2) 
  
# Driver Program 
  
print(Fibonacci(9)) 
  
#This code is contributed by Saket Modi

خروجی قطعه کد بالا، به صورت زیر است.

۲۱

برنامه محاسبه اعداد فیبوناچی در پایتون با استفاده از برنامه‌نویسی پویا

# Function for nth fibonacci number - Dynamic Programing 
# Taking 1st two fibonacci nubers as 0 and 1 
  
FibArray = [0,1] 
  
def fibonacci(n): 
    if n<0: 
        print("Incorrect input") 
    elif n<=len(FibArray): 
        return FibArray[n-1] 
    else: 
        temp_fib = fibonacci(n-1)+fibonacci(n-2) 
        FibArray.append(temp_fib) 
        return temp_fib 
  
# Driver Program 
  
print(fibonacci(9)) 
  
#This code is contributed by Saket Modi

خروجی قطعه کد بالا، به صورت زیر است.

۲۱

برنامه محاسبه اعداد فیبوناچی در پایتون با بهینه‌سازی فضایی

# Function for nth fibonacci number - Space Optimisataion 
# Taking 1st two fibonacci numbers as 0 and 1 
  
def fibonacci(n): 
    a = 0
    b = 1
    if n < 0: 
        print("Incorrect input") 
    elif n == 0: 
        return a 
    elif n == 1: 
        return b 
    else: 
        for i in range(2,n): 
            c = a + b 
            a = b 
            b = c 
        return b 
  
# Driver Program 
  
print(fibonacci(9)) 
  
#This code is contributed by Saket Modi

خروجی قطعه کد بالا، به صورت زیر است.

۲۱

اگر نوشته بالا برای شما مفید بوده است، آموزش‌های زیر نیز به شما پیشنهاد می‌شوند:

منبع [+]

پاسخی بگذارید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *