使用 While 和 Module 命令的 Mathematica、斐波那契数列

发布于 2024-10-29 12:43:35 字数 115 浏览 0 评论 0原文

我如何编写一个计算 f[n] 的程序(对于斐波那契数:f[n]=f[n]-f[n-2],其中 f[0] = 任何数字)使用 模块和一个While循环

How can i write a program that computes f[n] (for Fibonacci numbers:f[n]=f[n]-f[n-2], with f[0] = any number) using Module and a While loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说谎友 2024-11-05 12:43:35

家庭作业?我希望你通过例子学习。 ;-)

您的主题行说递归,但您没有在问题中指定这一点;相反,您可以指定 ModuleWhile。我会选择后者。

fib[n_] := 
  Module[{x = 1, y = 0, i = 0},
    While[i++ < n, {x, y} = {y, x + y}];
    y
  ]

Array[fib, 7]

(*  Out[]= {1, 1, 2, 3, 5, 8, 13}  *)

Table[fib[m], {m, 1,10}] 

(*  Out[]= {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}  *)

Homework? I hope you learn by example. ;-)

Your subject line says recursion, but you don't specify that in your question; rather, you specify Module and While. I'll go with the latter.

fib[n_] := 
  Module[{x = 1, y = 0, i = 0},
    While[i++ < n, {x, y} = {y, x + y}];
    y
  ]

Array[fib, 7]

(*  Out[]= {1, 1, 2, 3, 5, 8, 13}  *)

Table[fib[m], {m, 1,10}] 

(*  Out[]= {1, 1, 2, 3, 5, 8, 13, 21, 34, 55}  *)
指尖上得阳光 2024-11-05 12:43:35

如果您想给老师留下深刻印象,我会使用内存缓存方法。它比 Sjoerd 描述的方法要快得多。

考虑这个实现

fib[0]:=1
fib[1]:=1
fib[n_]:= (fib[n]=fib[n-1]+fib[n-2])

让我们比较两者,只是为了证明我的观点。

slowfib[0]:=1
slowfib[1]:=1
slowfib[n_]:=slowfib[n-1]+slowfib[n-2]

这是运行时间的比较:

Map[fib, Range[30]] // AbsoluteTiming

{0.000158, {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 
  987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 
  121393, 196418, 317811, 514229, 832040, 1346269}}

Map[slowfib, Range[30]] // AbsoluteTiming

{6.582185, {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 
987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 
121393, 196418, 317811, 514229, 832040, 1346269}}

运行时间要高得多,因为递归函数

fib[n_]:=fib[n-1]+fib[n-2]

生成 n^2 次递归调用(如果没有意义,请将其写在纸上)。另一方面,定义

fib[n_]:= fib[n]=fib[n-1]+fib[n-2]

利用内存缓存来计算项,这会导致运行时间显着加快,因为每次调用都会生成 fib[x] 的缓存值。

If you're trying to impress your instructor, I would use the memory cache approach. It is significantly faster than the approach Sjoerd is describing.

Consider this implementation

fib[0]:=1
fib[1]:=1
fib[n_]:= (fib[n]=fib[n-1]+fib[n-2])

Lets compare the two, just to prove my point.

slowfib[0]:=1
slowfib[1]:=1
slowfib[n_]:=slowfib[n-1]+slowfib[n-2]

Here's the comparison in runtimes:

Map[fib, Range[30]] // AbsoluteTiming

{0.000158, {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 
  987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 
  121393, 196418, 317811, 514229, 832040, 1346269}}

Map[slowfib, Range[30]] // AbsoluteTiming

{6.582185, {1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 
987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 
121393, 196418, 317811, 514229, 832040, 1346269}}

The runtime is so much higher because the recursive function

fib[n_]:=fib[n-1]+fib[n-2]

generates n^2 recursive calls (write it out on paper if that doesn't make sense). On the other hand, defining

fib[n_]:= fib[n]=fib[n-1]+fib[n-2]

takes advantage of memory caching to calculate the terms, which results in a drastically faster runtime, since each call generates a cached value for fib[x].

梅倚清风 2024-11-05 12:43:35

根据标题的第一部分,以下方法是如何做到这一点的示例:

fib[1] = 1; 
fib[2] = 1; 
fib[n_] := fib[n - 1] + fib[n - 2]

fib[3]
fib[7]

Out[11]= 2

Out[12]= 13

fib /@ Range[20]

Out[10]= {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 
610, 987, 1597, 2584, 4181,  6765}

Going by the first part of your title, the following approach would be an example of how to do that:

fib[1] = 1; 
fib[2] = 1; 
fib[n_] := fib[n - 1] + fib[n - 2]

fib[3]
fib[7]

Out[11]= 2

Out[12]= 13

fib /@ Range[20]

Out[10]= {1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 
610, 987, 1597, 2584, 4181,  6765}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文