Telephone Number

In mathematics, the telephone numbers involution numbers are a sequence of integers 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find the nth telephone number.
#include <bits/stdc++.h>
using namespace std;
  
// return the nth telephone number
int telephonenumber(int n)
{
    // base step
    if (n == 0 || n == 1)
        return 1;
  
    // recursive step
    return telephonenumber(n - 1) + 
          (n - 1) * telephonenumber(n - 2);
}
  
// Driven Program
int main()
{
    int n = 6;
    cout << telephonenumber(n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.    

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to find the 
# nth telephone number.
  
# return the nth telephone number
def telephonenumber (n):
  
    # base step
    if n == 0 or n == 1:
        return 1
          
    # 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".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find 
// the nth telephone number
  
// return the nth 
// telephone number
function 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.
?>

chevron_right



Output :

76

Below is efficient implementation of finding the nth telephone number using Dynamic Programming:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP Program to find the nth telephone number.
#include <bits/stdc++.h>
using namespace std;
  
// return the nth telephone number
int 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 Program
int main()
{
    int n = 6;
    cout << telephonenumber(n) << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA Code to find nth Telephone Number
import 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.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code to find the
# nth telephone number.
  
# return the nth telephone number
def 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 Code
n = 6
print(telephonenumber(n))
  
# This code is contributed by "Sharad_Bhardwaj".

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Code to find nth Telephone Number
using 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.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to find 
// the nth telephone number.
  
// return the nth telephone number
function 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.
?>

chevron_right



Output :

76


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : vt_m

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.