Pages

Circle Division by Chords

public class CircleDivisionByChords
{
    private static void print(int p, int r)
    {
        final int n = (p * (p - 1)) / 2; // number of chords
        System.out.print(p + " point");
        System.out.print(p == 1 ? ", " : "s, ");
        System.out.print(n + " line");
        System.out.print(n == 1 ? ", " : "s, ");
        System.out.print(r + " region");
        System.out.print(r == 1 ? "\n" : "s\n");
    }
    public static void main(String[] args)
    {
        int p = 0; // number of points
        int r = 1; // number of regions
        print(p, r);
        print(++p, r);
        print(++p, ++r);
        for (int i=1, j=1, k=0; k < 4; k++) {
            j += k;
            i += j;
            r += i;
            print(++p, r);
        }
    }
}