Java Program to Find Count of Different Straight Lines with Total n Points With m collinear

bookmark

public class Main
{
    public static void main(String[] args)
    {
        int n =5, m=3;
        // Prints the total number of lines
        System.out.println("The number of straight lines with total of "+n+" points and "
        +m+" collinear points is "+straightLinesCount(m,n));
    }
    // Returns number of straight lines
    public static int straightLinesCount(int m, int n)
    {
        int lines = 0;
        // Calculates the number of lines 
        lines = combination(n,2) - combination(m,2) + 1;
        return lines;
    }
    // Function that returns the number of combinations
    public static int combination(int n, int r)
    {
        return factorial(n) / (factorial(r) * factorial(n - r));
    }
    // Function that returns the factorial of the number
    public static int factorial(int number) {
        int res = 1;
        for (int factor = 2; factor <= number; factor++) 
        {
            res *= factor;
        }
        return res;
    }

 


Output:

The number of straight lines with total of 5 points and 3 collinear points is 8