Skip to main content

Display Trinomial triangle

Trinomial triangle is a type of number pattern where the numbers in each row are the sum of three consecutive terms in the previous row. It is a generalization of the well-known Pascal's triangle, where each number in a row is the sum of the two numbers above it.

The first few rows of the trinomial triangle are as follows:

				1
			1	1	1
		1	2	3	2	1
	1	3	6	7	6	3	1
1	4	10	16	19	16	10	4	1

As you can see, the first row is just the number 1. The second row consists of three 1's. In the third row, the first and last numbers are 1, and the middle number is the sum of the two 1's in the previous row. The pattern continues in the same way for each subsequent row.

Here given code implementation process.

//C Program 
//Display Trinomial triangle
#include <stdio.h>

#include <stdlib.h>

int sum_result(int result[], int row, int col, int size)
{
	int ans = 0;
	//Check boundary condition
	if (row >= 0 && row < size && col >= 0 && col < (size * 2))
	{
		if (col - 1 >= 0)
		{
			//When fill result column is greater than zero
			ans += result[(row) * (size * 2) + (col - 1)];
		}
		//get the result of just above element of resultant matrix
		ans += result[(row) * (size * 2) + (col)];
		if (col + 1 < (size * 2))
		{
			ans += result[(row) * (size * 2) + (col + 1)];
		}
	}
	return ans;
}
//Display result of trinomial triangle
void show(int result[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		for (int j = 0; j < size * 2; ++j)
		{
			if (result[(i * (size * 2)) + j] != 0)
			{
				printf("%d\t", result[(i * (size * 2)) + j]);
			}
			else if (j != 0)
			{
				printf("\t");
			}
		}
		printf("\n");
	}
}
//Set default value of resultant array
void set_default(int result[], int size)
{
	for (int i = 0; i < size; ++i)
	{
		result[i] = 0;
	}
}
void trinomial_triangle(int size)
{
	printf("\n ROW  SIZE : %d\n", size);
	// Number of spaces are required to store trinomial triangle element
	int capacity = size * (size * 2);
	// Used to store result of trinomial triangle
	int result[capacity];
	int col = (size * 2) / 2;
	int counter = 1;
	set_default(result, capacity);
	//Set the first row element
	result[col] = 1;
	for (int i = 1; i < size; ++i)
	{
		//This is indicate number of valid operation in i-th row
		counter = counter + 2;
		col = col - 1;
		for (int j = 0; j < counter && j + col < size * 2; ++j)
		{
			//Store the result of top three closest element
			result[(i * (size * 2)) + col + j] = sum_result(result, i - 1, col + j, size);
		}
	}
	show(result, size);
}
int main()
{
	//Test Cases
	trinomial_triangle(5);
	trinomial_triangle(7);
	return 0;
}

Output

 ROW  SIZE : 5
				1
			1	1	1
		1	2	3	2	1
	1	3	6	7	6	3	1
1	4	10	16	19	16	10	4	1

 ROW  SIZE : 7
						1
					1	1	1
				1	2	3	2	1
			1	3	6	7	6	3	1
		1	4	10	16	19	16	10	4	1
	1	5	15	30	45	51	45	30	15	5	1
1	6	21	50	90	126	141	126	90	50	21	6	1
// Java Program
// Display Trinomial Triangle
class MyPattern
{
	public int sum_result(int[] result, int row, int col, int size)
	{
		int ans = 0;
		//Check boundary condition
		if (row >= 0 && row < size && col >= 0 && col < (size * 2))
		{
			if (col - 1 >= 0)
			{
				//When fill result column is greater than zero
				ans += result[(row) * (size * 2) + (col - 1)];
			}
			//get the result of just above element of resultant matrix
			ans += result[(row) * (size * 2) + (col)];
			if (col + 1 < (size * 2))
			{
				ans += result[(row) * (size * 2) + (col + 1)];
			}
		}
		return ans;
	}
	//Display result of trinomial triangle
	public void show(int[] result, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			for (int j = 0; j < size * 2; ++j)
			{
				if (result[(i * (size * 2)) + j] != 0)
				{
					System.out.print("" + result[(i * (size * 2)) + j] + "\t");
				}
				else if (j != 0)
				{
					System.out.print("\t");
				}
			}
			System.out.print("\n");
		}
	}
	//Set default value of resultant array
	public void set_default(int[] result, int size)
	{
		for (int i = 0; i < size; ++i)
		{
			result[i] = 0;
		}
	}
	public void trinomial_triangle(int size)
	{
		System.out.print("\n ROW SIZE : " + size + "\n");
		// Number of spaces are required to store trinomial triangle element
		int capacity = size * (size * 2);
		// Used to store result of trinomial triangle
		int[] result = new int[capacity];
		int col = (size * 2) / 2;
		int counter = 1;
		set_default(result, capacity);
		//Set the first row element
		result[col] = 1;
		for (int i = 1; i < size; ++i)
		{
			//This is indicate number of valid operation in i-th row
			counter = counter + 2;
			col = col - 1;
			for (int j = 0; j < counter && j + col < size * 2; ++j)
			{
				//Store the result of top three closest element
				result[(i * (size * 2)) + col + j] = sum_result(result, i - 1, col + j, size);
			}
		}
		show(result, size);
	}
	public static void main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.trinomial_triangle(5);
		obj.trinomial_triangle(7);
	}
}

Output

 ROW SIZE : 5
				1
			1	1	1
		1	2	3	2	1
	1	3	6	7	6	3	1
1	4	10	16	19	16	10	4	1

 ROW SIZE : 7
						1
					1	1	1
				1	2	3	2	1
			1	3	6	7	6	3	1
		1	4	10	16	19	16	10	4	1
	1	5	15	30	45	51	45	30	15	5	1
1	6	21	50	90	126	141	126	90	50	21	6	1
// C++ Program
// Display Trinomial Triangle
#include<iostream>

using namespace std;
class MyPattern
{
	public: int sum_result(int result[], int row, int col, int size)
	{
		int ans = 0;
		//Check boundary condition
		if (row >= 0 && row < size && col >= 0 && col < (size *2))
		{
			if (col - 1 >= 0)
			{
				//When fill result column is greater than zero
				ans += result[(row) *(size *2) + (col - 1)];
			}
			//get the result of just above element of resultant matrix
			ans += result[(row) *(size *2) + (col)];
			if (col + 1 < (size *2))
			{
				ans += result[(row) *(size *2) + (col + 1)];
			}
		}
		return ans;
	}
	//Display result of trinomial triangle
	void show(int result[], int size)
	{
		for (int i = 0; i < size; ++i)
		{
			for (int j = 0; j < size *2; ++j)
			{
				if (result[(i *(size *2)) + j] != 0)
				{
					cout << "" << result[(i *(size *2)) + j] << "\t";
				}
				else if (j != 0)
				{
					cout << "\t";
				}
			}
			cout << "\n";
		}
	}
	//Set default value of resultant array
	void set_default(int result[], int size)
	{
		for (int i = 0; i < size; ++i)
		{
			result[i] = 0;
		}
	}
	void trinomial_triangle(int size)
	{
		cout << "\n ROW SIZE : " << size << "\n";
		// Number of spaces are required to store trinomial triangle element
		int capacity = size *(size *2);
		int *result = new int[capacity];
		int col = (size *2) / 2;
		int counter = 1;
		this->set_default(result, capacity);
		//Set the first row element
		result[col] = 1;
		for (int i = 1; i < size; ++i)
		{
			//This is indicate number of valid operation in i-th row
			counter = counter + 2;
			col = col - 1;
			for (int j = 0; j < counter && j + col < size *2; ++j)
			{
				//Store the result of top three closest element
				result[(i *(size *2)) + col + j] = this->sum_result(result, i - 1, col + j, size);
			}
		}
		this->show(result, size);
	}
};
int main()
{
	MyPattern obj =  MyPattern();
	//Test Cases
	obj.trinomial_triangle(5);
	obj.trinomial_triangle(7);
	return 0;
}

Output

 ROW SIZE : 5
				1
			1	1	1
		1	2	3	2	1
	1	3	6	7	6	3	1
1	4	10	16	19	16	10	4	1

 ROW SIZE : 7
						1
					1	1	1
				1	2	3	2	1
			1	3	6	7	6	3	1
		1	4	10	16	19	16	10	4	1
	1	5	15	30	45	51	45	30	15	5	1
1	6	21	50	90	126	141	126	90	50	21	6	1
// C# Program
// Display Trinomial Triangle
using System;
class MyPattern
{
	public int sum_result(int[] result, int row, int col, int size)
	{
		int ans = 0;
		//Check boundary condition
		if (row >= 0 && row < size && col >= 0 && col < (size * 2))
		{
			if (col - 1 >= 0)
			{
				//When fill result column is greater than zero
				ans += result[(row) * (size * 2) + (col - 1)];
			}
			//get the result of just above element of resultant matrix
			ans += result[(row) * (size * 2) + (col)];
			if (col + 1 < (size * 2))
			{
				ans += result[(row) * (size * 2) + (col + 1)];
			}
		}
		return ans;
	}
	//Display result of trinomial triangle
	public void show(int[] result, int size)
	{
		for (int i = 0; i < size; i++)
		{
			for (int j = 0; j < size * 2; j++)
			{
				if (result[(i * (size * 2)) + j] != 0)
				{
					Console.Write("" + result[(i * (size * 2)) + j] + "\t");
				}
				else if (j != 0)
				{
					Console.Write("\t");
				}
			}
			Console.Write("\n");
		}
	}
	//Set default value of resultant array
	public void set_default(int[] result, int size)
	{
		for (int i = 0; i < size; i++)
		{
			result[i] = 0;
		}
	}
	public void trinomial_triangle(int size)
	{
		Console.Write("\n ROW SIZE : " + size + "\n");
		// Number of spaces are required to store trinomial triangle element
		int capacity = size * (size * 2);
		int[] result = new int[capacity];
		int col = (size * 2) / 2;
		int counter = 1;
		set_default(result, capacity);
		//Set the first row element
		result[col] = 1;
		for (int i = 1; i < size; i++)
		{
			//This is indicate number of valid operation in i-th row
			counter = counter + 2;
			col = col - 1;
			for (int j = 0; j < counter && j + col < size * 2; j++)
			{
				//Store the result of top three closest element
				result[(i * (size * 2)) + col + j] = sum_result(result, i - 1, col + j, size);
			}
		}
		show(result, size);
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.trinomial_triangle(5);
		obj.trinomial_triangle(7);
	}
}

Output

 ROW SIZE : 5
				1
			1	1	1
		1	2	3	2	1
	1	3	6	7	6	3	1
1	4	10	16	19	16	10	4	1

 ROW SIZE : 7
						1
					1	1	1
				1	2	3	2	1
			1	3	6	7	6	3	1
		1	4	10	16	19	16	10	4	1
	1	5	15	30	45	51	45	30	15	5	1
1	6	21	50	90	126	141	126	90	50	21	6	1
<?php
// Php Program
// Display Trinomial Triangle
class MyPattern
{
	public 	function sum_result( & $result, $row, $col, $size)
	{
		$ans = 0;
		//Check boundary condition
		if ($row >= 0 && $row < $size && $col >= 0 && $col < ($size *2))
		{
			if ($col - 1 >= 0)
			{
				//When fill result column is greater than zero
				$ans += $result[($row) *($size *2) + ($col - 1)];
			}
			//get the result of just above element of resultant matrix
			$ans += $result[($row) *($size *2) + ($col)];
			if ($col + 1 < ($size *2))
			{
				$ans += $result[($row) *($size *2) + ($col + 1)];
			}
		}
		return $ans;
	}
	//Display result of trinomial triangle
	public 	function show( & $result, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			for ($j = 0; $j < $size *2; ++$j)
			{
				if ($result[($i *($size *2)) + $j] != 0)
				{
					echo("". $result[($i *($size *2)) + $j] ."\t");
				}
				else if ($j != 0)
				{
					echo("\t");
				}
			}
			echo("\n");
		}
	}
	//Set default value of resultant array
	public 	function set_default( & $result, $size)
	{
		for ($i = 0; $i < $size; ++$i)
		{
			$result[$i] = 0;
		}
	}
	public 	function trinomial_triangle($size)
	{
		echo("\n ROW SIZE : ". $size ."\n");
		// Number of spaces are required to store trinomial triangle element
		$capacity = $size *($size *2);
		// Used to store result of trinomial triangle
		$result = array_fill(0, $capacity, 0);
		$col = intval(($size *2) / 2);
		$counter = 1;
		$this->set_default($result, $capacity);
		//Set the first row element
		$result[$col] = 1;
		for ($i = 1; $i < $size; ++$i)
		{
			//This is indicate number of valid operation in i-th row
			$counter = $counter + 2;
			$col = $col - 1;
			for ($j = 0; $j < $counter && $j + $col < $size *2; ++$j)
			{
				//Store the result of top three closest element
				$result[($i *($size *2)) + $col + $j] = $this->sum_result($result, $i - 1, $col + $j, $size);
			}
		}
		$this->show($result, $size);
	}
}

function main()
{
	$obj = new MyPattern();
	//Test Cases
	$obj->trinomial_triangle(5);
	$obj->trinomial_triangle(7);
}
main();

Output

 ROW SIZE : 5
				1
			1	1	1
		1	2	3	2	1
	1	3	6	7	6	3	1
1	4	10	16	19	16	10	4	1

 ROW SIZE : 7
						1
					1	1	1
				1	2	3	2	1
			1	3	6	7	6	3	1
		1	4	10	16	19	16	10	4	1
	1	5	15	30	45	51	45	30	15	5	1
1	6	21	50	90	126	141	126	90	50	21	6	1
// Node Js Program
// Display Trinomial Triangle
class MyPattern
{
	sum_result(result, row, col, size)
	{
		var ans = 0;
		//Check boundary condition
		if (row >= 0 && row < size && col >= 0 && col < (size *2))
		{
			if (col - 1 >= 0)
			{
				//When fill result column is greater than zero
				ans += result[(row) *(size *2) + (col - 1)];
			}
			//get the result of just above element of resultant matrix
			ans += result[(row) *(size *2) + (col)];
			if (col + 1 < (size *2))
			{
				ans += result[(row) *(size *2) + (col + 1)];
			}
		}
		return ans;
	}
	//Display result of trinomial triangle
	show(result, size)
	{
		for (var i = 0; i < size; ++i)
		{
			for (var j = 0; j < size *2; ++j)
			{
				if (result[(i *(size *2)) + j] != 0)
				{
					process.stdout.write("" + result[(i *(size *2)) + j] + "\t");
				}
				else if (j != 0)
				{
					process.stdout.write("\t");
				}
			}
			process.stdout.write("\n");
		}
	}
	//Set default value of resultant array
	set_default(result, size)
	{
		for (var i = 0; i < size; ++i)
		{
			result[i] = 0;
		}
	}
	trinomial_triangle(size)
	{
		process.stdout.write("\n ROW SIZE : " + size + "\n");
		// Number of spaces are required to store trinomial triangle element
		var capacity = size *(size *2);
		// Used to store result of trinomial triangle
		var result = Array(capacity).fill(0);
		var col = parseInt((size *2) / 2);
		var counter = 1;
		this.set_default(result, capacity);
		//Set the first row element
		result[col] = 1;
		for (var i = 1; i < size; ++i)
		{
			//This is indicate number of valid operation in i-th row
			counter = counter + 2;
			col = col - 1;
			for (var j = 0; j < counter && j + col < size *2; ++j)
			{
				//Store the result of top three closest element
				result[(i *(size *2)) + col + j] = this.sum_result(result, i - 1, col + j, size);
			}
		}
		this.show(result, size);
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Test Cases
	obj.trinomial_triangle(5);
	obj.trinomial_triangle(7);
}
main();

Output

 ROW SIZE : 5
				1
			1	1	1
		1	2	3	2	1
	1	3	6	7	6	3	1
1	4	10	16	19	16	10	4	1

 ROW SIZE : 7
						1
					1	1	1
				1	2	3	2	1
			1	3	6	7	6	3	1
		1	4	10	16	19	16	10	4	1
	1	5	15	30	45	51	45	30	15	5	1
1	6	21	50	90	126	141	126	90	50	21	6	1
#  Python 3 Program
#  Display Trinomial Triangle
class MyPattern :
	def sum_result(self, result, row, col, size) :
		ans = 0
		# Check boundary condition
		if (row >= 0 and row < size and col >= 0 and col < (size * 2)) :
			if (col - 1 >= 0) :
				# When fill result column is greater than zero
				ans += result[(row) * (size * 2) + (col - 1)]
			
			# get the result of just above element of resultant matrix
			ans += result[(row) * (size * 2) + (col)]
			if (col + 1 < (size * 2)) :
				ans += result[(row) * (size * 2) + (col + 1)]
			
		
		return ans
	
	# Display result of trinomial triangle
	def show(self, result, size) :
		i = 0
		j = 0
		while (i < size) :
			j = 0
			while (j < size * 2) :
				if (result[(i * (size * 2)) + j] != 0) :
					print("", result[(i * (size * 2)) + j] ,"\t", end = "")
				elif (j != 0) :
					print("\t", end = "")
				
				j += 1
			
			print("\n", end = "")
			i += 1
		
	
	# Set default value of resultant array
	def set_default(self, result, size) :
		i = 0
		while (i < size) :
			result[i] = 0
			i += 1
		
	
	def trinomial_triangle(self, size) :
		print("\n ROW SIZE : ", size ,"\n", end = "")
		#  Number of spaces are required to store trinomial triangle element
		capacity = size * (size * 2)
		result = [0] * capacity
		col = int((size * 2) / 2)
		counter = 1
		self.set_default(result, capacity)
		# Set the first row element
		result[col] = 1
		i = 1
		while (i < size) :
			# This is indicate number of valid operation in i-th row
			counter = counter + 2
			col = col - 1
			j = 0
			while (j < counter and j + col < size * 2) :
				# Store the result of top three closest element
				result[(i * (size * 2)) + col + j] = self.sum_result(result, i - 1, col + j, size)
				j += 1
			
			i += 1
		
		self.show(result, size)
	

def main() :
	obj = MyPattern()
	# Test Cases
	obj.trinomial_triangle(5)
	obj.trinomial_triangle(7)


if __name__ == "__main__": main()

Output

 ROW SIZE :  5
				 1
			 1 	 1 	 1
		 1 	 2 	 3 	 2 	 1
	 1 	 3 	 6 	 7 	 6 	 3 	 1
 1 	 4 	 10 	 16 	 19 	 16 	 10 	 4 	 1

 ROW SIZE :  7
						 1
					 1 	 1 	 1
				 1 	 2 	 3 	 2 	 1
			 1 	 3 	 6 	 7 	 6 	 3 	 1
		 1 	 4 	 10 	 16 	 19 	 16 	 10 	 4 	 1
	 1 	 5 	 15 	 30 	 45 	 51 	 45 	 30 	 15 	 5 	 1
 1 	 6 	 21 	 50 	 90 	 126 	 141 	 126 	 90 	 50 	 21 	 6 	 1
#  Ruby Program
#  Display Trinomial Triangle
class MyPattern

	def sum_result(result, row, col, size)
	
		ans = 0
		# Check boundary condition
		if (row >= 0 && row < size && col >= 0 && col < (size * 2))
		
			if (col - 1 >= 0)
			
				# When fill result column is greater than zero
				ans += result[(row) * (size * 2) + (col - 1)]
			end
			# get the result of just above element of resultant matrix
			ans += result[(row) * (size * 2) + (col)]
			if (col + 1 < (size * 2))
			
				ans += result[(row) * (size * 2) + (col + 1)]
			end
		end
		return ans
	end
	# Display result of trinomial triangle
	def show(result, size)
	
		i = 0
		j = 0
		while (i < size)
		
			j = 0
			while (j < size * 2)
			
				if (result[(i * (size * 2)) + j] != 0)
				
					print("", result[(i * (size * 2)) + j] ,"\t")
				elsif (j != 0)
				
					print("\t")
				end
				j += 1
			end
			print("\n")
			i += 1
		end
	end
	# Set default value of resultant array
	def set_default(result, size)
	
		i = 0
		while (i < size)
		
			result[i] = 0
			i += 1
		end
	end
	def trinomial_triangle(size)
	
		print("\n ROW SIZE  :", size ,"\n")
		#  Number of spaces are required to store trinomial triangle element
		capacity = size * (size * 2)
		result = Array.new(capacity) {0}
		col = (size * 2) / 2
		counter = 1
		self.set_default(result, capacity)
		# Set the first row element
		result[col] = 1
		i = 1
		while (i < size)
		
			# This is indicate number of valid operation in i-th row
			counter = counter + 2
			col = col - 1
			j = 0
			while (j < counter && j + col < size * 2)
			
				# Store the result of top three closest element
				result[(i * (size * 2)) + col + j] = self.sum_result(result, i - 1, col + j, size)
				j += 1
			end
			i += 1
		end
		self.show(result, size)
	end
end
def main()

	obj = MyPattern.new()
	# Test Cases
	obj.trinomial_triangle(5)
	obj.trinomial_triangle(7)
end
main()

Output

 ROW SIZE  :5
				1					
			1	1	1				
		1	2	3	2	1			
	1	3	6	7	6	3	1		
1	4	10	16	19	16	10	4	1	

 ROW SIZE  :7
						1							
					1	1	1						
				1	2	3	2	1					
			1	3	6	7	6	3	1				
		1	4	10	16	19	16	10	4	1			
	1	5	15	30	45	51	45	30	15	5	1		
1	6	21	50	90	126	141	126	90	50	21	6	1	
// Scala Program
// Display Trinomial Triangle
class MyPattern
{
	def sum_result(result: Array[Int], row: Int, col: Int, size: Int): Int = {
		var ans: Int = 0;
		//Check boundary condition
		if (row >= 0 && row < size && col >= 0 && col < (size * 2))
		{
			if (col - 1 >= 0)
			{
				//When fill result column is greater than zero
				ans += result((row) * (size * 2) + (col - 1));
			}
			//get the result of just above element of resultant matrix
			ans += result((row) * (size * 2) + (col));
			if (col + 1 < (size * 2))
			{
				ans += result((row) * (size * 2) + (col + 1));
			}
		}
		return ans;
	}
	//Display result of trinomial triangle
	def show(result: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		var j: Int = 0;
		while (i < size)
		{
			j = 0;
			while (j < size * 2)
			{
				if (result((i * (size * 2)) + j) != 0)
				{
					print("" + result((i * (size * 2)) + j) + "\t");
				}
				else if (j != 0)
				{
					print("\t");
				}
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
	//Set default value of resultant array
	def set_default(result: Array[Int], size: Int): Unit = {
		var i: Int = 0;
		while (i < size)
		{
			result(i) = 0;
			i += 1;
		}
	}
	def trinomial_triangle(size: Int): Unit = {
		print("\n ROW SIZE : " + size + "\n");
		// Number of spaces are required to store trinomial triangle element
		var capacity: Int = size * (size * 2);
		var result: Array[Int] = Array.fill[Int](capacity)(0);
		var col: Int = ((size * 2) / 2).toInt;
		var counter: Int = 1;
		set_default(result, capacity);
		//Set the first row element
		result(col) = 1;
		var i: Int = 1;
		while (i < size)
		{
			//This is indicate number of valid operation in i-th row
			counter = counter + 2;
			col = col - 1;
			var j: Int = 0;
			while (j < counter && j + col < size * 2)
			{
				//Store the result of top three closest element
				result((i * (size * 2)) + col + j) = sum_result(result, i - 1, col + j, size);
				j += 1;
			}
			i += 1;
		}
		show(result, size);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test Cases
		obj.trinomial_triangle(5);
		obj.trinomial_triangle(7);
	}
}

Output

 ROW SIZE : 5
				1
			1	1	1
		1	2	3	2	1
	1	3	6	7	6	3	1
1	4	10	16	19	16	10	4	1

 ROW SIZE : 7
						1
					1	1	1
				1	2	3	2	1
			1	3	6	7	6	3	1
		1	4	10	16	19	16	10	4	1
	1	5	15	30	45	51	45	30	15	5	1
1	6	21	50	90	126	141	126	90	50	21	6	1
// Swift Program
// Display Trinomial Triangle
class MyPattern
{
	func sum_result(_ result: [Int], _ row: Int, _ col: Int, _ size: Int) -> Int
	{
		var ans: Int = 0;
		//Check boundary condition
		if (row >= 0 && row < size && col >= 0 && col < (size * 2))
		{
			if (col - 1 >= 0)
			{
				//When fill result column is greater than zero
				ans += result[(row) * (size * 2) + (col - 1)];
			}
			//get the result of just above element of resultant matrix
			ans += result[(row) * (size * 2) + (col)];
			if (col + 1 < (size * 2))
			{
				ans += result[(row) * (size * 2) + (col + 1)];
			}
		}
		return ans;
	}
	//Display result of trinomial triangle
	func show(_ result: [Int], _ size: Int)
	{
		var i: Int = 0;
		var j: Int = 0;
		while (i < size)
		{
			j = 0;
			while (j < size * 2)
			{
				if (result[(i * (size * 2)) + j] != 0)
				{
					print("", result[(i * (size * 2)) + j] ,"\t", terminator: "");
				}
				else if (j != 0)
				{
					print("\t", terminator: "");
				}
				j += 1;
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
	//Set default value of resultant array
	func set_default(_ result: inout[Int], _ size: Int)
	{
		var i: Int = 0;
		while (i < size)
		{
			result[i] = 0;
			i += 1;
		}
	}
	func trinomial_triangle(_ size: Int)
	{
		print("\n ROW SIZE : ", size ,"\n", terminator: "");
		// Number of spaces are required to store trinomial triangle element
		let capacity: Int = size * (size * 2);
		var result: [Int] = Array(repeating: 0, count: capacity);
		var col: Int = (size * 2) / 2;
		var counter: Int = 1;
		self.set_default(&result, capacity);
		//Set the first row element
		result[col] = 1;
		var i: Int = 1;
		while (i < size)
		{
			//This is indicate number of valid operation in i-th row
			counter = counter + 2;
			col = col - 1;
			var j: Int = 0;
			while (j < counter && j + col < size * 2)
			{
				//Store the result of top three closest element
				result[(i * (size * 2)) + col + j] = self.sum_result(result, i - 1, col + j, size);
				j += 1;
			}
			i += 1;
		}
		self.show(result, size);
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test Cases
	obj.trinomial_triangle(5);
	obj.trinomial_triangle(7);
}
main();

Output

 ROW SIZE :  5
				 1
			 1 	 1 	 1
		 1 	 2 	 3 	 2 	 1
	 1 	 3 	 6 	 7 	 6 	 3 	 1
 1 	 4 	 10 	 16 	 19 	 16 	 10 	 4 	 1

 ROW SIZE :  7
						 1
					 1 	 1 	 1
				 1 	 2 	 3 	 2 	 1
			 1 	 3 	 6 	 7 	 6 	 3 	 1
		 1 	 4 	 10 	 16 	 19 	 16 	 10 	 4 	 1
	 1 	 5 	 15 	 30 	 45 	 51 	 45 	 30 	 15 	 5 	 1
 1 	 6 	 21 	 50 	 90 	 126 	 141 	 126 	 90 	 50 	 21 	 6 	 1




Comment

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