Telephone Number
Last Updated :
17 Jul, 2022
In mathematics, the telephone numbers involution numbers are a sequence of integers that count the number of connection patterns in a telephone system with n subscribers, where connections are made between pairs of subscribers. These numbers also describe the number of matchings of a complete graph of n vertices, the number of permutations on n elements that are involutions, the sum of absolute value of coefficients of the Hermite polynomials, the number of standard Young tableaux with n cells, and the sum of the degrees of the irreducible representations of the symmetric group.
The telephone numbers are also used to count the number of ways to place n rooks on an n x n chessboard in such a way that no two rooks attack each other and in such a way the configuration of the rooks is symmetric under a diagonal reflection of the board.
The telephone number can be evaluated by the following recurrence relation:
Given a positive integer n. The task is to find the nth telephone number.
Examples :
Input : n = 4
Output : 10
Input : n = 6
Output : 76
Below is naive implementation of finding the nth telephone number based on above recursive formula.
C++
#include <bits/stdc++.h>
using namespace std;
int telephonenumber( int n)
{
if (n == 0 || n == 1)
return 1;
return telephonenumber(n - 1) +
(n - 1) * telephonenumber(n - 2);
}
int main()
{
int n = 6;
cout << telephonenumber(n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int telephonenumber( int n)
{
if (n == 0 || n == 1 )
return 1 ;
return telephonenumber(n - 1 ) +
(n - 1 ) * telephonenumber(n - 2 );
}
public static void main(String[] args)
{
int n = 6 ;
System.out.println(telephonenumber(n));
}
}
|
Python3
def telephonenumber (n):
if n = = 0 or n = = 1 :
return 1
return (telephonenumber(n - 1 ) + (n - 1 )
* telephonenumber(n - 2 ))
n = 6
print (telephonenumber(n))
|
C#
using System;
class GFG {
static int telephonenumber( int n)
{
if (n == 0 || n == 1)
return 1;
return telephonenumber(n - 1) +
(n - 1) * telephonenumber(n - 2);
}
public static void Main()
{
int n = 6;
Console.Write(telephonenumber(n));
}
}
|
PHP
<?php
function telephonenumber( $n )
{
if ( $n == 0 or $n == 1)
return 1;
return telephonenumber( $n - 1) +
( $n - 1) * telephonenumber( $n - 2);
}
$n = 6;
echo telephonenumber( $n ) ;
?>
|
Javascript
<script>
function telephonenumber(n)
{
if (n == 0 || n == 1)
return 1;
return telephonenumber(n - 1) +
(n - 1) * telephonenumber(n - 2);
}
var n = 6;
document.write( telephonenumber(n));
</script>
|
Output:
76
Time complexity: O(2n)
Auxiliary Space: O(2n)
Below is efficient implementation of finding the nth telephone number using Dynamic Programming:
C++
#include <bits/stdc++.h>
using namespace std;
int telephonenumber( int n)
{
int dp[n + 1];
memset (dp, 0, sizeof (dp));
dp[0] = dp[1] = 1;
for ( int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
return dp[n];
}
int main()
{
int n = 6;
cout << telephonenumber(n) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static int telephonenumber( int n)
{
int dp[] = new int [n + 1 ];
dp[ 0 ] = dp[ 1 ] = 1 ;
for ( int i = 2 ; i <= n; i++)
dp[i] = dp[i - 1 ] + (i - 1 ) * dp[i - 2 ];
return dp[n];
}
public static void main(String[] args)
{
int n = 6 ;
System.out.println(telephonenumber(n));
}
}
|
Python3
def telephonenumber (n):
dp = [ 0 ] * (n + 1 )
dp[ 0 ] = dp[ 1 ] = 1
for i in range ( 2 , n + 1 ):
dp[i] = dp[i - 1 ] + (i - 1 ) * dp[i - 2 ]
return dp[n]
n = 6
print (telephonenumber(n))
|
C#
using System;
class GFG {
static int telephonenumber( int n)
{
int [] dp = new int [n + 1];
dp[0] = dp[1] = 1;
for ( int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
return dp[n];
}
public static void Main()
{
int n = 6;
Console.Write(telephonenumber(n));
}
}
|
PHP
<?php
function telephonenumber( $n )
{
$dp = array ();
$dp [0] = $dp [1] = 1;
for ( $i = 2; $i <= $n ; $i ++)
$dp [ $i ] = $dp [ $i - 1] +
( $i - 1) *
$dp [ $i - 2];
return $dp [ $n ];
}
$n = 6;
echo telephonenumber( $n );
?>
|
Javascript
<script>
function telephonenumber(n)
{
let dp = [];
dp[0] = dp[1] = 1;
for (let i = 2; i <= n; i++)
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
return dp[n];
}
let n = 6;
document.write(telephonenumber(n));
</script>
|
Output:
76
Time complexity: O(n)
Auxiliary space: O(n)
Share your thoughts in the comments
Please Login to comment...