The Leibniz harmonic triangle is a triangular arrangement of unit fractions in which the outermost diagonals consist of the reciprocals of the row numbers and each inner cell is the cell diagonally above and to the left minus the cell to the left. To put it algebraically, L(r, 1) = 1/r, where r is the number of the row, starting from 1, and c is the number, never more than r and L(r, c) = L(r – 1, c – 1) – L(r, c – 1).
Relation with pascal’s triangleÂ
Whereas each entry in Pascal’s triangle is the sum of the two entries in the above row, each entry in the Leibniz triangle is the sum of the two entries in the row below it. For example, in the 5th row, the entry (1/30) is the sum of the two (1/60)s in the 6th row.Â
Just as Pascal’s triangle can be computed by using binomial coefficients, so can Leibniz’s:Â
PropertiesÂ
If one takes the denominators of the nth row and adds them, then the result will equal n.2n-1. For example, for the 3rd row, we have 3 + 6 + 3 = 12 = 3 × 22.
Given a positive integer n. The task is to print Leibniz harmonic triangle of height n.
Examples:Â Â
Input : n = 4 Output : 1 1/2 1/2 1/3 1/6 1/3 1/4 1/12 1/12 1/4 Input : n = 3 Output : 1 1/2 1/2 1/3 1/6 1/3
Below is the implementation of printing Leibniz harmonic triangle of height n based on above relation with Pascal triangle. Â
C++
// CPP Program to print Leibniz Harmonic Triangle#include <bits/stdc++.h>using namespace std;Â
// Print Leibniz Harmonic Trianglevoid LeibnizHarmonicTriangle(int n){Â Â Â Â int C[n + 1][n + 1];Â
    // Calculate value of Binomial Coefficient in    // bottom up manner    for (int i = 0; i <= n; i++) {        for (int j = 0; j <= min(i, n); j++) {Â
            // Base Cases            if (j == 0 || j == i)                C[i][j] = 1;Â
            // Calculate value using previously             // stored values            else                C[i][j] = C[i - 1][j - 1] + C[i - 1][j];        }    }Â
    // printing Leibniz Harmonic Triangle    for (int i = 1; i <= n; i++) {        for (int j = 1; j <= i; j++)            cout << "1/" << i * C[i - 1][j - 1] << " ";Â
        cout << endl;    }}Â
// Driven Programint main(){Â Â Â Â int n = 4;Â Â Â Â LeibnizHarmonicTriangle(n);Â Â Â Â return 0;} |
Java
// Java Program to print // Leibniz Harmonic Triangleimport java.io.*;import java.math.*;Â
class GFG {         // Print Leibniz Harmonic Triangle    static void LeibnizHarmonicTriangle(int n)    {        int C[][] = new int[n + 1][n + 1];             // Calculate value of Binomial         // Coefficient in bottom up manner        for (int i = 0; i <= n; i++) {            for (int j = 0; j <= Math.min(i, n);                                           j++) {                     // Base Cases                if (j == 0 || j == i)                    C[i][j] = 1;                     // Calculate value using                // previously stored values                else                    C[i][j] = C[i - 1][j - 1] +                               C[i - 1][j];            }        }             // printing Leibniz Harmonic Triangle        for (int i = 1; i <= n; i++) {            for (int j = 1; j <= i; j++)                System.out.print("1/" + i * C[i - 1][j - 1]                                           + " ");                 System.out.println();        }    }         // Driven Program    public static void main(String args[])    {        int n = 4;        LeibnizHarmonicTriangle(n);    }}Â
// This code is contributed by Nikita Tiwari |
Python3
# Python3 Program to print # Leibniz Harmonic TriangleÂ
# Print Leibniz Harmonic # Triangledef LeibnizHarmonicTriangle(n):    C = [[0 for x in range(n + 1)]             for y in range(n + 1)];                 # Calculate value of Binomial     # Coefficient in bottom up manner    for i in range(0, n + 1):        for j in range(0, min(i, n) + 1):                         # Base Cases            if (j == 0 or j == i):                C[i][j] = 1;                             # Calculate value using             # previously stored values            else:                C[i][j] = (C[i - 1][j - 1] +                           C[i - 1][j]);                               # printing Leibniz     # Harmonic Triangle    for i in range(1, n + 1):        for j in range(1, i + 1):            print("1/", end = "");            print(i * C[i - 1][j - 1],                            end = " ");        print();Â
# Driver CodeLeibnizHarmonicTriangle(4);Â
# This code is contributed# by mits. |
C#
// C# Program to print Leibniz Harmonic Triangleusing System;Â
class GFG {         // Print Leibniz Harmonic Triangle    static void LeibnizHarmonicTriangle(int n)    {        int [,]C = new int[n + 1,n + 1];             // Calculate value of Binomial         // Coefficient in bottom up manner        for (int i = 0; i <= n; i++) {            for (int j = 0; j <= Math.Min(i, n);                                         j++) {                     // Base Cases                if (j == 0 || j == i)                    C[i,j] = 1;                     // Calculate value using                // previously stored values                else                    C[i,j] = C[i - 1,j - 1] +                             C[i - 1,j];            }        }             // printing Leibniz Harmonic Triangle        for (int i = 1; i <= n; i++) {            for (int j = 1; j <= i; j++)                Console.Write("1/" + i * C[i - 1,j - 1]                                         + " ");             Console.WriteLine();        }    }         // Driven Program    public static void Main()    {        int n = 4;                 LeibnizHarmonicTriangle(n);    }}Â
// This code is contributed by vt_m. |
PHP
<?php// PHP Program to print // Leibniz Harmonic TriangleÂ
// Print Leibniz Harmonic Trianglefunction LeibnizHarmonicTriangle($n){         // Calculate value of     // Binomial Coefficient in    // bottom up manner    for ($i = 0; $i <= $n; $i++)     {        for ($j = 0; $j <= min($i, $n); $j++)        {Â
            // Base Cases            if ($j == 0 || $j == $i)                $C[$i][$j] = 1;Â
            // Calculate value             // using previously             // stored values            else                $C[$i][$j] = $C[$i - 1][$j - 1] +                                  $C[$i - 1][$j];        }    }Â
    // printing Leibniz    // Harmonic Triangle    for ($i = 1; $i <= $n; $i++)     {        for ($j = 1; $j <= $i; $j++)            echo "1/", $i * $C[$i - 1][$j - 1], " ";Â
        echo "\n";    }}Â
    // Driver Code    $n = 4;    LeibnizHarmonicTriangle($n);     // This code is contributed by aj_36?> |
Javascript
<script>Â
// JavaScript Program to print // Leibniz Harmonic TriangleÂ
    // Print Leibniz Harmonic Triangle    function LeibnizHarmonicTriangle(n)    {        let C = new Array(n + 1);                 // Loop to create 2D array using 1D array        for (let i = 0; i < C.length; i++) {                C[i] = new Array(2);        }               // Calculate value of Binomial         // Coefficient in bottom up manner        for (let i = 0; i <= n; i++) {            for (let j = 0; j <= Math.min(i, n);                                           j++) {                       // Base Cases                if (j == 0 || j == i)                    C[i][j] = 1;                       // Calculate value using                // previously stored values                else                    C[i][j] = C[i - 1][j - 1] +                               C[i - 1][j];            }        }               // printing Leibniz Harmonic Triangle        for (let i = 1; i <= n; i++)        {            for (let j = 1; j <= i; j++)                document.write("1/" + i * C[i - 1][j - 1]                                           + " ");                   document.write("<br/>");        }    }Â
// Driver Code        let n = 4;        LeibnizHarmonicTriangle(n);     // This code is contributed by avijitmondal1998.</script> |
Output:Â
1/1 1/2 1/2 1/3 1/6 1/3 1/4 1/12 1/12 1/4
Time complexity: O(n2) for given n
Auxiliary Space: O(n2)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

