// This program counts the occurrences of the last digit of the triangle // numbers. The nth triangle number is equal to the sum of the first n // integers (1 + 2 + 3 + ... + n) and is also equal to n * (n + 1) / 2. public class TriangleCount { public static void main(String[] args) { int[] count = new int[100]; for (int i = 1; i <= 10000; i++) { int next = i * (i + 1) / 2; int digits = next % 100; count[digits]++; if (i % 200 == 0) { System.out.print("."); } } System.out.println(); for (int i = 0; i < count.length; i++) { if (count[i] != 0) { System.out.println("count for " + i + " = " + count[i]); } } } }