Open In App

Program to print tetrahedral numbers upto Nth term

Prerequisites: 
 


Given a value n, and the task is to print tetrahedral number series up to nth term.
Examples: 
 

Input: 5
Output: 1  4  10  20  35

Input: 10
Output: 1  4  10  20  35  56  84  120  165  220 


 


Method 1: Using Triangular Number series: 
This problem can be easily solved with the fact that Nth Tetrahedral Number is equal to the sum of first N Triangular Numbers.
Let’s have a look on Series of triangular and tetrahedral Numbers. 
 

To print series upto 5th term:
Triangular Numbers  =  1        3          6             10                  15
Tetrahedral numbers =  1        4          10            20                  35 
                 i.e  (1)    (1 + 3)  (1 + 3 + 6)  (1 + 3 + 6 + 10)  (1 + 3 + 6 + 10 + 35)   


Calculate Nth Triangular number using formula 
So, print the tetrahedral numbers series by generating triangular numbers and adding it with the sum of all previously generated triangular numbers. 
Below is the implementation of the above approach:
 

// C++ program to generate tetrahedral
// number series
#include <bits/stdc++.h>
using namespace std;
 
// function to generate nth triangular
// number
long findTriangularNumber(int n)
{
    return (n * (n + 1)) / 2;
}
 
// function to print tetrahedral number
// series up to n
void printSeries(int n)
{
    // Initialize prev as 0. It stores
    // the sum of all previously generated
    // triangular number
    int prev = 0;
    int curr;
 
    // Loop to print series
    for (int i = 1; i <= n; i++)
    {
        // Find ith triangular number
        curr = findTriangularNumber(i);
 
        // Add ith triangular number to
        // sum of all previously generated
        // triangular number to get ith
        // tetrahedral number
        curr = curr + prev;
        cout << curr << " ";
 
        // Update sum of all previously
        // generated triangular number
        prev = curr;
    }
}
 
// Driver code
int main()
{
    int n = 10;
     
    // function call to print series
    printSeries(n);
     
    return 0;
}

                    
// Java program to generate tetrahedral
// number series
import java.io.*;
 
class GFG {
     
    // function to generate nth triangular
    // number
    static long findTriangularNumber(int n)
    {
        return (n * (n + 1)) / 2;
    }
     
    // function to print tetrahedral number
    // series up to n
    static void printSeries(int n)
    {
        // Initialize prev as 0. It store
        // the sum of all previously generated
        // triangular number
        long prev = 0;
        long curr;
     
        // Loop to print series
        for (int i = 1; i <= n; i++)
        {
            // Find ithh triangular number
            curr = findTriangularNumber(i);
     
            // Add ith triangular number to
            // sum of all previously generated
            // triangular number to get ith
            // tetrahedral number
            curr = curr + prev;
            System.out.print(curr + " ");
     
            // Update sum of all previously
            // generated triangular number
            prev = curr;
        }
    }
 
    // Driver code
    public static void main (String[] args)
    {
        int n = 10;
     
        // function call to print series
        printSeries(n);
    }
}

                    
# Python3 program to generate
# tetrahedral number series
 
# function to generate nth
# triangular number
def findTriangularNumber(n):
    return (n * (n + 1)) / 2
 
# function to print tetrahedral
# number series up to n
def printSeries(n):
 
    # Initialize prev as 0.
    # It stores the sum of all
    # previously generated
    # triangular number
    prev = 0
 
    # Loop to print series
    for i in range(1, n+1):
     
        # Find ith triangular number
        curr = findTriangularNumber(i)
 
        # Add ith triangular number
        # to sum of all previously
        # generated triangular number
        # to get ith tetrahedral number
        curr = int(curr + prev)
        print(curr, end = ' ')
 
        # Update sum of all previously
        # generated triangular number
        prev = curr
 
# Driver code
n = 10
     
# function call to
# print series
printSeries(n)
 
# This code is contributed by Mahadev.

                    
// C# program to generate tetrahedral
// number series
using System;
 
public class GFG{
     
    // function to generate nth triangular
    // number
    static long findTriangularNumber(int n)
    {
        return (n * (n + 1)) / 2;
    }
     
    // function to print tetrahedral number
    // series up to n
    static void printSeries(int n)
    {
        // Initialize prev as 0. It store
        // the sum of all previously generated
        // triangular number
        long prev = 0;
        long curr;
     
        // Loop to print series
        for (int i = 1; i <= n; i++)
        {
            // Find ithh triangular number
            curr = findTriangularNumber(i);
     
            // Add ith triangular number to
            // sum of all previously generated
            // triangular number to get ith
            // tetrahedral number
            curr = curr + prev;
            Console.Write(curr + " ");
     
            // Update sum of all previously
            // generated triangular number
            prev = curr;
        }
    }
 
    // Driver code
    static public void Main ()
    {
        int n = 10;
     
        // function call to print series
        printSeries(n);
    }
}

                    
<?php
// PHP program to generate tetrahedral
// number series
 
// function to generate nth triangular
// number
function findTriangularNumber($n)
{
    return ($n * ($n + 1)) / 2;
}
 
// function to print tetrahedral number
// series up to n
function printSeries($n)
{
    // Initialize prev as 0. It store
    // the sum of all previously generated
    // triangular number
    $prev = 0;
    $curr;
 
    // Loop to print series
    for ($i = 1; $i <= $n; $i++)
    {
        // Find ithh triangular number
        $curr = findTriangularNumber($i);
 
        // Add ith triangular number to
        // sum of all previously generated
        // triangular number to get ith
        // tetrahedral number
        $curr = $curr + $prev;
        echo($curr . " ");
 
        // Update sum of all previously
        // generated triangular number
        $prev = $curr;
    }
}
 
// Driver code
$n = 10;
     
// function call to print series
printSeries($n);
?>

                    
<script>
// Javascript program to generate tetrahedral
// number series
 
// function to generate nth triangular
// number
function findTriangularNumber(n)
{
    return (n * (n + 1)) / 2;
}
 
// function to print tetrahedral number
// series up to n
function printSeries(n)
{
 
    // Initialize prev as 0. It stores
    // the sum of all previously generated
    // triangular number
    var prev = 0;
    var curr;
 
    // Loop to print series
    for (var i = 1; i <= n; i++)
    {
        // Find ith triangular number
        curr = findTriangularNumber(i);
 
        // Add ith triangular number to
        // sum of all previously generated
        // triangular number to get ith
        // tetrahedral number
        curr = curr + prev;
        document.write( curr + " ");
 
        // Update sum of all previously
        // generated triangular number
        prev = curr;
    }
}
 
// Driver code
var n = 10;
 
// function call to print series
printSeries(n);
 
// This code is contributed by itsok.
</script>

                    

Output: 
1 4 10 20 35 56 84 120 165 220

 

Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.


Method 2: Using Tetrahedral Number Formula: 
Formula to find nth tetrahedral number:
Below is the required implementation:
 

// C++ program to generate series of
// tetrahedral numbers
#include <bits/stdc++.h>
using namespace std;
 
// function to print tetrahedral
// number series up to n
void printSeries(int n)
{
 
    // loop to print series
    for (int i = 1; i <= n; i++)
    {
        // Calculate and print ith
        // tetrahedral number
                int num = i * (i + 1) * (i + 2) / 6;
        cout << num << " ";
    }
}
 
// Driver code
int main()
{
    int n = 10;
     
    // function call to print series
    printSeries(n);
     
    return 0;
}

                    
// Java program to generate series of
// tetrahedral numbers
import java.io.*;
 
class GFG {
     
    // function to print tetrahedral
    // number series up to n
    static void printSeries(int n)
    {
     
        // loop to print series
        for (int i = 1; i <= n; i++)
        {
            // Calculate and print ith
            // tetrahedral number
            int num = i * (i + 1) * (i + 2) / 6;
             
            System.out.print(num + " ");
        }
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 10;
     
        // function call to print series
        printSeries(n);
    }
}

                    
# Python3 code to print tetrahedral
# numbers series up to n
  
# function to print tetrahedral series up to n
def printSeries(n):
  
    # loop to print series
    for i in range(1, n + 1):
         
        # Calculate and print ith
        # Tetrahedral number
        num = i * (i + 1) * (i + 2) // 6
         
        print(num, end =' ')
                   
# Driver code
n = 10
 
# function call to print series
printSeries(n)

                    
// C# program to generate series of
// tetrahedral numbers
using System;
 
public class GFG{
     
    // function to print tetrahedral
    // number series up to n
    static void printSeries(int n)
    {
     
        // loop to print series
        for (int i = 1; i <= n; i++)
        {
            // Calculate and print ith
            // tetrahedral number
            int num = i * (i + 1) * (i + 2) / 6;
             
            Console.Write(num + " ");
        }
    }
     
    // Driver code
    static public void Main ()
    {
        int n = 10;
     
        // function call to print series
        printSeries(n);
    }
}

                    
<?php
// PHP program to generate series of
// tetrahedral numbers
 
// function to print tetrahedral
// number series up to n
function printSeries($n)
{
 
    // loop to print series
    for ($i = 1; $i <= $n; $i++)
    {
        // Calculate and print ith
        // tetrahedral number
        $num = $i * ($i + 1) * ($i + 2) / 6;
        echo ($num . " ");
    }
}
 
// Driver code
$n = 10;
     
// function call to print series
printSeries($n);
 
?>

                    
<script>
 
// Javascript program to generate series of
// tetrahedral numbers
 
    // function to print tetrahedral
    // number series up to n
    function printSeries(n)
    {
      let i;
       
        // loop to print series
        for (i = 1; i <= n; i++)
        {
            // Calculate and print ith
            // tetrahedral number
            let num = i * (i + 1) * ((i + 2) / 6);
               
            document.write(num + " ");
        }
    }
 
// driver program
     
        let n = 10;
       
        // function call to print series
        printSeries(n);
  
 // This code is contributed by susmitakundugoaldanga.
</script>

                    

Output: 
1 4 10 20 35 56 84 120 165 220

 

Time Complexity: O(n), where n represents the given integer.
Auxiliary Space: O(1), no extra space is required, so it is a constant.


Article Tags :