博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Recursive
阅读量:7094 次
发布时间:2019-06-28

本文共 1967 字,大约阅读时间需要 6 分钟。

Calculating Factorial
"If the integer number is less than zero, reject it. If the number is zero or one, its factorial is one. If the number is larger than one, multiply it by the factorial of the next smaller number."

In other words: Fact(n) : = n Fact(n-1) if n > 1 otherwise Fact(n) := 1.

function Fact(inbr:integer):integer; begin   if inbr < 1 then Result := 1   else Result := inbr * Fact(inbr-1) ; end;
 
Greatest common divisor
In mathematics, GCD or greatest common divisor is defined as the largest number that divides both of two given integer numbers. Around 2,000 years ago, Euclid gave the following method of computing the GCD of two integers, a and b:

If b divides a, then the GCD is a. Otherwise GCD(a,b):=GCD(b, a mod b)

Where the mod function gives the reminder after integer division.

function GCD(a,b : integer):integer; begin   if (b mod a) = 0 then Result := a   else Result := GCD(b, a mod b) ;
 
Exponents m^n
The problem is: how to calculate mN if N is some positive integer number.
function iPow(base, exp: integer): integer; begin   if exp = 0 then Result := 1   else Result := base * iPow(base, exp - 1) ; end;
 

Mutual recursions

You know that in Delphi we can't use an identifier until we have declared it. In other words, Delphi doesn't let one routine call another routine that is defined after it. To make such mutual recursion possible, we need to declare a routine as a forward declaration. To make a forward declaration, add the forward reserved word at the end of the header of the subroutine. Then place the header with the extra forward declaration before the code for the subroutine that will call it. Here's an example of using a forward declaration:

procedure MyProc1(dummy : integer) ; forward;  procedure MyProc2; begin    MyProc1(5) ; end;  procedure MyProc1(dummy : integer) ; var   j:integer; begin   for j:= 1 to dummy do    ShowMessage(IntToStr(j)) ; end;

转载于:https://www.cnblogs.com/xxd0825/archive/2012/12/17/2822065.html

你可能感兴趣的文章
ASP.NET调用V3版本的Google Maps API
查看>>
苹果面试8大难题及答案
查看>>
运行 tomcat 错误 : java.lang.Exception: Socket bind failed: [730048] 解决方法:
查看>>
并行接口和串行接口
查看>>
Adding a nested ESXi on 5.1
查看>>
检查管理员exception is java.sql.SQLException: 无效的列索引
查看>>
类型函数C语言void关键字
查看>>
数字频率计的verilog实现
查看>>
使用递归算法结合数据库解析成java树形结构
查看>>
SqlServer和Oracle中一些常用的sql语句6 存储过程
查看>>
喝咖啡写脚本,顺便再加一点点CSS语法糖 2.五分钟学会Less
查看>>
编辑简单的 shell程序
查看>>
fileupload的乱码解决
查看>>
php 裁剪图片类
查看>>
如何设置Jquery UI Menu 菜单为横向展示
查看>>
mini2440裸机之I2C
查看>>
async、await
查看>>
iScroll4插件的使用实例
查看>>
Android 利用ViewPager、Fragment、PagerTabStrip实现多页面滑动效果
查看>>
SMARTFORM报表程序设计(1)
查看>>