Monday, April 7, 2025
Google search engine
HomeData Modelling & AITelephone Number

Telephone Number

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


Javascript




<script>
 
 
// Javascript Program to find
// the nth telephone number.
 
// return the nth telephone number
function telephonenumber(n)
{
    // base step
    if (n == 0 || n == 1)
        return 1;
 
    // recursive step
    return telephonenumber(n - 1) +
          (n - 1) * telephonenumber(n - 2);
}
 
// Driven Program
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++




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


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)
 

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!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments