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>using namespace std;Â
// return the nth telephone numberint telephonenumber(int n){    // base step    if (n == 0 || n == 1)        return 1;Â
    // recursive step    return telephonenumber(n - 1) +           (n - 1) * telephonenumber(n - 2);}Â
// Driven Programint main(){Â Â Â Â int n = 6;Â Â Â Â cout << telephonenumber(n) << endl;Â Â Â Â return 0;} |
Java
// JAVA Code to find the nth// telephone number.import java.util.*;Â
class GFG {         // return the nth telephone number    static int telephonenumber(int n)    {        // base step        if (n == 0 || n == 1)            return 1;              // recursive step        return telephonenumber(n - 1) +               (n - 1) * telephonenumber(n - 2);    }         /* Driver program to test above function */    public static void main(String[] args)     {        int n = 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 numberdef telephonenumber (n):Â
    # base step    if n == 0 or n == 1:        return 1             # recursive step    return (telephonenumber(n - 1) + (n - 1)            * telephonenumber(n - 2))Â
# Driven Programn = 6print(telephonenumber(n))Â
# This code is contributed by "Sharad_Bhardwaj". |
C#
// C# Code to find the nth// telephone number.using System;Â
class GFG {Â
    // return the nth telephone number    static int telephonenumber(int n)    {        // base step        if (n == 0 || n == 1)            return 1;Â
        // recursive step        return telephonenumber(n - 1) +             (n - 1) * telephonenumber(n - 2);    }Â
    /* Driver program to test above function */    public static void Main()    {        int n = 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 numberfunction telephonenumber( $n){    // base step    if ($n == 0 or $n == 1)        return 1;Â
    // recursive step    return telephonenumber($n - 1) +         ($n - 1) * telephonenumber($n - 2);}Â
// Driven Code$n = 6;echo telephonenumber($n) ;Â
// This code is contributed by anuj_67.?> |
Javascript
<script>Â
Â
// Javascript Program to find // the nth telephone number.Â
// return the nth telephone numberfunction telephonenumber(n){    // base step    if (n == 0 || n == 1)        return 1;Â
    // recursive step    return telephonenumber(n - 1) +           (n - 1) * telephonenumber(n - 2);}Â
// Driven Programvar 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++
// CPP Program to find the nth telephone number.#include <bits/stdc++.h>using namespace std;Â
// return the nth telephone numberint telephonenumber(int n){Â Â Â Â int dp[n + 1];Â Â Â Â memset(dp, 0, sizeof(dp));Â
    // Base case    dp[0] = dp[1] = 1;Â
    // finding ith telephone number, where 2 <= i <= n.    for (int i = 2; i <= n; i++)        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];Â
    return dp[n];}Â
// Driver Programint main(){Â Â Â Â int n = 6;Â Â Â Â cout << telephonenumber(n) << endl;Â Â Â Â return 0;} |
Java
// JAVA Code to find nth Telephone Numberimport java.util.*;Â
class GFG {         // return the nth telephone number    static int telephonenumber(int n)    {        int dp[] = new int[n + 1];                // Base case        dp[0] = dp[1] = 1;              // finding ith telephone number,        // where 2 <= i <= n.        for (int i = 2; i <= n; i++)            dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];              return dp[n];    }         /* Driver program to test above function */    public static void main(String[] args)     {         int n = 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 numberdef telephonenumber (n):    dp = [0] * (n + 1)         # Base case    dp[0] = dp[1] = 1         # finding ith telephone number,    # where 2 <= i <= n.    for i in range(2, n + 1):        dp[i] = dp[i - 1] + (i - 1) * dp[i - 2]             return dp[n]     # Driver Coden = 6print(telephonenumber(n))Â
# This code is contributed by "Sharad_Bhardwaj". |
C#
// C# Code to find nth Telephone Numberusing System;Â
class GFG {Â
    // return the nth telephone number    static int telephonenumber(int n)    {        int[] dp = new int[n + 1];Â
        // Base case        dp[0] = dp[1] = 1;Â
        // finding ith telephone number,        // where 2 <= i <= n.        for (int i = 2; i <= n; i++)            dp[i] = dp[i - 1] + (i - 1) * dp[i - 2];Â
        return dp[n];    }Â
    /* Driver program to test above function */    public static void Main()    {        int n = 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 numberfunction telephonenumber($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;echo telephonenumber($n);Â
// This code is contributed by anuj_67.?> |
Javascript
<script>Â
// JavaScript Program to find nth Telephone NumberÂ
    // return the nth telephone number    function telephonenumber(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];                return dp[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)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!


… [Trackback]
[…] Read More on that Topic: geeksforgeeks.org/telephone-number/ […]
… [Trackback]
[…] Find More to that Topic: geeksforgeeks.org/telephone-number/ […]