Skip to content
Related Articles

Related Articles

Telephone Number
  • Difficulty Level : Medium
  • Last Updated : 22 May, 2018

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++




// 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;
}


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


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



Output :

76

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 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;
}


Java




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


Python3




# 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".


C#




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


PHP




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



Output :

76

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :