我們可以增加遞迴函數的參數,讓函數在遞迴的時候帶走更多資訊。像是費氏數列,就可以讓遞迴函數多加兩個參數,來儲存每次遞迴時第#{{ n - 1 }}#項的值和第#{{ n }}#項的值。如此一來就可以在每次遞迴時計算出下一項的值,並將這個值又作為下一次遞迴呼叫時的第#{{ n }}#項的值,同時也將原本第#{{ n }}#項的值作為下一次第#{{ n - 1 }}#項的值。而原本的參數n就會變成計次用途了。
static int fibonacciRecursively(final int counter, final int init, final int accumulator) {
if (counter == 0) {
return init;
} else {
return fibonacciRecursively(counter - 1, accumulator, init + accumulator);
}
}
public static int fibonacci(final int n) {
return fibonacciRecursively(n, 0, 1);
}
static int fibonacciRecursively(final int counter, final int init, final int accumulator) {
while (true) {
if (counter == 0) {
return init;
} else {
return fibonacciRecursively(counter - 1, accumulator, init + accumulator);
}
break;
}
}
public static int fibonacci(final int n) {
return fibonacciRecursively(n, 0, 1);
}
pub fn fibonacci(n: u32) -> u32 {
let mut init = 0;
let mut accumulator = 1;
for _ in 0..n {
let temp = accumulator;
accumulator += init;
init = temp;
}
init
}
public static int fibonacci(final int n) {
int init = 0;
int accumulator = 1;
for (int counter = 0; counter < n; ++counter) {
final int temp = accumulator;
accumulator += init;
init = temp;
}
return init;
}
function fibonacci(n: number) {
let init = 0;
let accumulator = 1;
for (let counter = 0;counter < n;counter++) {
const temp = accumulator;
accumulator += init;
init = temp;
}
return init;
}
我們可以將階乘的第#{{ n }}#項移出,使得#{{n! = n \times (n - 1)!}}#。如何?明顯又是個遞迴函數了吧!
由於階乘函數比費氏數列函數還要簡單,所以直接看以下程式碼吧!
pub fn factorial_1(n: u32) -> u32 {
if n <= 1 {
1
} else {
n * factorial_1(n - 1)
}
}
fn factorial_2_recursively(n: u32, accumulator: u32) -> u32 {
if n <= 1 {
accumulator
} else {
factorial_2_recursively(n - 1, n * accumulator)
}
}
pub fn factorial_2(n: u32) -> u32 {
factorial_2_recursively(n, 1)
}
fn factorial_3_recursively(n: u32, accumulator: u32) -> u32 {
loop {
if n <= 1 {
return accumulator;
} else {
return factorial_3_recursively(n - 1, n * accumulator);
}
break;
}
}
pub fn factorial_3(n: u32) -> u32 {
factorial_3_recursively(n, 1)
}
fn factorial_4_inner(mut n: u32, mut accumulator: u32) -> u32 {
loop {
if n <= 1 {
return accumulator;
} else {
accumulator = n * accumulator;
n -= 1;
continue;
}
break;
}
}
pub fn factorial_4(n: u32) -> u32 {
factorial_4_inner(n, 1)
}
pub fn factorial_5(n: u32) -> u32 {
let mut accumulator = 1;
for i in 2..=n {
accumulator *= i;
}
accumulator
}
public static int factorial1(final int n) {
if (n <= 1) {
return 1;
} else {
return n * factorial1(n - 1);
}
}
static int factorial2Recursively(final int n, final int accumulator) {
if (n <= 1) {
return accumulator;
} else {
return factorial2Recursively(n - 1, n * accumulator);
}
}
public static int factorial2(final int n) {
return factorial2Recursively(n, 1);
}
static int factorial3Recursively(final int n, final int accumulator) {
while (true) {
if (n <= 1) {
return accumulator;
} else {
return factorial3Recursively(n - 1, n * accumulator);
}
break;
}
}
public static int factorial3(final int n) {
return factorial3Recursively(n, 1);
}
static int factorial4Inner(int n, int accumulator) {
while (true) {
if (n <= 1) {
return accumulator;
} else {
accumulator *= n;
--n;
continue;
}
break;
}
}
public static int factorial4(final int n) {
return factorial4Inner(n, 1);
}
public static int factorial5(final int n) {
int accumulator = 1;
for (int i = 2; i <= n; ++i) {
accumulator *= i;
}
return accumulator;
}
function factorial1(n: number) {
if (n <= 1) {
return 1;
} else {
return n * factorial1(n - 1);
}
}
function factorial2Recursively(n: number, accumulator: number) {
if (n <= 1) {
return accumulator;
} else {
return factorial2Recursively(n - 1, n * accumulator);
}
}
function factorial2(n: number) {
return factorial2Recursively(n, 1);
}
function factorial3Recursively(n: number, accumulator: number) {
for (; ;) {
if (n <= 1) {
return accumulator;
} else {
return factorial3Recursively(n - 1, n * accumulator);
}
break;
}
}
function factorial3(n: number) {
return factorial3Recursively(n, 1);
}
function factorial4Inner(n: number, accumulator: number) {
for (; ;) {
if (n <= 1) {
return accumulator;
} else {
accumulator *= n;
--n;
continue;
}
break;
}
}
function factorial4(n: number) {
return factorial4Inner(n, 1);
}
function factorial5(n: number) {
let accumulator = 1;
for (let i = 2;i <= n;i++) {
accumulator *= i;
}
return accumulator;
}
func Factorial1(n int) int {
if n <= 1 {
return 1
} else {
return n * Factorial1(n-1)
}
}
func factorial2Recursively(n int, accumulator int) int {
if n <= 1 {
return accumulator
} else {
return factorial2Recursively(n-1, n*accumulator)
}
}
func Factorial2(n int) int {
return factorial2Recursively(n, 1)
}
func factorial3Recursively(n int, accumulator int) int {
for {
if n <= 1 {
return accumulator
} else {
return factorial3Recursively(n-1, n*accumulator)
}
break
}
}
func Factorial3(n int) int {
return factorial3Recursively(n, 1)
}
func factorial4Inner(n int, accumulator int) int {
for {
if n <= 1 {
return accumulator
} else {
accumulator *= n
n--
continue
}
break
}
}
func Factorial4(n int) int {
return factorial4Inner(n, 1)
}
func Factorial5(n int) int {
accumulator := 1
for i := 2; i <= n; i++ {
accumulator *= i
}
return accumulator
}
pub fn binary_search_1(array: &[i32], target_element: i32, start: usize, end: usize) -> Option<usize> {
let middle = (end + start) / 2;
let element = array[middle];
if element == target_element {
Some(middle)
} else if end == start {
None
} else {
if target_element > element {
binary_search_1(array, target_element, middle + 1, end)
} else {
if middle > 0 {
binary_search_1(array, target_element, start, middle - 1)
} else {
None
}
}
}
}
pub fn binary_search_3(array: &[i32], target_element: i32, start: usize, end: usize) -> Option<usize> {
loop {
let middle = (end + start) / 2;
let element = array[middle];
if element == target_element {
Some(middle)
} else if end == start {
None
} else {
if target_element > element {
binary_search_3(array, target_element, middle + 1, end)
} else {
if middle > 0 {
binary_search_3(array, target_element, start, middle - 1)
} else {
None
}
}
}
break;
}
}
pub fn binary_search_4(array: &[i32], target_element: i32, mut start: usize, mut end: usize) -> Option<usize> {
loop {
let middle = (end + start) / 2;
let element = array[middle];
if element == target_element {
Some(middle)
} else if end == start {
None
} else {
if target_element > element {
start = middle + 1;
continue;
} else {
if middle > 0 {
end = middle - 1;
continue;
} else {
None
}
}
}
break;
}
}
pub fn binary_search_5(array: &[i32], target_element: i32, mut start: usize, mut end: usize) -> Option<usize> {
while end >= start {
let middle = (end + start) / 2;
let element = array[middle];
if element == target_element {
return Some(middle);
} else {
if target_element > element {
start = middle + 1;
} else {
if middle > 0 {
end = middle - 1;
} else {
return None;
}
}
}
}
None
}
public static int binarySearch1(final int[] array, final int targetElement, final int start, final int end) {
final int middle = (end + start) / 2;
final int element = array[middle];
if (element == targetElement) {
return middle;
} else if (end == start) {
return -1;
} else {
if (targetElement > element) {
return binarySearch1(array, targetElement, middle + 1, end);
} else {
return binarySearch1(array, targetElement, start, middle - 1);
}
}
}
public static int binarySearch3(final int[] array, final int targetElement, final int start, final int end) {
while (true) {
final int middle = (end + start) / 2;
final int element = array[middle];
if (element == targetElement) {
return middle;
} else if (end == start) {
return -1;
} else {
if (targetElement > element) {
return binarySearch3(array, targetElement, middle + 1, end);
} else {
return binarySearch3(array, targetElement, start, middle - 1);
}
}
break;
}
}
public static int binarySearch4(final int[] array, final int targetElement, int start, int end) {
while (true) {
final int middle = (end + start) / 2;
final int element = array[middle];
if (element == targetElement) {
return middle;
} else if (end == start) {
return -1;
} else {
if (targetElement > element) {
start = middle + 1;
continue;
} else {
end = middle - 1;
continue;
}
}
break;
}
}
public static int binarySearch5(final int[] array, final int targetElement, int start, int end) {
while (end >= start) {
final int middle = (end + start) / 2;
final int element = array[middle];
if (element == targetElement) {
return middle;
} else {
if (targetElement > element) {
start = middle + 1;
} else {
end = middle - 1;
}
}
}
return -1;
}
static int step(final int n) {
return n * factorial(n - 1);
}
public static int factorial(final int n) {
if (n <= 1) {
return 1;
} else {
return step(n);
}
}
function step(n: number): number {
return n * factorial(n - 1);
}
function factorial(n: number) {
if (n <= 1) {
return 1;
} else {
return step(n);
}
}
func step(n int) int {
return n * Factorial(n-1)
}
func Factorial(n int) int {
if n <= 1 {
return 1
} else {
return step(n)
}
}
步驟二:拆解子程序的程式敘述
將子程序的步驟分為三大部份:遞迴呼叫(取得前項結果)、計算目前這項的結果,以及回傳結果。
程式碼如下:
fn step(n: u32) -> u32 {
let previous_x = factorial(n - 1);
let x = n * previous_x;
x
}
pub fn factorial(n: u32) -> u32 {
if n <= 1 {
1
} else {
step(n)
}
}
static int step(final int n) {
final int previousX = factorial(n - 1);
final int x = n * previousX;
return x;
}
public static int factorial(final int n) {
if (n <= 1) {
return 1;
} else {
return step(n);
}
}
function step(n: number) {
const previousX = factorial(n - 1);
const x = n * previousX;
return x;
}
function factorial(n: number): number {
if (n <= 1) {
return 1;
} else {
return step(n);
}
}
func step(n int) int {
previousX := Factorial(n - 1)
x := n * previousX
return x
}
func Factorial(n int) int {
if n <= 1 {
return 1
} else {
return step(n)
}
}
fn step(n: u32, previous_x: Option<u32>) -> u32 {
let previous_x = if let Some(previous_x) = previous_x {
previous_x
} else {
factorial(n - 1)
};
let x = n * previous_x;
x
}
pub fn factorial(n: u32) -> u32 {
if n <= 1 {
1
} else {
step(n, None)
}
}
static int step(final int n, final Optional<Integer> previousX) {
final int iPreviousX;
if (previousX.isPresent()) {
iPreviousX = previousX.get();
} else {
iPreviousX = factorial(n - 1);
}
final int x = n * iPreviousX;
return x;
}
public static int factorial(final int n) {
if (n <= 1) {
return 1;
} else {
return step(n, Optional.empty());
}
}
function step(n: number, previousX?: number) {
if (typeof previousX !== "number") {
previousX = factorial(n - 1);
}
const x = n * previousX;
return x;
}
function factorial(n: number): number {
if (n <= 1) {
return 1;
} else {
return step(n);
}
}
func step(n int, previousX ...int) int {
var iPreviousX int
if len(previousX) > 0 {
iPreviousX = previousX[0]
} else {
iPreviousX = Factorial(n - 1)
}
x := n * iPreviousX
return x
}
func Factorial(n int) int {
if n <= 1 {
return 1
} else {
return step(n)
}
}
fn step(n: u32, previous_x: Option<u32>) -> (u32, u32) {
let previous_x = if let Some(previous_x) = previous_x {
previous_x
} else {
factorial(n - 1)
};
let x = n * previous_x;
(n - 1, x)
}
pub fn factorial(n: u32) -> u32 {
if n <= 1 {
1
} else {
step(n, None).1
}
}
static int[] step(final int n, final Optional<Integer> previousX) {
final int iPreviousX;
if (previousX.isPresent()) {
iPreviousX = previousX.get();
} else {
iPreviousX = factorial(n - 1);
}
final int x = n * iPreviousX;
return new int[]{n + 1, x};
}
public static int factorial(final int n) {
if (n <= 1) {
return 1;
} else {
return step(n, Optional.empty())[1];
}
}
function step(n: number, previousX?: number) {
if (typeof previousX !== "number") {
previousX = factorial(n - 1);
}
const x = n * previousX;
return [n + 1, x];
}
function factorial(n: number): number {
if (n <= 1) {
return 1;
} else {
return step(n)[1];
}
}
func step(n int, previousX ...int) (int, int) {
var iPreviousX int
if len(previousX) > 0 {
iPreviousX = previousX[0]
} else {
iPreviousX = Factorial(n - 1)
}
x := n * iPreviousX
return n + 1, x
}
func Factorial(n int) int {
if n <= 1 {
return 1
} else {
_, x := step(n)
return x
}
}
fn step(n: u32, previous_x: Option<u32>) -> (u32, u32) {
let previous_x = if let Some(previous_x) = previous_x {
previous_x
} else {
factorial(n - 1)
};
let x = n * previous_x;
(n + 1, x)
}
pub fn factorial(n: u32) -> u32 {
if n <= 1 {
1
} else {
let t = n;
let (mut n, mut previous_x) = (1, 1);
for _ in 0..t {
let (next_n, x) = step(n, Some(previous_x));
n = next_n;
previous_x = x;
}
previous_x
}
}
static int[] step(final int n, final Optional<Integer> previousX) {
final int iPreviousX;
if (previousX.isPresent()) {
iPreviousX = previousX.get();
} else {
iPreviousX = factorial(n - 1);
}
final int x = n * iPreviousX;
return new int[]{n + 1, x};
}
public static int factorial(final int n) {
if (n <= 1) {
return 1;
} else {
final int t = n;
int nn = 1;
int previousX = 1;
for (int i = 0; i < t; ++i) {
final int[] r = step(nn, Optional.of(previousX));
nn = r[0];
previousX = r[1];
}
return previousX;
}
}
function step(n: number, previousX?: number) {
if (typeof previousX !== "number") {
previousX = factorial(n - 1);
}
const x = n * previousX;
return [n + 1, x];
}
function factorial(n: number) {
if (n <= 1) {
return 1;
} else {
const t = n;
let nn = 1;
let previousX = 1;
for (let i = 0;i < t;i++) {
[nn, previousX] = step(nn, previousX);
}
return previousX;
}
}
func step(n int, previousX ...int) (int, int) {
var iPreviousX int
if len(previousX) > 0 {
iPreviousX = previousX[0]
} else {
iPreviousX = Factorial(n - 1)
}
x := n * iPreviousX
return n + 1, x
}
func Factorial(n int) int {
if n <= 1 {
return 1
} else {
t := n
nn := 1
previousX := 1
for i := 0; i < t; i++ {
nn, previousX = step(nn, previousX)
}
return previousX
}
}
fn step(n: u32, previous_x: u32) -> (u32, u32) {
let x = n * previous_x;
(n + 1, x)
}
pub fn factorial(n: u32) -> u32 {
let t = n;
let (mut n, mut previous_x) = (1, 1);
for _ in 0..t {
let (next_n, x) = step(n, previous_x);
n = next_n;
previous_x = x;
}
previous_x
}
static int[] step(final int n, final int previousX) {
final int x = n * previousX;
return new int[]{n + 1, x};
}
public static int factorial(final int n) {
final int t = n;
int nn = 1;
int previousX = 1;
for (int i = 0; i < t; ++i) {
final int[] r = step(nn, previousX);
nn = r[0];
previousX = r[1];
}
return previousX;
}
function step(n: number, previousX: number) {
const x = n * previousX;
return [n + 1, x];
}
function factorial(n: number) {
const t = n;
let nn = 1;
let previousX = 1;
for (let i = 0;i < t;i++) {
[nn, previousX] = step(nn, previousX);
}
return previousX;
}
func step(n int, previousX int) (int, int) {
x := n * previousX
return n + 1, x
}
func Factorial(n int) int {
t := n
nn := 1
previousX := 1
for i := 0; i < t; i++ {
nn, previousX = step(nn, previousX)
}
return previousX
}
pub fn factorial(n: u32) -> u32 {
let t = n;
let (mut n, mut previous_x) = (1, 1);
for _ in 0..t {
previous_x *= n;
n += 1;
}
previous_x
}
public static int factorial(final int n) {
final int t = n;
int nn = 1;
int previousX = 1;
for (int i = 0; i < t; ++i) {
previousX *= nn;
++nn;
}
return previousX;
}
function factorial(n: number) {
const t = n;
let nn = 1;
let previousX = 1;
for (let i = 0;i < t;i++) {
previousX *= nn;
nn += 1;
}
return previousX;
}
func Factorial(n int) int {
t := n
nn := 1
previousX := 1
for i := 0; i < t; i++ {
previousX *= nn
nn++
}
return previousX
}
步驟八:再次整理程式碼
內聯之後的迭代函數,可能會有一些冗餘的地方可以進行合併與刪減。
整理一下後,程式碼最終會變成:
pub fn factorial(n: u32) -> u32 {
let mut previous_x = 1;
for i in 2..=n {
previous_x *= i;
}
previous_x
}
public static int factorial(final int n) {
int previousX = 1;
for (int i = 2; i <= n; ++i) {
previousX *= i;
}
return previousX;
}
function factorial(n: number) {
let previousX = 1;
for (let i = 2;i <= n;i++) {
previousX *= i;
}
return previousX;
}
func Factorial(n int) int {
previousX := 1
for i := 2; i <= n; i++ {
previousX *= i
}
return previousX
}
更多的轉換例子
有遞迴關係的數列函數基本上都可以嘗試使用上面的流程來轉換成迭代函數,以下再舉別的例子。
費氏數列
本篇文章最一開始就有詳細說明費氏數列的遞迴函數轉迭代函數的方式,現在則要改用第二種方式來進行轉換。
直接看以下程式碼吧!
fn step_1(n: u32) -> u32 {
fibonacci_1(n - 1) + fibonacci_1(n - 2)
}
pub fn fibonacci_1(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
n => step_1(n),
}
}
fn step_2(n: u32) -> u32 {
let previous_x = fibonacci_2(n - 1);
let before_previous_x = fibonacci_2(n - 2);
let x = previous_x + before_previous_x;
x
}
pub fn fibonacci_2(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
n => step_2(n),
}
}
fn step_3(n: u32, previous_x: Option<u32>, before_previous_x: Option<u32>) -> u32 {
let previous_x = if let Some(previous_x) = previous_x {
previous_x
} else {
fibonacci_3(n - 1)
};
let before_previous_x = if let Some(before_previous_x) = before_previous_x {
before_previous_x
} else {
fibonacci_3(n - 2)
};
let x = previous_x + before_previous_x;
x
}
pub fn fibonacci_3(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
n => step_3(n, None, None),
}
}
fn step_4(n: u32, previous_x: Option<u32>, before_previous_x: Option<u32>) -> (u32, u32, u32) {
let previous_x = if let Some(previous_x) = previous_x {
previous_x
} else {
fibonacci_4(n - 1)
};
let before_previous_x = if let Some(before_previous_x) = before_previous_x {
before_previous_x
} else {
fibonacci_4(n - 2)
};
let x = previous_x + before_previous_x;
(n + 1, previous_x, x)
}
pub fn fibonacci_4(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
n => step_4(n, None, None).2,
}
}
fn step_5(n: u32, previous_x: Option<u32>, before_previous_x: Option<u32>) -> (u32, u32, u32) {
let previous_x = if let Some(previous_x) = previous_x {
previous_x
} else {
fibonacci_5(n - 1)
};
let before_previous_x = if let Some(before_previous_x) = before_previous_x {
before_previous_x
} else {
fibonacci_5(n - 2)
};
let x = previous_x + before_previous_x;
(n + 1, previous_x, x)
}
pub fn fibonacci_5(n: u32) -> u32 {
match n {
0 => 0,
1 => 1,
n => {
let t = n - 1;
let (mut n, mut previous_x, mut before_previous_x) = (2, 1, 0);
for _ in 0..t {
let (next_n, next_before_previous_x, x) = step_5(n, Some(previous_x), Some(before_previous_x));
n = next_n;
previous_x = x;
before_previous_x = next_before_previous_x;
}
previous_x
}
}
}
fn step_6(n: u32, previous_x: u32, before_previous_x: u32) -> (u32, u32, u32) {
let x = previous_x + before_previous_x;
(n + 1, previous_x, x)
}
pub fn fibonacci_6(n: u32) -> u32 {
match n {
0 => 0,
n => {
let t = n - 1;
let (mut n, mut previous_x, mut before_previous_x) = (2, 1, 0);
for _ in 0..t {
let (next_n, next_before_previous_x, x) = step_6(n, previous_x, before_previous_x);
n = next_n;
previous_x = x;
before_previous_x = next_before_previous_x;
}
previous_x
}
}
}
pub fn fibonacci_7(n: u32) -> u32 {
match n {
0 => 0,
n => {
let t = n - 1;
let (mut n, mut previous_x, mut before_previous_x) = (2, 1, 0);
for _ in 0..t {
let x = previous_x + before_previous_x;
n += 1;
before_previous_x = previous_x;
previous_x = x;
}
previous_x
}
}
}
pub fn fibonacci_8(n: u32) -> u32 {
match n {
0 => 0,
n => {
let (mut previous_x, mut before_previous_x) = (1, 0);
for _ in 1..n {
let x = previous_x + before_previous_x;
before_previous_x = previous_x;
previous_x = x;
}
previous_x
}
}
}
static int step1(final int n) {
return fibonacci1(n - 1) + fibonacci7(n - 2);
}
public static int fibonacci1(final int n) {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return step1(n);
}
}
static int step2(final int n) {
final int previousX = fibonacci2(n - 1);
final int beforePreviousX = fibonacci2(n - 2);
final int x = previousX + beforePreviousX;
return x;
}
public static int fibonacci2(final int n) {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return step2(n);
}
}
static int step3(final int n, final Optional<Integer> previousX, final Optional<Integer> beforePreviousX) {
final int iPreviousX, iBeforePreviousX;
if (previousX.isPresent()) {
iPreviousX = previousX.get();
} else {
iPreviousX = fibonacci3(n - 1);
}
if (beforePreviousX.isPresent()) {
iBeforePreviousX = beforePreviousX.get();
} else {
iBeforePreviousX = fibonacci3(n - 2);
}
final int x = iPreviousX + iBeforePreviousX;
return x;
}
public static int fibonacci3(final int n) {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return step3(n, Optional.empty(), Optional.empty());
}
}
static int[] step4(final int n, final Optional<Integer> previousX, final Optional<Integer> beforePreviousX) {
final int iPreviousX, iBeforePreviousX;
if (previousX.isPresent()) {
iPreviousX = previousX.get();
} else {
iPreviousX = fibonacci4(n - 1);
}
if (beforePreviousX.isPresent()) {
iBeforePreviousX = beforePreviousX.get();
} else {
iBeforePreviousX = fibonacci4(n - 2);
}
final int x = iPreviousX + iBeforePreviousX;
return new int[]{n + 1, iPreviousX, x};
}
public static int fibonacci4(final int n) {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return step4(n, Optional.empty(), Optional.empty())[2];
}
}
static int[] step5(final int n, final Optional<Integer> previousX, final Optional<Integer> beforePreviousX) {
final int iPreviousX, iBeforePreviousX;
if (previousX.isPresent()) {
iPreviousX = previousX.get();
} else {
iPreviousX = fibonacci5(n - 1);
}
if (beforePreviousX.isPresent()) {
iBeforePreviousX = beforePreviousX.get();
} else {
iBeforePreviousX = fibonacci5(n - 2);
}
final int x = iPreviousX + iBeforePreviousX;
return new int[]{n + 1, iPreviousX, x};
}
public static int fibonacci5(final int n) {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
final int t = n - 1;
int nn = 2;
int previousX = 1;
int beforePreviousX = 0;
for (int i = 0; i < t; ++i) {
final int[] r = step5(nn, Optional.of(previousX), Optional.of(beforePreviousX));
nn = r[0];
previousX = r[2];
beforePreviousX = r[1];
}
return previousX;
}
}
static int[] step6(int n, final int previousX, final int beforePreviousX) {
final int x = previousX + beforePreviousX;
return new int[]{n + 1, previousX, x};
}
public static int fibonacci6(final int n) {
switch (n) {
case 0:
return 0;
default:
final int t = n - 1;
int nn = 2;
int previousX = 1;
int beforePreviousX = 0;
for (int i = 0; i < t; ++i) {
final int[] r = step6(nn, previousX, beforePreviousX);
nn = r[0];
previousX = r[2];
beforePreviousX = r[1];
}
return previousX;
}
}
public static int fibonacci7(final int n) {
switch (n) {
case 0:
return 0;
default:
final int t = n - 1;
int nn = 2;
int previousX = 1;
int beforePreviousX = 0;
for (int i = 0; i < t; ++i) {
final int x = previousX + beforePreviousX;
++nn;
beforePreviousX = previousX;
previousX = x;
}
return previousX;
}
}
public static int fibonacci8(final int n) {
switch (n) {
case 0:
return 0;
default:
int previousX = 1;
int beforePreviousX = 0;
for (int i = 1; i < n; ++i) {
final int x = previousX + beforePreviousX;
beforePreviousX = previousX;
previousX = x;
}
return previousX;
}
}
function step1(n: number) {
return fibonacci1(n - 1) + fibonacci1(n - 2);
}
function fibonacci1(n: number): number {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return step1(n);
}
}
function step2(n: number) {
const previousX = fibonacci2(n - 1);
const beforePreviousX = fibonacci2(n - 2);
const x = previousX + beforePreviousX;
return x;
}
function fibonacci2(n: number): number {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return step2(n);
}
}
function step3(n: number, previousX?: number, beforePreviousX?: number) {
if (typeof previousX !== "number") {
previousX = fibonacci3(n - 1);
}
if (typeof beforePreviousX !== "number") {
beforePreviousX = fibonacci3(n - 2);
}
const x = previousX + beforePreviousX;
return x;
}
function fibonacci3(n: number): number {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return step3(n);
}
}
function step4(n: number, previousX?: number, beforePreviousX?: number) {
if (typeof previousX !== "number") {
previousX = fibonacci4(n - 1);
}
if (typeof beforePreviousX !== "number") {
beforePreviousX = fibonacci4(n - 2);
}
const x = previousX + beforePreviousX;
return [n + 1, previousX, x];
}
function fibonacci4(n: number): number {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default:
return step4(n)[2];
}
}
function step5(n: number, previousX?: number, beforePreviousX?: number) {
if (typeof previousX !== "number") {
previousX = fibonacci5(n - 1);
}
if (typeof beforePreviousX !== "number") {
beforePreviousX = fibonacci5(n - 2);
}
const x = previousX + beforePreviousX;
return [n + 1, previousX, x];
}
function fibonacci5(n: number) {
switch (n) {
case 0:
return 0;
case 1:
return 1;
default: {
const t = n - 1;
let nn = 2;
let previousX = 1;
let beforePreviousX = 0;
for (let i = 0;i < t;i++) {
[nn, beforePreviousX, previousX] = step5(nn, previousX, beforePreviousX);
}
return previousX;
}
}
}
function step6(n: number, previousX: number, beforePreviousX: number) {
const x = previousX + beforePreviousX;
return [n + 1, previousX, x];
}
function fibonacci6(n: number) {
switch (n) {
case 0:
return 0;
default: {
const t = n - 1;
let nn = 2;
let previousX = 1;
let beforePreviousX = 0;
for (let i = 0;i < t;i++) {
[nn, beforePreviousX, previousX] = step6(nn, previousX, beforePreviousX);
}
return previousX;
}
}
}
function fibonacci7(n: number) {
switch (n) {
case 0:
return 0;
default: {
const t = n - 1;
let nn = 2;
let previousX = 1;
let beforePreviousX = 0;
for (let i = 0;i < t;i++) {
const x = previousX + beforePreviousX;
nn += 1;
beforePreviousX = previousX;
previousX = x;
}
return previousX;
}
}
}
function fibonacci8(n: number) {
switch (n) {
case 0:
return 0;
default: {
let previousX = 1;
let beforePreviousX = 0;
for (let i = 1;i < n;i++) {
const x = previousX + beforePreviousX;
beforePreviousX = previousX;
previousX = x;
}
return previousX;
}
}
}
func step1(n int) int {
return Fibonacci1(n-1) + Fibonacci1(n-2)
}
func Fibonacci1(n int) int {
switch n {
case 0:
return 0
case 1:
return 1
default:
return step1(n)
}
}
func step2(n int) int {
previousX := Fibonacci2(n - 1)
beforePreviousX := Fibonacci2(n - 2)
x := previousX + beforePreviousX
return x
}
func Fibonacci2(n int) int {
switch n {
case 0:
return 0
case 1:
return 1
default:
return step2(n)
}
}
func step3(n int, previous ...int) int {
var previousX, beforePreviousX int
if len(previous) > 0 {
previousX = previous[0]
} else {
previousX = Fibonacci3(n - 1)
}
if len(previous) > 1 {
beforePreviousX = previous[1]
} else {
beforePreviousX = Fibonacci3(n - 2)
}
x := previousX + beforePreviousX
return x
}
func Fibonacci3(n int) int {
switch n {
case 0:
return 0
case 1:
return 1
default:
return step3(n)
}
}
func step4(n int, previous ...int) (int, int, int) {
var previousX, beforePreviousX int
if len(previous) > 0 {
previousX = previous[0]
} else {
previousX = Fibonacci4(n - 1)
}
if len(previous) > 1 {
beforePreviousX = previous[1]
} else {
beforePreviousX = Fibonacci4(n - 2)
}
x := previousX + beforePreviousX
return n + 1, previousX, x
}
func Fibonacci4(n int) int {
switch n {
case 0:
return 0
case 1:
return 1
default:
_, _, x := step4(n)
return x
}
}
func step5(n int, previous ...int) (int, int, int) {
var previousX, beforePreviousX int
if len(previous) > 0 {
previousX = previous[0]
} else {
previousX = Fibonacci5(n - 1)
}
if len(previous) > 1 {
beforePreviousX = previous[1]
} else {
beforePreviousX = Fibonacci5(n - 2)
}
x := previousX + beforePreviousX
return n + 1, previousX, x
}
func Fibonacci5(n int) int {
switch n {
case 0:
return 0
case 1:
return 1
default:
t := n - 1
nn := 2
previousX := 1
beforePreviousX := 0
for i := 0; i < t; i++ {
nn, beforePreviousX, previousX = step5(nn, previousX, beforePreviousX)
}
return previousX
}
}
func step6(n int, previousX int, beforePreviousX int) (int, int, int) {
x := previousX + beforePreviousX
return n + 1, previousX, x
}
func Fibonacci6(n int) int {
switch n {
case 0:
return 0
default:
t := n - 1
nn := 2
previousX := 1
beforePreviousX := 0
for i := 0; i < t; i++ {
nn, beforePreviousX, previousX = step6(nn, previousX, beforePreviousX)
}
return previousX
}
}
func Fibonacci7(n int) int {
switch n {
case 0:
return 0
default:
t := n - 1
nn := 2
previousX := 1
beforePreviousX := 0
for i := 0; i < t; i++ {
x := previousX + beforePreviousX
nn++
beforePreviousX = previousX
previousX = x
}
return previousX
}
}
func Fibonacci8(n int) int {
switch n {
case 0:
return 0
default:
previousX := 1
beforePreviousX := 0
for i := 1; i < n; i++ {
x := previousX + beforePreviousX
beforePreviousX = previousX
previousX = x
}
return previousX
}
}
void f(param1, param2, ..., paramN) {
stack1 = Empty Stack;
stack2 = Empty Stack;
push param1, param2, ..., paramN to stack1
loop {
pop param1, param2, ..., paramN from stack1, if pop nothing then break
push a, b, ..., n to stack1 // recursive call 1
push aa, bb, ..., nn to stack1 // recursive call 2
// ...
push aaa, bbb, ..., nnn to stack1 // recursive call n
push param1, param2, ..., paramN to stack2
}
loop {
pop param1, param2, ..., paramN from stack2, if pop nothing then break
// do something
}
}