The Wayback Machine - https://web.archive.org/web/20240930190714/https://www.geeksforgeeks.org/pascal-triangle/
Open In App

Pascal’s Triangle

Last Updated : 02 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Medium
23.68%
1.1L

Pascal’s triangle is a triangular array of binomial coefficients. Write a function that takes an integer value N as input and prints the first N lines of Pascal’s triangle.

Example:

The below image shows the Pascal’s Triangle for N=6

Pascal's-Triangle-for-first-6-Rows

Pascal’s Triangle using Binomial Coefficient:

The number of entries in every line is equal to line number. For example, the first line has “1“, the second line has “1 1“, the third line has “1 2 1“,.. and so on. Every entry in a line is value of a Binomial Coefficient. The value of ith entry in line number line is C(line, i). The value can be calculated using following formula. 

  • C(line, i) = line! / ( (line-i)! * i! )
 

Algorithm:

  • Run a loop for each row of pascal’s triangle i.e. 1 to N.
    • For each row, run an internal loop for each element of that row.
      • Calculate the binomial coefficient for the element using the formula mentioned in the approach.

Below is the implementation of the above approach:

C++
//  C++ code for Pascal's Triangle
#include <iostream>
using namespace std;


int binomialCoeff(int n, int k);

// Function to print first
// n lines of Pascal's
// Triangle
void printPascal(int n)
{
    // Iterate through every line and
    // print entries in it
    for (int line = 0; line < n; line++) {
        // Every line has number of
        // integers equal to line
        // number
        for (int i = 0; i <= line; i++)
            cout << " " << binomialCoeff(line, i);
        cout << "\n";
    }
}

// See
// https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/
// for details of this function
int binomialCoeff(int n, int k)
{
    int res = 1;
    if (k > n - k)
        k = n - k;
    for (int i = 0; i < k; ++i) {
        res *= (n - i);
        res /= (i + 1);
    }

    return res;
}

// Driver program
int main()
{
    int n = 7;
    printPascal(n);
    return 0;
}

// This code is contributed by shivanisinghss2110
C
//  C++ code for Pascal's Triangle
#include <stdio.h>

// See https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/ 
// for details of this function
int binomialCoeff(int n, int k);

// Function to print first
// n lines of Pascal's 
// Triangle
void printPascal(int n)
{
    // Iterate through every line and
    // print entries in it
    for (int line = 0; line < n; line++)
    {
        // Every line has number of 
        // integers equal to line 
        // number
        for (int i = 0; i <= line; i++)
            printf("%d ",
                    binomialCoeff(line, i));
        printf("\n");
    }
}

// See https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/
// for details of this function
int binomialCoeff(int n, int k)
{
    int res = 1;
    if (k > n - k)
    k = n - k;
    for (int i = 0; i < k; ++i)
    {
        res *= (n - i);
        res /= (i + 1);
    }
    
    return res;
}

// Driver program 
int main()
{
    int n = 7;
    printPascal(n);
    return 0;
}
Java
// Java code for Pascal's Triangle
import java.io.*;

class GFG {
    
    // Function to print first
    // n lines of Pascal's Triangle
    static void printPascal(int n)
    {
        
    // Iterate through every line
    // and print entries in it
    for (int line = 0; line < n; line++)
    {
        // Every line has number of 
        // integers equal to line number
        for (int i = 0; i <= line; i++)
        System.out.print(binomialCoeff
                        (line, i)+" ");
                        
        System.out.println();
    }
    }
    
    // Link for details of this function
    // https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/
    static int binomialCoeff(int n, int k)
    {
        int res = 1;
        
        if (k > n - k)
        k = n - k;
            
        for (int i = 0; i < k; ++i)
        {
            res *= (n - i);
            res /= (i + 1);
        }
        return res;
    }
    
    // Driver code
    public static void main(String args[])
    {
    int n = 7;
    printPascal(n);
    }
}

/*This code is contributed by Nikita Tiwari.*/
Python
# Python 3 code for Pascal's Triangle
# A simple O(n^3) 
# program for 
# Pascal's Triangle

# Function to print 
# first n lines of
# Pascal's Triangle
def printPascal(n) :
    
    # Iterate through every line 
    # and print entries in it
    for line in range(0, n) :
        
        # Every line has number of 
        # integers equal to line
        # number
        for i in range(0, line + 1) :
            print(binomialCoeff(line, i),
                " ", end = "")
        print()
    

# See https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/
# for details of this function
def binomialCoeff(n, k) :
    res = 1
    if (k > n - k) :
        k = n - k
    for i in range(0 , k) :
        res = res * (n - i)
        res = res // (i + 1)
    
    return res

# Driver program
n = 7
printPascal(n)


# This code is contributed by Nikita Tiwari.
C#
// C# code for Pascal's Triangle
using System;

class GFG {
    
    // Function to print first
    // n lines of Pascal's Triangle
    static void printPascal(int n)
    {
        
    // Iterate through every line
    // and print entries in it
    for (int line = 0; line < n; line++)
    {
        // Every line has number of 
        // integers equal to line number
        for (int i = 0; i <= line; i++)
        Console.Write(binomialCoeff
                        (line, i)+" ");
                        
        Console.WriteLine();
    }
    }
    
    // Link for details of this function
    // https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/
    static int binomialCoeff(int n, int k)
    {
        int res = 1;
        
        if (k > n - k)
        k = n - k;
            
        for (int i = 0; i < k; ++i)
        {
            res *= (n - i);
            res /= (i + 1);
        }
        return res;
    }
    
    // Driver code
    public static void Main()
    {
    int n = 7;
    printPascal(n);
    }
}

/*This code is contributed by vt_m.*/
JavaScript
<script>

// Javascript code for Pascal's Triangle

    // Function to print first
    // n lines of Pascal's Triangle
    function printPascal(n)
    {
        
    // Iterate through every line
    // and print entries in it
    for (let line = 0; line < n; line++)
    {
        // Every line has number of 
        // integers equal to line number
        for (let i = 0; i <= line; i++)
        document.write(binomialCoeff
                        (line, i)+" ");
                        
        document.write("<br />");
    }
    }
    
    // Link for details of this function
    // https://www.geeksforgeeks.org/space-and-time-efficient-binomial-coefficient/
    function binomialCoeff(n, k)
    {
        let res = 1;
        
        if (k > n - k)
        k = n - k;
            
        for (let i = 0; i < k; ++i)
        {
            res *= (n - i);
            res /= (i + 1);
        }
        return res;
    }


// Driver Code

    let n = 7;
    printPascal(n);

</script>
PHP
<?php
// PHP implementation for
// Pascal's Triangle

// for details of this function
function binomialCoeff($n, $k)
{
    $res = 1;
    if ($k > $n - $k)
    $k = $n - $k;
    for ($i = 0; $i < $k; ++$i)
    {
        $res *= ($n - $i);
        $res /= ($i + 1);
    }
return $res;
}

// Function to print first
// n lines of Pascal's 
// Triangle
function printPascal($n)
{
    // Iterate through every line and
    // print entries in it
    for ($line = 0; $line < $n; $line++)
    {
        // Every line has number of 
        // integers equal to line 
        // number
        for ($i = 0; $i <= $line; $i++)
                echo "".binomialCoeff($line, $i)." ";
                
        echo "\n";
    }
}

// Driver Code
$n=7;
printPascal($n);

// This code is contributed by Mithun Kumar
?>

Output
 1
 1 1
 1 2 1
 1 3 3 1
 1 4 6 4 1
 1 5 10 10 5 1
 1 6 15 20 15 6 1

Time complexity: O(N^3), where N is the number of rows you want to print
Auxiliary Space: O(1)

Pascal’s Triangle using Dynamic Programming:

If we take a closer at the triangle, we observe that every entry is sum of the two values above it. So using dynamic programming we can create a 2D array that stores previously generated values. In order to generate a value in a line, we can use the previously stored values from array. 

Cases:

  • If line == 0 or line == i
    • arr[line][i] =1
  • Else:
    • arr[line][i] = arr[line-1][i-1] + arr[line-1][i]

Below is the implementation of the above approach:

C++
// C++ program for Pascal’s Triangle
// A O(n^2) time and O(n^2) extra space 
// method for Pascal's Triangle
#include <bits/stdc++.h>
using namespace std;

void printPascal(int n)
{
    
    // An auxiliary array to store 
    // generated pascal triangle values
    int arr[n][n]; 

    // Iterate through every line and 
    // print integer(s) in it
    for (int line = 0; line < n; line++)
    {
        // Every line has number of integers 
        // equal to line number
        for (int i = 0; i <= line; i++)
        {
        // First and last values in every row are 1
        if (line == i || i == 0)
            arr[line][i] = 1;
          
        // Other values are sum of values just 
        // above and left of above
        else
            arr[line][i] = arr[line - 1][i - 1] + 
                            arr[line - 1][i];
        cout << arr[line][i] << " ";
        }
        cout << "\n";
    }
}

// Driver code
int main()
{
    int n = 5;
    printPascal(n);
    return 0;
}

// This code is Contributed by Code_Mech.
C
// C program for Pascal’s Triangle
// A O(n^2) time and O(n^2) extra space 
// method for Pascal's Triangle
void printPascal(int n)
{
// An auxiliary array to store 
// generated pascal triangle values
int arr[n][n]; 

// Iterate through every line and print integer(s) in it
for (int line = 0; line < n; line++)
{
    // Every line has number of integers 
    // equal to line number
    for (int i = 0; i <= line; i++)
    {
    // First and last values in every row are 1
    if (line == i || i == 0)
        arr[line][i] = 1;
    // Other values are sum of values just 
    // above and left of above
    else 
        arr[line][i] = arr[line-1][i-1] + arr[line-1][i];
    printf("%d ", arr[line][i]);
    }
    printf("\n");
}
}
// Driver code
int main()
{
int n = 5;
    printPascal(n);
    return 0;
}
Java
// java program for Pascal's Triangle
// A O(n^2) time and O(n^2) extra 
// space method for Pascal's Triangle
import java.io.*;

class GFG {
    public static void main (String[] args) {
        int n = 5;
        printPascal(n);
    }

public static void printPascal(int n)
{
// An auxiliary array to store generated pascal triangle values
int[][] arr = new int[n][n]; 

// Iterate through every line and print integer(s) in it
for (int line = 0; line < n; line++)
{
    // Every line has number of integers equal to line number
    for (int i = 0; i <= line; i++)
    {
    // First and last values in every row are 1
    if (line == i || i == 0)
        arr[line][i] = 1;
    else // Other values are sum of values just above and left of above
        arr[line][i] = arr[line-1][i-1] + arr[line-1][i];
    System.out.print(arr[line][i]);
    }
    System.out.println("");
}
}
}
Python3
# Python3 program for Pascal's Triangle

# A O(n^2) time and O(n^2) extra 
# space method for Pascal's Triangle
def printPascal(n:int):

    # An auxiliary array to store 
    # generated pascal triangle values
    arr = [[0 for x in range(n)] 
              for y in range(n)] 

    # Iterate through every line
    # and print integer(s) in it
    for line in range (0, n):

        # Every line has number of 
        # integers equal to line number
        for i in range (0, line + 1):

            # First and last values 
            # in every row are 1
            if(i is 0 or i is line):
                arr[line][i] = 1
                print(arr[line][i], end = " ") 

            # Other values are sum of values
            # just above and left of above 
            else:
                arr[line][i] = (arr[line - 1][i - 1] + 
                                arr[line - 1][i])
                print(arr[line][i], end = " ")             
        print("\n", end = "")

# Driver Code
n = 5
printPascal(n)

# This code is contributed 
# by Sanju Maderna
C#
// C# program for Pascal's Triangle
// A O(n^2) time and O(n^2) extra 
// space method for Pascal's Triangle
using System;

class GFG 
{
public static void printPascal(int n)
{
    
// An auxiliary array to store 
// generated pascal triangle values
int[,] arr = new int[n, n]; 

// Iterate through every line
// and print integer(s) in it
for (int line = 0; line < n; line++)
{
    // Every line has number of 
    // integers equal to line number
    for (int i = 0; i <= line; i++)
    {
        
    // First and last values 
    // in every row are 1
    if (line == i || i == 0)
        arr[line, i] = 1;
    else // Other values are sum of values
         // just above and left of above
        arr[line, i] = arr[line - 1, i - 1] + 
                       arr[line - 1, i];
    Console.Write(arr[line, i]);
    }
Console.WriteLine("");
}
}

// Driver Code
public static void Main () 
{
    int n = 5;
    printPascal(n);
}
}

// This code is contributed 
// by Akanksha Rai(Abby_akku)
JavaScript
<script>

// javascript program for Pascal's Triangle
// A O(n^2) time and O(n^2) extra 
// space method for Pascal's Triangle
var n = 5;
printPascal(n);

function printPascal(n)
{
// An auxiliary array to store generated pascal triangle values
arr = a = Array(n).fill(0).map(x => Array(n).fill(0));

// Iterate through every line and print integer(s) in it
for (line = 0; line < n; line++)
{
    // Every line has number of integers equal to line number
    for (i = 0; i <= line; i++)
    {
    // First and last values in every row are 1
    if (line == i || i == 0)
        arr[line][i] = 1;
    else 
    // Other values are sum of values just above and left of above
        arr[line][i] = arr[line-1][i-1] + arr[line-1][i];
    document.write(arr[line][i]);
    }
    document.write("<br>");
}
}

// This code is contributed by 29AjayKumar 

</script>
PHP
<?php
// PHP program for Pascal’s Triangle
// A O(n^2) time and O(n^2) extra space 
// method for Pascal's Triangle
function printPascal($n)
{
    // An auxiliary array to store 
    // generated pascal triangle values
    $arr = array(array()); 
    
    // Iterate through every line and 
    // print integer(s) in it
    for ($line = 0; $line < $n; $line++)
    {
        // Every line has number of integers 
        // equal to line number
        for ($i = 0; $i <= $line; $i++)
        {
            // First and last values in every row are 1
            if ($line == $i || $i == 0)
                $arr[$line][$i] = 1;
                
            // Other values are sum of values just 
            // above and left of above
            else
                $arr[$line][$i] = $arr[$line - 1][$i - 1] + 
                                $arr[$line - 1][$i];
            echo $arr[$line][$i] . " ";
        }
        echo "\n";
    }
}

// Driver code
$n = 5;
printPascal($n);

// This code is contributed
// by Akanksha Rai
?>

Output
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 

Time Complexity: O(N^2)
Auxiliary Space: O(N^2)

Note: This method can be optimized to use O(n) extra space as we need values only from previous row. So we can create an auxiliary array of size n and overwrite values. Following is another method uses only O(1) extra space.

Pascal’s Triangle using Binomial Coefficient (Space Optimised):

This method is based on approach using Binomial Coefficient. We know that ith entry in a line number line is Binomial Coefficient C(line, i) and all lines start with value 1. The idea is to calculate C(line, i) using C(line, i-1). It can be calculated in O(1) time.

  • C(line, i) = line! / ( (line-i)! * i! )
  • C(line, i-1) = line! / ( (line – i + 1)! * (i-1)! )
  • We can derive following expression from above two expressions.
  • C(line, i) = C(line, i-1) * (line – i + 1) / i
  • So C(line, i) can be calculated from C(line, i-1) in O(1) time

below is the implementation of the approach:

C++
// C++ program for Pascal’s Triangle
// A O(n^2) time and O(1) extra space
// function for Pascal's Triangle
#include <bits/stdc++.h>

using namespace std;
void printPascal(int n)
{

    for (int line = 1; line <= n; line++) {
        int C = 1; // used to represent C(line, i)
        for (int i = 1; i <= line; i++) {

            // The first value in a line is always 1
            cout << C << " ";
            C = C * (line - i) / i;
        }
        cout << "\n";
    }
}

// Driver code
int main()
{
    int n = 5;
    printPascal(n);
    return 0;
}

// This code is contributed by Code_Mech
C
// C program for Pascal’s Triangle
// A O(n^2) time and O(1) extra space 
// function for Pascal's Triangle
void printPascal(int n)
{
for (int line = 1; line <= n; line++)
{
    int C = 1; // used to represent C(line, i)
    for (int i = 1; i <= line; i++) 
    {
    printf("%d ", C); // The first value in a line is always 1
    C = C * (line - i) / i; 
    }
    printf("\n");
}
}
// Driver code
int main()
{
int n = 5;
    printPascal(n);
    return 0;
}
Java
// Java program for Pascal's Triangle
// A O(n^2) time and O(1) extra 
// space method for Pascal's Triangle
import java.io.*;
class GFG {

//Pascal function 
public static void printPascal(int n)
{
    for(int line = 1; line <= n; line++)
    {
        
    int C=1;// used to represent C(line, i)
    for(int i = 1; i <= line; i++)
    { 
        // The first value in a line is always 1
        System.out.print(C+" ");
        C = C * (line - i) / i; 
    }
    System.out.println();
    }
}

// Driver code
public static void main (String[] args) {
    int n = 5;
    printPascal(n);
} 
}
// This code is contributed 
// by Archit Puri
Python
# Python3 program for Pascal's Triangle 
# A O(n^2) time and O(1) extra 
# space method for Pascal's Triangle 

# Pascal function 
def printPascal(n): 

    for line in range(1, n + 1): 
        C = 1; # used to represent C(line, i) 
        for i in range(1, line + 1): 
            
            # The first value in a 
            # line is always 1 
            print(C, end = " "); 
            C = int(C * (line - i) / i); 
        print(""); 

# Driver code 
n = 5; 
printPascal(n);

# This code is contributed by mits
C#
// C# program for Pascal's Triangle 
// A O(n^2) time and O(1) extra 
// space method for Pascal's Triangle 
using System;
class GFG 
{ 

// Pascal function 
public static void printPascal(int n) 
{ 
    for(int line = 1; 
            line <= n; line++) 
    { 
        
    int C = 1;// used to represent C(line, i) 
    for(int i = 1; i <= line; i++) 
    { 
        // The first value in a
        // line is always 1 
        Console.Write(C + " "); 
        C = C * (line - i) / i; 
    } 
    Console.Write("\n") ;
    } 
} 

// Driver code 
public static void Main ()
{ 
    int n = 5; 
    printPascal(n); 
} 
} 

// This code is contributed
// by ChitraNayal
JavaScript
<script>

// JavaScript program for Pascal's Triangle
// A O(n^2) time and O(1) extra 
// space method for Pascal's Triangle

//Pascal function 
function printPascal(n)
{
    for(line = 1; line <= n; line++)
    {
        
    var C=1;// used to represent C(line, i)
    for(i = 1; i <= line; i++)
    { 
        // The first value in a line is always 1
        document.write(C+" ");
        C = C * (line - i) / i; 
    }
    document.write("<br>");
    }
}

// Driver code
var n = 5;
printPascal(n);

// This code is contributed by 29AjayKumar 

</script>
PHP
<?php
// PHP program for Pascal's Triangle 
// A O(n^2) time and O(1) extra 
// space method for Pascal's Triangle 

// Pascal function 
function printPascal($n) 
{ 
    for($line = 1; $line <= $n; $line++) 
    { 
        $C = 1;// used to represent C(line, i) 
        for($i = 1; $i <= $line; $i++) 
        { 
            // The first value in a 
            // line is always 1 
            print($C . " "); 
            $C = $C * ($line - $i) / $i; 
        } 
        print("\n"); 
    } 
} 

// Driver code 
$n = 5; 
printPascal($n);

// This code is contributed by mits
?>

Output
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 

Time Complexity: O(n2)
Auxiliary Space: O(1)

Variations of the problem that may be asked in interviews:

  • Find the whole pascal triangle as shown above.
  • Find just the one element of a pascal’s triangle given row number and column number in O(n) time.
  • Find a particular row of pascal’s triangle given a row number in O(n) time.
     


Previous Article
Next Article

Similar Reads

Odd numbers in N-th row of Pascal's Triangle
Given N, the row number of Pascal's triangle(row starting from 0). Find the count of odd numbers in the N-th row of Pascal's Triangle.Prerequisite: Pascal's Triangle | Count number of 1's in binary representation of N Examples: Input : 11 Output : 8 Input : 20 Output : 4 Approach: It appears the answer is always a power of 2. In fact, the following
5 min read
Sum of all the numbers present at given level in Pascal's triangle
Given a level L. The task is to find the sum of all the integers present at the given level in Pascal's triangle . A Pascal triangle with 6 levels is shown below: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Examples: Input: L = 3 Output: 4 1 + 2 + 1 = 4 Input: L = 2 Output:2 Approach: If we observe carefully the series of the sum of levels will go
3 min read
Maximum of all the integers in the given level of Pascal triangle
Given an integer L, the task is to find the maximum of all the integers present at the given level in Pascal's triangle. A Pascal triangle with 6 levels is shown below: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 Examples: Input: L = 3 Output: 3 0th level -> 1 1st level -> 1 1 2nd level -> 1 2 1 3rd level -> 1 3 3 1 Input: L = 5 Output:
5 min read
Sum of all the numbers present at given level in Modified Pascal’s triangle
Given a level N, the task is to find the sum of all the integers present at this given level in an Alternating Pascal's triangle. A Modified Pascal triangle with 5 levels is shown below. 1 -1 1 1 -2 1 -1 3 -3 1 1 -4 6 -4 1 Examples: Input: N = 1 Output: 1 Input: N = 2 Output: 0 Approach: As we can observe for even level sum is 0 and for odd level e
3 min read
Find the Nth row in Pascal's Triangle
Given a non-negative integer N, the task is to find the Nth row of Pascal's Triangle. Note: The row index starts from 0. Pascal's Triangle: 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 Examples: Input: N = 3 Output: 1, 3, 3, 1 Explanation: The elements in the 3rd row are 1 3 3 1. Input: N = 0 Output: 1 Naive Approach: The simplest approach to solve the problem is
9 min read
Check if Pascal's Triangle is possible with a complete layer by using numbers upto N
Given a number N, the task is to determine if it is possible to make Pascal's triangle with a complete layer by using total number N integer if possible print Yes otherwise print No. Note: Pascal’s triangle is a triangular array of the binomial coefficients. Following are the first 6 rows of Pascal’s Triangle. 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 1
4 min read
Encrypt given Array in single digit using inverted Pascal Triangle
Given an array arr[] of length N (N > 1)containing positive integers, the task is to encrypt the numbers of the array into a single digit using the inverted Pascal triangle as follows. From the starting of the array find the sum of two adjacent elements.Replace the sum with only the digit at the unit position of the sum.Replace all the array ele
11 min read
Sum of all elements up to Nth row in a Pascal triangle
Given a row number n, the task is to calculate the sum of all elements of each row up to nth row. Examples: Input : 2Output : 7 Explanation: row 0 have element 1 row 1 have elements 1, 1 row 2 have elements 1, 2, 1 so, sum will be ((1) + (1 + 1) + (1 + 2 + 1)) = 7Input : 4Output : 31Explanation: row 0 have element 1 row 1 have elements 1, 1 row 2 h
9 min read
Calculate nCr using Pascal's Triangle
A useful application of Pascal's triangle is the calculation of combinations. The formula to find nCr is n! / r! * (n - r)! which is also the formula for a cell of Pascal's triangle. Pascal's triangle:  Input: n = 5, r = 3Output: 10Explanation:n! / r! * (n - r)! = 5! / 3! * (2!) = 120 / 12 = 10Input: n = 7, r = 2Output: 21Explanation:n! / r! * (n -
11 min read
Program to print a Hollow Triangle inside a Triangle
Given a number N(? 8), the task is to print a Hollow Triangle inside a Triangle pattern.Example: Input: N = 9 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Approach: Let i be the index for rows and j be the index for columns. Then: For sides of outer triangle: If the index of column(j) is equals to (N - i + 1) or (
7 min read
Calculate ratio of area of a triangle inscribed in an Ellipse and the triangle formed by corresponding points on auxiliary circle
Given two integers A and B representing the lengths of semi-major and semi-minor axes of an ellipse, the task is to calculate the ratio of any triangle inscribed in the ellipse and that of the triangle formed by the corresponding points on its auxiliary circle. Examples: Input: A = 1, B = 2Output: 2Explanation: Ratio = B / A = 2 / 1 = 2 Input: A =
4 min read
Maximum size of subset of given array such that a triangle can be formed by any three integers as the sides of the triangle
Given an array arr[] consisting of N integers, the task is to find the size of the largest subset of the array such that a triangle can be formed from any of the three integers of the subset as the sides of a triangle. Examples: Input: arr[] = {1, 4, 7, 4}Output: 3Explanation: A possible subsets that follow the given conditions are {1, 4, 4} and {4
7 min read
Sum of upper triangle and lower triangle
Given a N x M matrix. The task is to print the sum of upper and lower triangular elements (i.e elements on the diagonal and the upper and lower elements). Examples : Input: {{6, 5, 4}, {1, 2, 5}, {7, 9, 7}}Output:Upper sum is 29Lower sum is 32 Input: {{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}Output:Upper sum is 10Lower sum is 14 Recommended Problem Sum of u
6 min read
Pascal Matrix
In mathematics, particularly in matrix theory and combinatorics, the Pascal Matrix is an infinite matrix containing binomial coefficients as its elements. There are three ways to achieve this: as either an upper-triangular matrix, a lower-triangular matrix, or a symmetric matrix. The 5 x 5 truncations of these are shown below: The elements of the s
6 min read
What are Pascal Strings ?
This article intends to provide a basic understanding of traditionally used strings i.e. "Pascal Strings". As the name is self-explanatory, pascal strings are an essential constituent of the PASCAL programming language. Pascal is a computer programming language that Niklaus Wirth of Switzerland created around 1970 to teach organized programming. Wh
4 min read
Significance of Pascal’s Identity
We know the Pascal's Identity very well, i.e. ncr = n-1cr + n-1cr-1 A curious reader might have observed that Pascal's Identity is instrumental in establishing recursive relation in solving binomial coefficients. It is quite easy to prove the above identity using simple algebra. Here I'm trying to explain it's practical significance. Recap from cou
2 min read
Sum triangle from array
Given an array of integers, print a sum triangle from it such that the first level has all array elements. From then, at each level number of elements is one less than the previous level and elements at the level is be the Sum of consecutive two elements in the previous level. Example : Input : A = {1, 2, 3, 4, 5} Output : [48] [20, 28] [8, 12, 16]
11 min read
Find the dimensions of Right angled triangle
Given H (Hypotenuse) and A (area) of a right angled triangle, find the dimensions of right angled triangle such that the hypotenuse is of length H and its area is A. If no such triangle exists, print "Not Possible". Examples: Input : H = 10, A = 24 Output : P = 6.00, B = 8.00 Input : H = 13, A = 36 Output : Not Possible Approach: Before moving to e
6 min read
Area of a triangle inscribed in a rectangle which is inscribed in an ellipse
Given here is an ellipse with axes length 2a and 2b, which inscribes a rectangle of length l and breadth h, which in turn inscribes a triangle.The task is to find the area of this triangle.Examples: Input: a = 4, b = 3 Output: 12 Input: a = 5, b = 2 Output: 10 Approach: We know the Area of the rectangle inscribed within the ellipse is, Ar = 2ab(Ple
4 min read
Program to find third side of triangle using law of cosines
Given two sides A, B and angle C. Find the third side of the triangle using law of cosines.Examples: Input : a = 5, b = 8, c = 49 Output : 6.04339 In particular, the Law of Cosines can be used to find the length of the third side of a triangle when you know the length of two sides and the angle in between. See here to learn to how to find the value
7 min read
Hosoya's Triangle
The Fibonacci triangle or Hosoya's triangle is a triangular arrangement of numbers based on Fibonacci numbers. Each number is the sum of two numbers above in either the left diagonal or the right diagonal. The first few rows are: The numbers in this triangle follow the recurrence relations Relation to Fibonacci numbers The entries in the triangle s
11 min read
Leibniz harmonic triangle
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 th
7 min read
Trinomial Triangle
The trinomial triangle is a variation of Pascal's triangle. The difference between the two is that an entry in the trinomial triangle is the sum of the three (rather than the two in Pascal's triangle) entries above it : The k-th entry of the n-th row is denoted by : [Tex]\binom nk _2 [/Tex]Rows are counted starting from 0. The entries of the n-th r
15 min read
Find Perimeter of a triangle
Given side (a, b, c) of a triangle, we have to find the perimeter of a triangle. Perimeter : Perimeter of a triangle is the sum of the length of side of a triangle. where a, b, c are length of side of a triangle.Perimeter of a triangle can simply be evaluated using following formula : [Tex]perimeter = (a + b + c) [/Tex] Examples : Input : a = 2.0,
3 min read
Fibonomial coefficient and Fibonomial triangle
Fibonomial Coefficient: In mathematics, the Fibonomial coefficients or Fibonacci-binomial coefficients are defined as[Tex]\binom{n}{k}_f = \frac{F_n F_{n-1}....F_{n-k+1} }{F_k F_{k-1}....F1} = \frac{n_F!}{k_F!(n-k)_F!} [/Tex]where n and k are non-negative integers, 0 ? k ? n, Fj is the j-th Fibonacci number and n!F is the nth Fibonorial, where 0!F,
11 min read
Program to calculate area and perimeter of equilateral triangle
An equilateral triangle is a triangle in which all three sides and angles are equal. All three internal angles of equilateral triangle measures 60 degree. If we know the length of each sides of equilateral triangle, then we can use below mentioned formula to calculate area of equilateral triangle.Area of Equilateral Triangle = (sqrt(3)/4) * a * a I
4 min read
Check whether triangle is valid or not if sides are given
Given three sides, check whether triangle is valid or not. Examples: Input : a = 7, b = 10, c = 5 Output : Valid Input : a = 1 b = 10 c = 12 Output : Invalid Approach: A triangle is valid if sum of its two sides is greater than the third side. If three sides are a, b and c, then three conditions should be met. 1.a + b > c 2.a + c > b 3.b + c
4 min read
Program to print Fibonacci Triangle
Given the value of n(n < 10), i.e, number of lines, print the Fibonacci triangle. Examples: Input : n = 5 Output : 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 Input : n = 7 Output : 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 The Fibonacci numbers are the numbers in the fol
7 min read
Sierpinski triangle
Sierpinski triangle is a fractal and attractive fixed set with the overall shape of an equilateral triangle. It subdivides recursively into smaller triangles. Examples : Input : n = 4 Output : * * * * * * * * * Input : n = 8 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * Approach : Sierpinski Triangle will be constructed from an equ
6 min read
Time required to meet in equilateral triangle
Given length of sides of equilateral triangle (s), and velocities(v) of each animal tagged on the vertices of triangle, find out the time after which they meet, if they start moving towards their right opposite, forming a trajectory. Examples: Input: s = 2, v = 5Output: 0.266667 Input: s = 11, v = 556Output: 0.013189 Approach : To find the total am
3 min read