📜  莱布尼兹谐波三角

📅  最后修改于: 2021-05-07 06:02:34             🧑  作者: Mango

莱布尼兹谐波三角是单位分数的三角形排列,其中最外面的对角线由行数的倒数组成,每个内部单元格是对角线上方和左侧的单元格减去左侧的单元格。换句话说, L(r,1)= 1 / r ,其中r是从1开始的行数,而c是从不大于r的行数,并且L(r,c)= L(r – 1,c – 1)– L(r,c – 1)

与帕斯卡三角形的关系
Pascal三角形中的每个条目都是上一行中两个条目的总和,Leibniz三角形中的每个条目都是其下一行中两个条目的总和。例如,在第五行中,条目(1/30)是第六行中两个(1/60)的总和。
正如可以使用二项式系数计算帕斯卡的三角形一样,莱布尼兹的也可以:
L(r, c)=\frac{1}{r*^{r-1}\textrm{C}_(_c-_1_)}
特性
如果将第n行的分母加起来,则结果将等于n.2 n-1 。例如,对于第三行,我们有3 + 6 + 3 = 12 = 3×22。
给定正整数n 。任务是打印高度为n的莱布尼兹谐波三角
例子:

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

下面是基于上述与Pascal三角形的关系来打印高度为n的莱布尼兹谐波三角形的实现。

C++
// CPP Program to print Leibniz Harmonic Triangle
#include 
using namespace std;
 
// Print Leibniz Harmonic Triangle
void 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 Program
int main()
{
    int n = 4;
    LeibnizHarmonicTriangle(n);
    return 0;
}


Java
// Java Program to print
// Leibniz Harmonic Triangle
import 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
# Triangle
def 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 Code
LeibnizHarmonicTriangle(4);
 
# This code is contributed
# by mits.


C#
// C# Program to print Leibniz Harmonic Triangle
using 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


Javascript


输出:

1/1 
1/2 1/2 
1/3 1/6 1/3 
1/4 1/12 1/12 1/4