计算斐波那契(Fibonacci)
发布时间:2020-12-17 04:11:43 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 # Calculate the Fibonacci to 'x' number of places and return it as an arraydef Fibonacci(limit) # Creates a new array array = Array.new # Ou
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 # Calculate the Fibonacci to 'x' number of places and return it as an array def Fibonacci(limit) # Creates a new array array = Array.new # Our first number num1 = 1 # Our second number num2 = 1 # Our next number nextNum = 0 # Loop through until we reach our limit # NOTE: we need to subtract 2 because we will add two numbers to the beginning later while nextNum < (limit - 2) # Our third number will be made by adding our first and second number together num3 = num1 + num2; # Our new first number is our old second number num1 = num2; # Our new second number is our old third number num2 = num3; # Insert our new number into our array array.insert(nextNum,num3) # This will be our next number nextNum += 1 # You can also use: nextNum = nextNum.next # Exit the 'while' loop end # Insert the number 1 into the beginning of our array array.insert(0,1) # Insert the number 1 into the 2nd position of our array array.insert(1,1) # Return our array return array # Exit the method end 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |