Fibonacci序列由递归关系定义:
F n = F n-1 + F n-2,其中F 1 = 1且F 2 = 1.因此前12项将是F 1 = 1,F 2 = 1,F 3 = 2,F 4 = 3 ,F 5 = 5,F 6 = 8,F 7 = 13,F 8 = 21,F 9 = 34,F 10 = 55,F 11 = 89,F 12 = 144
第12个学期F 12是第一个包含三位数的术语.
Fibonacci序列中包含1000位数的第一项是什么?
我在Python中制作了一个强力解决方案,但计算实际解决方案绝对是永远的.任何人都可以提出非暴力解决方案吗?
def Fibonacci(NthTerm):
if NthTerm == 1 or NthTerm == 2:
return 1 # Challenge defines 1st and 2nd term …Run Code Online (Sandbox Code Playgroud) 我编写了以下递归函数,但由于最大递归深度而导致运行时错误.我想知道是否有可能编写一个迭代函数来克服这个问题:
def finaldistance(n):
if n%2 == 0:
return 1 + finaldistance(n//2)
elif n != 1:
a = finaldistance(n-1)+1
b = distance(n)
return min(a,b)
else:
return 0
Run Code Online (Sandbox Code Playgroud)
我试过的是这个,但似乎没有用,
def finaldistance(n, acc):
while n > 1:
if n%2 == 0:
(n, acc) = (n//2, acc+1)
else:
a = finaldistance(n-1, acc) + 1
b = distance(n)
if a < b:
(n, acc) = (n-1, acc+1)
else:
(n, acc) =(1, acc + distance(n))
return acc
Run Code Online (Sandbox Code Playgroud) 有落实经典龙格-库塔方案Python中显示的两种方式在这里.第一个使用lambda函数,第二个没有它们.
哪一个会更快,为什么呢?
几天前有人对我说,递归会比迭代更好,如果可能的话,应该总是使用.
所以,我进入递归并尝试编写一个简单的程序来获得一个数字的阶乘.这是递归:
def fact(n):
if n == 1:
return 1
return n * fact(n - 1)
Run Code Online (Sandbox Code Playgroud)
虽然这样可以正常工作,但RuntimeError: maximum recursion depth exceeded只要n达到997以上就会得到.
所以我写了一个简单的函数,完全相同,但有一个for loop.
def fact(n):
a = 1
for x in range (0, n, 1):
a = a * (n - x)
return a
Run Code Online (Sandbox Code Playgroud)
而n < 10000它在150毫秒内给出答案.
所以,我虽然可能递归速度较快,但数字较少,但不是.这需要更长时间:

所以我的问题是:
在Python中编写程序时是否有任何理由使用递归?
并且:
有任何问题只能通过递归来解决吗?
我正在学习Python(使用3.6.2),在我上一堂课上,他们让我做一些我需要进行无限for循环的事情.出于某种原因,老师不希望我们while用于整个练习.这是它变得复杂的地方......
所以,我一直在寻找一种方法来做到这一点.但是,这也很困难,因为老师不希望我们使用我们在课堂上没有看到的任何命令.所以我不能使用.append,sys函数,好吧,我甚至不能使用休息.我必须找到一种方法来处理"简单"命令.
我以为我可以这样做;
x=1
for i in range(x):
do_something()
x += 1
Run Code Online (Sandbox Code Playgroud)
但是,它似乎没有用.我认为那是因为Python没有再次读取该范围的值?
我找不到办法,但经过几个小时的思考后,我发现自己可以使用一个小的解决方法:
def ex():
print("Welcome")
for i in range(1):
math = int(input("Please insert grades obtained at Math, or insert 666 to exit" ))
if(math > 0 and math < 60):
print("Sorry. You failed the test")
return ex():
elif(math >= 60 and math <= 100):
print("Congratulations. You passed the test")
return ex():
elif(math == 666): …Run Code Online (Sandbox Code Playgroud) 我正在做的任务是:
给定一组int,递归计算值11出现在数组中的次数.我们将使用仅考虑从给定索引开始的数组部分的约定.通过这种方式,递归调用可以传递索引+ 1以向下移动数组.
我需要递归地做这件事.我对此很陌生,但我在技术上使它成功了.我有以下内容:
def array11(arr, index, cnt=0, num=0):
if(cnt==len(arr)-index):
print("yay!!! number 11 appears %d times"%num)
return
elif(arr[index:][cnt]==11):
num+=1
cnt+=1
array11(arr,index,cnt,num)
else:
cnt+=1
array11(arr,index,cnt,num)
Run Code Online (Sandbox Code Playgroud)
但我觉得我通过添加默认值的"cnt"和"num"参数,出于某种原因便宜了.我只是不知道如何在没有计数器的情况下通过"arr"阵列!
所以这可以接受吗?你会以同样的方式做到吗?
提前致谢
我有以下代码用于计算python中数字的阶乘.但我无法理解为什么我得到的答案是1.可以有人纠正我的代码.我想在不使用递归的情况下计算阶乘.
def factorial (n):
result =1
num = n
while n<1:
result = result * num
num = num -1
return result
factorial(5)
1
Run Code Online (Sandbox Code Playgroud) 我经常看到类似于以下代码的代码(类似于aiohttp文档中的示例).
@asyncio.coroutine
def init(loop):
srv = yield from loop.create_server(web.Application().make_handler(), '0.0.0.0', 8080)
return srv
Run Code Online (Sandbox Code Playgroud)
在下面的1行中有没有任何优点/缺点,假设你不想srv在获取它和返回之间对对象做任何事情?
@asyncio.coroutine
def init(loop):
return (yield from loop.create_server(web.Application().make_handler(), '0.0.0.0', 8080))
Run Code Online (Sandbox Code Playgroud) 如何将列表["one","two","three","four"]变成类似于列表{"one": {"two": {"three":{"four"}}}}中的每个项目都是字典中其他元素的后代的内容?我认为它可以在递归函数中完成,但我不确定如何.
这是我试过的:
l = ["one","two","three","four"]
d = {}
for v in l[:-1]:
d[v] = {l}
d = d[v]
print(d)
Run Code Online (Sandbox Code Playgroud)
谢谢!
为什么java.util.Arrays中的binarySearch()方法使用循环而不是使用递归来实现?
我是python的新手,我试图理解如何将Python与C/C++联系起来.考虑问题:检查给定的链接列表是否是回文.从这个来源,我发现了一个非常有效的解决方案:
// Initial parameters to this function are &head and head
bool isPalindromeUtil(struct node **left, struct node *right)
{
/* stop recursion when right becomes NULL */
if (right == NULL)
return true;
/* If sub-list is not palindrome then no need to
check for current left and right, return false */
bool isp = isPalindromeUtil(left, right->next);
if (isp == false)
return false;
/* Check values at current left and right */
bool isp1 = (right->data == (*left)->data); …Run Code Online (Sandbox Code Playgroud) python ×10
python-3.x ×3
recursion ×3
python-2.7 ×2
algorithm ×1
brute-force ×1
c ×1
fibonacci ×1
for-loop ×1
java ×1
loops ×1
optimization ×1
performance ×1
pointers ×1
runge-kutta ×1