惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
雷峰网
雷峰网
V
V2EX
博客园_首页
Engineering at Meta
Engineering at Meta
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
GbyAI
GbyAI
H
Help Net Security
A
About on SuperTechFans
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
D
Docker
Security Archives - TechRepublic
Security Archives - TechRepublic
Help Net Security
Help Net Security
N
News and Events Feed by Topic
Simon Willison's Weblog
Simon Willison's Weblog
G
Google Developers Blog
A
Arctic Wolf
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Google DeepMind News
Google DeepMind News
博客园 - 三生石上(FineUI控件)
aimingoo的专栏
aimingoo的专栏
Hacker News: Ask HN
Hacker News: Ask HN
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
博客园 - 司徒正美
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Privacy International News Feed
T
Troy Hunt's Blog
T
Tenable Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Recorded Future
Recorded Future
F
Fortinet All Blogs
D
DataBreaches.Net
B
Blog
T
Threat Research - Cisco Blogs
MyScale Blog
MyScale Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
The GitHub Blog
The GitHub Blog
Security Latest
Security Latest
M
MIT News - Artificial intelligence

博客园 - 大口仔

网上邻居访问提示"未授予用户在此计算机上的请求登录类型"的解决 获取保存在路由器中的ADSL账号和密码 GX DM800遥控TV设置 硅钢片铁芯、坡莫合金、非晶及纳米晶软磁合金 在HTML中显示回车和空格 - 大口仔 - 博客园 SQL Server和Oracle的常用函数对比 自动配置IE代理脚本 - 大口仔 - 博客园 DD-WRT 上 VPN 的设置 - 大口仔 数据绑定以及Container.DataItem - 大口仔 - 博客园 css文本 jQuery插件 用户控件路径的解决方案 - 大口仔 - 博客园 CSS属性大全 关闭excel进程 c# 枚举基础 与 枚举属性的访问 首字母大写 SQL Server 2005之PIVOT/UNPIVOT行列转换 Excel导出方法总结
n!和 Fibnoacci函数的递归与非递归
大口仔 · 2009-07-03 · via 博客园 - 大口仔

求N的阶乘,对于阶乘,一般的递归运算的函数可以为

long factorial(int n){
    
if(n <= 1)
        
return 1;
    
else 
        
return n * factorial(n-1);
}

而非递归的运算函数可以表示为

long factorial( int n )
{
    
int    result = 1;

    
while( n > 1 ){
        result 
*= n;
        n 
-= 1;
    }


    
return result;
}

菲波那锲数列的定义都知道吧,是这样子的
它的递归函数都会写

long
fibonacci( 
int n )
{
   
if( n <= 2 )
      
return 1;
   
else
      
return fibonacci( n - 1 ) + fibonacci( n - 2 );
}

但是它的递归函数实在效率太低,重复计算的值相当的多,所以我们需要改进,使用循环

long
fibonacci( int n )
{
    long    next_older_result = 0;
    long    previous_result = 1;
    long    result = 1;

    if( 1 == n)
        return 0;
    else if( 2 == n)
        return 1;
    int i = 2;
    while(i < n){
        i++;
        next_older_result = previous_result;
        previous_result = result;
        result = previous_result + next_older_result;
    }
    return result;
}

如果你仔细观察一下,你会发现以上两个的递归调用都是函数所执行的最后一项任务。这个函数是尾部递归
尾部递归很容易也成非递归的形式。