Número de triángulos en un plano si no más de dos puntos son colineales

Dados n puntos en un plano y no más de dos puntos son colineales, la tarea es contar el número de triángulos en un plano dado.
Ejemplos: 
 

Input :  n = 3
Output : 1

Input :  n = 4
Output : 4

Number of Triangles

Si hay n puntos en un plano y no hay tres o más puntos colineales, entonces el número de triángulos en el plano dado está dado por ^{n}\textrm{C}_{3} = \frac{n(n-1)(n-2)}{6}
 

C++

// C++ program to find the number of
// triangles in a plane if no more
// then two points are collinear.
#include <bits/stdc++.h>
using namespace std;
 
// Function to find number of triangles
// in a plane.
int countNumberOfTriangles(int n)
{
 
    // Formula to find number of triangles
    // nC3 = n * (n - 1) * (n - 2) / 6
    return n * (n - 1) * (n - 2) / 6;
}
 
// Driver code
int main()
{
    int n = 4;
    cout << countNumberOfTriangles(n);
    return 0;
}

C

// C program to find the number of
// triangles in a plane if no more
// then two points are collinear.
#include <stdio.h>
 
// Function to find number of triangles
// in a plane.
int countNumberOfTriangles(int n)
{
    // Formula to find number of triangles
    // nC3 = n * (n - 1) * (n - 2) / 6
    return n * (n - 1) * (n - 2) / 6;
}
 
// Driver code
int main()
{
    int n = 4;
    printf("%d",countNumberOfTriangles(n));
    return 0;
}
 
// This code is contributed by kothavvsaakash.

Java

// Java program to find the number of
// triangles in a plane if no more
// then two points are collinear.
import java.io.*;
 
class GFG {
 
    // Function to find number of triangles
    // in a plane.
    static int countNumberOfTriangles(int n)
    {
 
        // Formula to find number of triangle
        // nC3 = n * (n - 1) * (n - 2) / 6
        return n * (n - 1) * (n - 2) / 6;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int n = 4;
 
        System.out.println(
            countNumberOfTriangles(n));
    }
}

Python3

# Python3 program to find
# the number of triangles
# in a plane if no more
# then two points are collinear.
 
# Function to find number
# of triangles in a plane.
def countNumberOfTriangles(n) :
     
    # Formula to find
    # number of triangles
    # nC3 = n * (n - 1) *
    # (n - 2) / 6
    return (n * (n - 1) *
                (n - 2) // 6)
 
# Driver Code
if __name__ == '__main__' :
     
    n = 4
    print(countNumberOfTriangles(n))
 
                 
# This code is contributed
# by ajit

C#

// C# program to find the
// number of triangles in
// a plane if no more then
// two points are collinear.
using System;
 
class GFG
{
 
    // Function to find number
    // of triangles in a plane.
    static int countNumberOfTriangles(int n)
    {
 
        // Formula to find number
        // of triangle
        // nC3 = n * (n - 1) *
        //           (n - 2) / 6
        return n * (n - 1) *
                   (n - 2) / 6;
    }
 
    // Driver code
    public static void Main()
    {
        int n = 4;
 
        Console.WriteLine(
            countNumberOfTriangles(n));
    }
}
 
// This code is contributed by anuj_67.

PHP

<?php
// PHP program to find the
// number of triangles in a
// plane if no more then
// two points are collinear.
 
// Function to find number
// of triangles in a plane.
function countNumberOfTriangles($n)
{
    // Formula to find number
    // of triangles nC3 = n *
    // (n - 1) * (n - 2) / 6
    return $n * ($n - 1) *
                ($n - 2) / 6;
}
 
// Driver code
$n = 4;
echo countNumberOfTriangles($n);
 
// This code is contributed
// by anuj_67.
?>

Javascript

<script>
// javascript program to find the number of
// triangles in a plane if no more
// then two points are collinear.
 
    // Function to find number of triangles
    // in a plane.
    function countNumberOfTriangles(n)
    {
 
        // Formula to find number of triangle
        // nC3 = n * (n - 1) * (n - 2) / 6
        return n * (n - 1) * (n - 2) / 6;
    }
 
    // Driver code
        var n = 4;
        document.write(countNumberOfTriangles(n));
 
// This code is contributed by aashish1995
</script>
Producción: 

4

 

Publicación traducida automáticamente

Artículo escrito por KRV y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *