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++
// CPP Program to find the nth telephone number.
#include <bits/stdc++.h>
usingnamespacestd;
// return the nth telephone number
inttelephonenumber(intn)
{
// base step
if(n == 0 || n == 1)
return1;
// recursive step
returntelephonenumber(n - 1) +
(n - 1) * telephonenumber(n - 2);
}
// Driven Program
intmain()
{
intn = 6;
cout << telephonenumber(n) << endl;
return0;
}
Java
// JAVA Code to find the nth
// telephone number.
importjava.util.*;
classGFG {
// return the nth telephone number
staticinttelephonenumber(intn)
{
// base step
if(n == 0|| n == 1)
return1;
// recursive step
returntelephonenumber(n - 1) +
(n - 1) * telephonenumber(n - 2);
}
/* Driver program to test above function */
publicstaticvoidmain(String[] args)
{
intn = 6;
System.out.println(telephonenumber(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Python3 code to find the
# nth telephone number.
# return the nth telephone number
deftelephonenumber (n):
# base step
ifn ==0orn ==1:
return1
# recursive step
return(telephonenumber(n -1) +(n -1)
*telephonenumber(n -2))
# Driven Program
n =6
print(telephonenumber(n))
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# Code to find the nth
// telephone number.
usingSystem;
classGFG {
// return the nth telephone number
staticinttelephonenumber(intn)
{
// base step
if(n == 0 || n == 1)
return1;
// recursive step
returntelephonenumber(n - 1) +
(n - 1) * telephonenumber(n - 2);
}
/* Driver program to test above function */
publicstaticvoidMain()
{
intn = 6;
Console.Write(telephonenumber(n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP Program to find
// the nth telephone number
// return the nth
// telephone number
functiontelephonenumber( $n)
{
// base step
if($n== 0 or$n== 1)
return1;
// recursive step
returntelephonenumber($n- 1) +
($n- 1) * telephonenumber($n- 2);
}
// Driven Code
$n= 6;
echotelephonenumber($n) ;
// This code is contributed by anuj_67.
?>
Javascript
<script>
// Javascript Program to find
// the nth telephone number.
// return the nth telephone number
functiontelephonenumber(n)
{
// base step
if(n == 0 || n == 1)
return1;
// recursive step
returntelephonenumber(n - 1) +
(n - 1) * telephonenumber(n - 2);
}
// Driven Program
varn = 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++
// CPP Program to find the nth telephone number.
#include <bits/stdc++.h>
usingnamespacestd;
// return the nth telephone number
inttelephonenumber(intn)
{
intdp[n + 1];
memset(dp, 0, sizeof(dp));
// Base case
dp[0] = dp[1] = 1;
// finding ith telephone number, where 2 <= i <= n.
for(inti = 2; i <= n; i++)
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
returndp[n];
}
// Driver Program
intmain()
{
intn = 6;
cout << telephonenumber(n) << endl;
return0;
}
Java
// JAVA Code to find nth Telephone Number
importjava.util.*;
classGFG {
// return the nth telephone number
staticinttelephonenumber(intn)
{
intdp[] = newint[n + 1];
// Base case
dp[0] = dp[1] = 1;
// finding ith telephone number,
// where 2 <= i <= n.
for(inti = 2; i <= n; i++)
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
returndp[n];
}
/* Driver program to test above function */
publicstaticvoidmain(String[] args)
{
intn = 6;
System.out.println(telephonenumber(n));
}
}
// This code is contributed by Arnav Kr. Mandal.
Python3
# Python3 code to find the
# nth telephone number.
# return the nth telephone number
deftelephonenumber (n):
dp =[0] *(n +1)
# Base case
dp[0] =dp[1] =1
# finding ith telephone number,
# where 2 <= i <= n.
fori inrange(2, n +1):
dp[i] =dp[i -1] +(i -1) *dp[i -2]
returndp[n]
# Driver Code
n =6
print(telephonenumber(n))
# This code is contributed by "Sharad_Bhardwaj".
C#
// C# Code to find nth Telephone Number
usingSystem;
classGFG {
// return the nth telephone number
staticinttelephonenumber(intn)
{
int[] dp = newint[n + 1];
// Base case
dp[0] = dp[1] = 1;
// finding ith telephone number,
// where 2 <= i <= n.
for(inti = 2; i <= n; i++)
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
returndp[n];
}
/* Driver program to test above function */
publicstaticvoidMain()
{
intn = 6;
Console.Write(telephonenumber(n));
}
}
// This code is contributed by vt_m.
PHP
<?php
// PHP Program to find
// the nth telephone number.
// return the nth telephone number
functiontelephonenumber($n)
{
$dp= array();
// Base case
$dp[0] = $dp[1] = 1;
// finding ith telephone number,
// where 2 <= i <= n.
for( $i= 2; $i<= $n; $i++)
$dp[$i] = $dp[$i- 1] +
($i- 1) *
$dp[$i- 2];
return$dp[$n];
}
// Driver Code
$n= 6;
echotelephonenumber($n);
// This code is contributed by anuj_67.
?>
Javascript
<script>
// JavaScript Program to find nth Telephone Number
// return the nth telephone number
functiontelephonenumber(n)
{
let dp = [];
// Base case
dp[0] = dp[1] = 1;
// finding ith telephone number,
// where 2 <= i <= n.
for(let i = 2; i <= n; i++)
dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];
returndp[n];
}
// Driver code
let n = 6;
document.write(telephonenumber(n));
// This code is contributed by sanjoy_62.
</script>
Output:
76
Time complexity: O(n)
Auxiliary space: O(n)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!