以Hofstadter雌雄序列为例的相互递归

相互递归是一种变体 递归 .如果第一个函数对第二个函数进行递归调用,而第二个函数又反过来调用第一个函数,则这两个函数是相互递归调用的。 在软件开发中,这个概念被用于循环依赖,循环依赖是两个或多个模块之间的关系,这些模块直接或间接地相互依赖以正常运行。这种模块也称为相互递归模块。 相互递归的一个很好的例子是实现Hofstadter序列。

null

霍夫斯塔德序列

在数学中,Hofstadter序列是由 非线性 递归关系。在本例中,我们将重点介绍 Hofstadter雌雄序列:

F ( 0 ) = 1M ( 0 ) = 0 F ( n ) = n - M ( F ( n - 1 ) ), n > 0 M ( n ) = n - F ( M ( n - 1 ) ), n > 0. 

C++

// C++ program to implement Hofstader Sequence
// using mutual recursion
#include <bits/stdc++.h>
using namespace std;
int hofstaderFemale( int );
int hofstaderMale( int );
// Female function
int hofstaderFemale( int n)
{
if (n < 0)
return 0;
else
if (n == 0)
return 1;
else
return (n - hofstaderFemale(n - 1));
}
// Male function
int hofstaderMale( int n)
{
if (n < 0)
return 0;
else
if (n == 0)
return 0;
else
return (n - hofstaderMale(n - 1));
}
// Driver Code
int main()
{
int i;
cout << "F: " ;
for (i = 0; i < 20; i++)
cout << hofstaderFemale(i) << " " ;
cout << "" ;
cout << "M: " ;
for (i = 0; i < 20; i++)
cout << hofstaderMale(i)<< " " ;
return 0;
}
// This code is contributed by shubhamsingh10


C

// C program to implement Hofstader Sequence
// using mutual recursion
#include <stdio.h>
int hofstaderFemale( int );
int hofstaderMale( int );
// Female function
int hofstaderFemale( int n)
{
if (n < 0)
return ;
else
return (n == 0) ? 1 : n - hofstaderFemale(n - 1);
}
// Male function
int hofstaderMale( int n)
{
if (n < 0)
return ;
else
return (n == 0) ? 0 : n - hofstaderMale(n - 1);
}
// hard coded driver function to run the program
int main()
{
int i;
printf ( "F: " );
for (i = 0; i < 20; i++)
printf ( "%d " , hofstaderFemale(i));
printf ( "" );
printf ( "M: " );
for (i = 0; i < 20; i++)
printf ( "%d " , hofstaderMale(i));
return 0;
}


JAVA

// Java program to implement Hofstader
// Sequence using mutual recursion
import java .io.*;
class GFG {
// Female function
static int hofstaderFemale( int n)
{
if (n < 0 )
return 0 ;
else
return (n == 0 ) ? 1 : n -
hofstaderFemale(n - 1 );
}
// Male function
static int hofstaderMale( int n)
{
if (n < 0 )
return 0 ;
else
return (n == 0 ) ? 0 : n -
hofstaderMale(n - 1 );
}
// Driver Code
static public void main (String[] args)
{
int i;
System.out.print( "F: " );
for (i = 0 ; i < 20 ; i++)
System.out.print(hofstaderFemale(i)
+ " " );
System.out.println();
System.out.print( "M: " );
for (i = 0 ; i < 20 ; i++)
System.out.print(hofstaderMale(i)
+ " " );
}
}
// This code is contributed by anuj_67.


Python3

# Python program to implement
# Hofstader Sequence using
# mutual recursion
# Female function
def hofstaderFemale(n):
if n < 0 :
return ;
else :
val = 1 if n = = 0 else (
n - hofstaderFemale(n - 1 ))
return val
# Male function
def hofstaderMale(n):
if n < 0 :
return ;
else :
val = 0 if n = = 0 else (
n - hofstaderMale(n - 1 ))
return val
# Driver code
print ( "F:" , end = " " )
for i in range ( 0 , 20 ):
print (hofstaderFemale(i), end = " " )
print ( "" )
print ( "M:" , end = " " )
for i in range ( 0 , 20 ):
print (hofstaderMale(i), end = " " )
# This code is contributed
# by Shantanu Sharma


C#

// C# program to implement Hofstader
// Sequence using mutual recursion
using System;
class GFG {
// Female function
static int hofstaderFemale( int n)
{
if (n < 0)
return 0;
else
return (n == 0) ? 1 : n -
hofstaderFemale(n - 1);
}
// Male function
static int hofstaderMale( int n)
{
if (n < 0)
return 0;
else
return (n == 0) ? 0 : n -
hofstaderMale(n - 1);
}
// Driver Code
static public void Main ()
{
int i;
Console.WriteLine( "F: " );
for (i = 0; i < 20; i++)
Console.Write(hofstaderFemale(i) + " " );
Console.WriteLine();
Console.WriteLine( "M: " );
for (i = 0; i < 20; i++)
Console.Write(hofstaderMale(i) + " " );
}
}
// This code is contributed by Ajit.


PHP

<?php
// PHP program to implement
// Hofstader Sequence
// using mutual recursion
//function hofstaderFemale(int);
//int hofstaderMale(int);
// Female function
function hofstaderFemale( $n )
{
if ( $n < 0)
return ;
else
return ( $n == 0) ? 1 : $n - hofstaderFemale( $n - 1);
}
// Male function
function hofstaderMale( $n )
{
if ( $n < 0)
return ;
else
return ( $n == 0) ? 0 : $n - hofstaderMale( $n - 1);
}
// Driver Code
$i ;
echo "F: " ;
for ( $i = 0; $i < 20; $i ++)
echo hofstaderFemale( $i ), " " ;
echo "" ;
echo "M: " ;
for ( $i = 0; $i < 20; $i ++)
echo hofstaderMale( $i ), " " ;
// This code contributed by Ajit
?>


Javascript

<script>
// Javascript program to implement Hofstader
// Sequence using mutual recursion
// Female function
function hofstaderFemale(n)
{
if (n < 0)
return 0;
else
return (n == 0) ? 1 :
n - hofstaderFemale(n - 1);
}
// Male function
function hofstaderMale(n)
{
if (n < 0)
return 0;
else
return (n == 0) ? 0 :
n - hofstaderMale(n - 1);
}
// Driver code
let i;
document.write( "F: " );
for (i = 0; i < 20; i++)
document.write(hofstaderFemale(i) + " " );
document.write( "</br>" );
document.write( "M: " );
for (i = 0; i < 20; i++)
document.write(hofstaderMale(i) + " " );
// This code is contributed by rameshtravel07
</script>


输出:

F: 1 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 M: 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10 

循环依赖/相互递归的缺点:

  1. 当一个模块中的一个小的局部更改扩展到其他模块并产生不必要的全局影响时,循环依赖可能会导致多米诺效应
  2. 循环依赖还可能导致无限递归或其他意外故障。
  3. 循环依赖还可能通过阻止某些非常原始的自动垃圾收集器(使用引用计数的垃圾收集器)释放未使用的对象而导致内存泄漏。

参考资料: 维基百科

本文由 帕拉什尼甘酒店 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享