Display leibniz harmonic Triangle
In this article, we will discuss the Leibniz Harmonic Triangle, a fascinating mathematical construct, and how to implement a program to display it using C programming language.
Introduction
The Leibniz Harmonic Triangle is a triangular arrangement of numbers where each number in a row is the harmonic mean of the two numbers directly above it in the previous row. The harmonic mean of two numbers is the reciprocal of the arithmetic mean of their reciprocals. This triangle is named after Gottfried Wilhelm Leibniz, a German mathematician and philosopher.
Problem Statement
Given a positive integer representing the size of the triangle, we need to write a program that displays the Leibniz Harmonic Triangle.
For example, if the size is 5, the program should display:
1 1/2 1/2 1/3 1/6 1/3 1/4 1/12 1/12 1/4 1/5 1/20 1/30 1/20 1/5
The triangle is symmetric, and the numbers are displayed in a right-aligned triangular pattern.
Algorithm and Pseudocode
Let's discuss the algorithm to generate and display the Leibniz Harmonic Triangle:
- Start by defining a function named 'show' that takes an integer array 'result' and its size as parameters.
- In the 'show' function, iterate through each row 'i' from 1 to the given size.
- For each row, iterate through the columns 'j' from 'i' to 'size' and print a tab character to create the triangular pattern.
- Inside the second loop, iterate through the columns 'j' from 1 to 'i'.
- In the inner loop, check if 'i' is equal to 1 and 'j' is equal to 1. If true, print 1 as the first element of the triangle.
- If the above condition is false, calculate the value of the element using the formula: 1 / (result[i-1][j-1] * i).
- Print the calculated value followed by a tab character to maintain the triangular pattern.
- After printing all elements of a row, move to the next line.
- Define another function named 'harmonic_triangle' that takes the size of the triangle as a parameter.
- In the 'harmonic_triangle' function, declare a 2D array named 'result' with dimensions (size+1) × (size+1).
- Initialize the base case of the triangle by setting the elements where 'j' is equal to 0 or 'j' is equal to 'i' to 1.
- For the remaining elements, calculate the value by adding the two elements from the previous row and column, i.e., result[i-1][j-1] + result[i-1][j].
- Call the 'show' function, passing the 'result' array and the size of the triangle.
- In the 'main' function, test the program by calling the 'harmonic_triangle' function with different sizes.
Here is the pseudocode representation of the algorithm:
function show(result, size):
for i from 1 to size:
for j from i to size:
print tab character
for j from 1 to i:
if i = 1 and j = 1:
print 1
else:
print 1 / (result[i-1][j-1] * i)
print tab character
move to the next line
function harmonic_triangle(size):
result[size+1][size+1]
for i from 0 to size:
for j from 0 to i:
if j = i or j = 0:
result[i][j] = 1
else:
result[i][j] = result[i-1][j-1] + result[i-1][j]
show(result, size)
main:
harmonic_triangle(5)
harmonic_triangle(7)
Code Solution
Here given code implementation process.
//C Program
//Display leibniz harmonic Triangle
#include <stdio.h>
//Display result of leibniz harmonic triangle
void show(int *result, int size)
{
int i = 0, j = 0;
for (i = 1; i <= size; ++i)
{
for (j = i; j < size; ++j)
{
printf("\t");
}
for (j = 1; j <= i; ++j)
{
if (i == 1 && j == 1)
{
printf("%d", 1);
}
else
{
//in 2d array result[i-1][j-1]*i
printf("1/%d\t\t", *((result + (i - 1) *(size + 1) + (j - 1))) *i);
}
}
printf("\n");
}
}
void harmonic_triangle(int size)
{
printf("\n ROW SIZE : %d\n", size);
//Create a 2d array, which is store the result
int result[size + 1][size + 1];
int i = 0, j = 0;
for (i = 0; i <= size; ++i)
{
for (j = 0; j <= i; ++j)
{
if (j == i || j == 0)
{
//Simple base case when need to set 1
result[i][j] = 1;
}
else
{
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
}
}
}
show((int *) result, size);
}
int main()
{
//Test Cases
harmonic_triangle(5);
harmonic_triangle(7);
return 0;
}
Output
ROW SIZE : 5
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
ROW SIZE : 7
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
1/6 1/30 1/60 1/60 1/30 1/6
1/7 1/42 1/105 1/140 1/105 1/42 1/7
/*
Java Program
Display leibniz harmonic Triangle
*/
class MyPattern
{
//Display result of leibniz harmonic triangle
public void show(int[][] result, int size)
{
int i = 0, j = 0;
for (i = 1; i <= size; ++i)
{
for (j = i; j < size; ++j)
{
System.out.print("\t");
}
for (j = 1; j <= i; ++j)
{
if (i == 1 && j == 1)
{
System.out.print("1");
}
else
{
System.out.print("1/" + result[i - 1][j - 1] * i + "\t\t");
}
}
System.out.print("\n");
}
}
public void harmonic_triangle(int size)
{
System.out.print("\n ROW SIZE : " + size + "\n");
//Create a 2d array, which is store the result
int result[][] = new int[size + 1][size + 1];
int i = 0, j = 0;
for (i = 0; i <= size; ++i)
{
for (j = 0; j <= i; ++j)
{
if (j == i || j == 0)
{
//Simple base case when need to set 1
result[i][j] = 1;
}
else
{
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
}
}
}
show(result, size);
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Cases
obj.harmonic_triangle(5);
obj.harmonic_triangle(7);
}
}
Output
ROW SIZE : 5
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
ROW SIZE : 7
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
1/6 1/30 1/60 1/60 1/30 1/6
1/7 1/42 1/105 1/140 1/105 1/42 1/7
/*
C++ Program
Display leibniz harmonic Triangle
*/
#include<iostream>
using namespace std;
class MyPattern
{
public:
//Display result of leibniz harmonic triangle
void show(int *result, int size)
{
int i = 0, j = 0;
for (i = 1; i <= size; ++i)
{
for (j = i; j < size; ++j)
{
cout << "\t";
}
for (j = 1; j <= i; ++j)
{
if (i == 1 && j == 1)
{
cout << "1";
}
else
{
cout << "1/" << *((result + (i - 1) *(size + 1) + (j - 1))) *i << "\t\t";
}
}
cout << "\n";
}
}
void harmonic_triangle(int size)
{
cout << "\n ROW SIZE : " << size << "\n";
//Create a 2d array, which is store the result
int result[size + 1][size + 1];
int i = 0, j = 0;
for (i = 0; i <= size; ++i)
{
for (j = 0; j <= i; ++j)
{
if (j == i || j == 0)
{
//Simple base case when need to set 1
result[i][j] = 1;
}
else
{
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
}
}
}
this->show((int*)result, size);
}
};
int main()
{
MyPattern obj = MyPattern();
//Test Cases
obj.harmonic_triangle(5);
obj.harmonic_triangle(7);
return 0;
}
Output
ROW SIZE : 5
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
ROW SIZE : 7
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
1/6 1/30 1/60 1/60 1/30 1/6
1/7 1/42 1/105 1/140 1/105 1/42 1/7
/*
C# Program
Display leibniz harmonic Triangle
*/
using System;
class MyPattern
{
//Display result of leibniz harmonic triangle
public void show(int[,] result, int size)
{
int i = 0, j = 0;
for (i = 1; i <= size; i++)
{
for (j = i; j < size; j++)
{
Console.Write("\t");
}
for (j = 1; j <= i; j++)
{
if (i == 1 && j == 1)
{
Console.Write("1");
}
else
{
Console.Write("1/" + result[i - 1,j - 1] * i + "\t\t");
}
}
Console.Write("\n");
}
}
public void harmonic_triangle(int size)
{
Console.Write("\n ROW SIZE : " + size + "\n");
int[,] result = new int[size + 1,size + 1];
int i = 0, j = 0;
for (i = 0; i <= size; i++)
{
for (j = 0; j <= i; j++)
{
if (j == i || j == 0)
{
//Simple base case when need to set 1
result[i,j] = 1;
}
else
{
result[i,j] = result[i - 1,j - 1] + result[i - 1,j];
}
}
}
show(result, size);
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
//Test Cases
obj.harmonic_triangle(5);
obj.harmonic_triangle(7);
}
}
Output
ROW SIZE : 5
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
ROW SIZE : 7
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
1/6 1/30 1/60 1/60 1/30 1/6
1/7 1/42 1/105 1/140 1/105 1/42 1/7
<?php
/*
Php Program
Display leibniz harmonic Triangle
*/
class MyPattern
{
//Display result of leibniz harmonic triangle
public function show( $result, $size)
{
$i = 0;
$j = 0;
for ($i = 1; $i <= $size; ++$i)
{
for ($j = $i; $j < $size; ++$j)
{
echo("\t");
}
for ($j = 1; $j <= $i; ++$j)
{
if ($i == 1 && $j == 1)
{
echo("1");
}
else
{
echo("1/". $result[$i - 1][$j - 1] * $i ."\t\t");
}
}
echo("\n");
}
}
public function harmonic_triangle($size)
{
echo("\n ROW SIZE : ". $size ."\n");
//Create a 2d array, which is store the result
$result = array_fill(0, $size + 1, array_fill(0, $size + 1, 0));
$i = 0;
$j = 0;
for ($i = 0; $i <= $size; ++$i)
{
for ($j = 0; $j <= $i; ++$j)
{
if ($j == $i || $j == 0)
{
//Simple base case when need to set 1
$result[$i][$j] = 1;
}
else
{
$result[$i][$j] = $result[$i - 1][$j - 1] + $result[$i - 1][$j];
}
}
}
$this->show($result, $size);
}
}
function main()
{
$obj = new MyPattern();
//Test Cases
$obj->harmonic_triangle(5);
$obj->harmonic_triangle(7);
}
main();
Output
ROW SIZE : 5
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
ROW SIZE : 7
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
1/6 1/30 1/60 1/60 1/30 1/6
1/7 1/42 1/105 1/140 1/105 1/42 1/7
/*
Node Js Program
Display leibniz harmonic Triangle
*/
class MyPattern
{
//Display result of leibniz harmonic triangle
show(result, size)
{
var i = 0;
var j = 0;
for (i = 1; i <= size; ++i)
{
for (j = i; j < size; ++j)
{
process.stdout.write("\t");
}
for (j = 1; j <= i; ++j)
{
if (i == 1 && j == 1)
{
process.stdout.write("1");
}
else
{
process.stdout.write("1/" + result[i - 1][j - 1] * i + "\t\t");
}
}
process.stdout.write("\n");
}
}
harmonic_triangle(size)
{
process.stdout.write("\n ROW SIZE : " + size + "\n");
//Create a 2d array, which is store the result
var result = Array(size + 1).fill(0).map(() => new Array(size + 1).fill(0));
var i = 0;
var j = 0;
for (i = 0; i <= size; ++i)
{
for (j = 0; j <= i; ++j)
{
if (j == i || j == 0)
{
//Simple base case when need to set 1
result[i][j] = 1;
}
else
{
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
}
}
}
this.show(result, size);
}
}
function main(args)
{
var obj = new MyPattern();
//Test Cases
obj.harmonic_triangle(5);
obj.harmonic_triangle(7);
}
main();
Output
ROW SIZE : 5
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
ROW SIZE : 7
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
1/6 1/30 1/60 1/60 1/30 1/6
1/7 1/42 1/105 1/140 1/105 1/42 1/7
# Python 3 Program
# Display leibniz harmonic Triangle
class MyPattern :
# Display result of leibniz harmonic triangle
def show(self, result, size) :
i = 1
j = 1
while (i <= size) :
j = i
while (j < size) :
print("\t", end = "")
j += 1
j = 1
while (j <= i) :
if (i == 1 and j == 1) :
print("1", end = "")
else :
print("1/", result[i - 1][j - 1] * i ,"\t\t", end = "")
j += 1
print("\n", end = "")
i += 1
def harmonic_triangle(self, size) :
print("\n ROW SIZE : ", size ,"\n", end = "")
result = [[0 for _ in range(size+1)] for _ in range(size+1)]
i = 0
j = 0
while (i <= size) :
j = 0
while (j <= i) :
if (j == i or j == 0) :
# Simple base case when need to set 1
result[i][j] = 1
else :
result[i][j] = result[i - 1][j - 1] + result[i - 1][j]
j += 1
i += 1
self.show(result, size)
def main() :
obj = MyPattern()
# Test Cases
obj.harmonic_triangle(5)
obj.harmonic_triangle(7)
if __name__ == "__main__": main()
Output
ROW SIZE : 5
1
1/ 2 1/ 2
1/ 3 1/ 6 1/ 3
1/ 4 1/ 12 1/ 12 1/ 4
1/ 5 1/ 20 1/ 30 1/ 20 1/ 5
ROW SIZE : 7
1
1/ 2 1/ 2
1/ 3 1/ 6 1/ 3
1/ 4 1/ 12 1/ 12 1/ 4
1/ 5 1/ 20 1/ 30 1/ 20 1/ 5
1/ 6 1/ 30 1/ 60 1/ 60 1/ 30 1/ 6
1/ 7 1/ 42 1/ 105 1/ 140 1/ 105 1/ 42 1/ 7
# Ruby Program
# Display leibniz harmonic Triangle
class MyPattern
# Display result of leibniz harmonic triangle
def show(result, size)
i = 1
j = 1
while (i <= size)
j = i
while (j < size)
print("\t")
j += 1
end
j = 1
while (j <= i)
if (i == 1 && j == 1)
print("1")
else
print("1/", result[i - 1][j - 1] * i ,"\t\t")
end
j += 1
end
print("\n")
i += 1
end
end
def harmonic_triangle(size)
print("\n ROW SIZE : ", size ,"\n")
result = Array.new(size + 1) {Array.new(size + 1) }
i = 0
j = 0
while (i <= size)
j = 0
while (j <= i)
if (j == i || j == 0)
# Simple base case when need to set 1
result[i][j] = 1
else
result[i][j] = result[i - 1][j - 1] + result[i - 1][j]
end
j += 1
end
i += 1
end
self.show(result, size)
end
end
def main()
obj = MyPattern.new()
# Test Cases
obj.harmonic_triangle(5)
obj.harmonic_triangle(7)
end
main()
Output
ROW SIZE : 5
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
ROW SIZE : 7
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
1/6 1/30 1/60 1/60 1/30 1/6
1/7 1/42 1/105 1/140 1/105 1/42 1/7
/*
Scala Program
Display leibniz harmonic Triangle
*/
class MyPattern
{
//Display result of leibniz harmonic triangle
def show(result: Array[Array[Int]], size: Int): Unit = {
var i: Int = 1;
var j: Int = 1;
while (i <= size)
{
j = i;
while (j < size)
{
print("\t");
j += 1;
}
j = 1;
while (j <= i)
{
if (i == 1 && j == 1)
{
print("1");
}
else
{
print("1/" + result(i - 1)(j - 1) * i + "\t\t");
}
j += 1;
}
print("\n");
i += 1;
}
}
def harmonic_triangle(size: Int): Unit = {
print("\n ROW SIZE : " + size + "\n");
var result: Array[Array[Int]] = Array.fill[Int](size + 1,size+1)(0);
var i: Int = 0;
var j: Int = 0;
while (i <= size)
{
j = 0;
while (j <= i)
{
if (j == i || j == 0)
{
//Simple base case when need to set 1
result(i)(j) = 1;
}
else
{
result(i)(j) = result(i - 1)(j - 1) + result(i - 1)(j);
}
j += 1;
}
i += 1;
}
show(result, size);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
//Test Cases
obj.harmonic_triangle(5);
obj.harmonic_triangle(7);
}
}
Output
ROW SIZE : 5
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
ROW SIZE : 7
1
1/2 1/2
1/3 1/6 1/3
1/4 1/12 1/12 1/4
1/5 1/20 1/30 1/20 1/5
1/6 1/30 1/60 1/60 1/30 1/6
1/7 1/42 1/105 1/140 1/105 1/42 1/7
/*
Swift Program
Display leibniz harmonic Triangle
*/
class MyPattern
{
//Display result of leibniz harmonic triangle
func show(_ result: [
[Int]
], _ size: Int)
{
var i: Int = 1;
var j: Int = 1;
while (i <= size)
{
j = i;
while (j < size)
{
print("\t", terminator: "");
j += 1;
}
j = 1;
while (j <= i)
{
if (i == 1 && j == 1)
{
print("1", terminator: "");
}
else
{
print("1/", result[i - 1][j - 1] * i , terminator: "\t\t");
}
j += 1;
}
print("\n", terminator: "");
i += 1;
}
}
func harmonic_triangle(_ size: Int)
{
print("\n ROW SIZE : ", size ,"\n", terminator: "");
var result: [
[Int]
] = Array(repeating: Array(repeating: 0, count: size + 1), count: size + 1);
var i: Int = 0;
var j: Int = 0;
while (i <= size)
{
j = 0;
while (j <= i)
{
if (j == i || j == 0)
{
//Simple base case when need to set 1
result[i][j] = 1;
}
else
{
result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
}
j += 1;
}
i += 1;
}
self.show(result, size);
}
}
func main()
{
let obj: MyPattern = MyPattern();
//Test Cases
obj.harmonic_triangle(5);
obj.harmonic_triangle(7);
}
main();
Output
ROW SIZE : 5
1
1/ 2 1/ 2
1/ 3 1/ 6 1/ 3
1/ 4 1/ 12 1/ 12 1/ 4
1/ 5 1/ 20 1/ 30 1/ 20 1/ 5
ROW SIZE : 7
1
1/ 2 1/ 2
1/ 3 1/ 6 1/ 3
1/ 4 1/ 12 1/ 12 1/ 4
1/ 5 1/ 20 1/ 30 1/ 20 1/ 5
1/ 6 1/ 30 1/ 60 1/ 60 1/ 30 1/ 6
1/ 7 1/ 42 1/ 105 1/ 140 1/ 105 1/ 42 1/ 7
Resultant Output Explanation
Let's analyze the output for the given test cases:
For the test case with a size of 5:
1 1/2 1/2 1/3 1/6 1/3 1/4 1/12 1/12 1/4 1/5 1/20 1/30 1/20 1/5
Each row represents a row of the Leibniz Harmonic Triangle. The first row consists of a single element, which is 1.
In the second row, we have two elements, each being the harmonic mean of the two elements above it in the previous row (1/2 and 1/2).
Similarly, in the third row, each element is the harmonic mean of the two elements directly above it (1/3 and 1/6, 1/6 and 1/3).
This pattern continues for the remaining rows.
For the test case with a size of 7:
1 1/2 1/2 1/3 1/6 1/3 1/4 1/12 1/12 1/4 1/5 1/20 1/30 1/20 1/5 1/6 1/30 1/60 1/60 1/30 1/6 1/7 1/42 1/105 1/140 1/105 1/42 1/7
The pattern is similar to the previous example, but the triangle is larger. Each element is the harmonic mean of the two elements directly above it.
Time Complexity of the Code
The time complexity of the code can be analyzed as follows:
- The 'harmonic_triangle' function has two nested loops that iterate through each row and column of the result array. The time complexity of these loops is O(size^2).
- The 'show' function has two nested loops that iterate through each row and column of the result array. The time complexity of these loops is also O(size^2).
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment