Skip to content

Instantly share code, notes, and snippets.

@laxman954
Last active July 4, 2016 11:47
Show Gist options
  • Save laxman954/7d4b5c371abaf8a369713e53bc048879 to your computer and use it in GitHub Desktop.
Save laxman954/7d4b5c371abaf8a369713e53bc048879 to your computer and use it in GitHub Desktop.
Triangle Number
package com.lp.mysample;
import java.math.BigInteger;
/**
* @author lperumalm
* @version 1.0 This class print Triangular number in matrix format, change
* dimension as your need See
* {@link https://www.mathsisfun.com/algebra/triangular-numbers.html} ,
* {@link https://en.wikipedia.org/wiki/Triangular_number}
*
*/
public class Triangularnumber {
public static void main(String[] args) {
int dimension = 200;
int value = 1;
BigInteger number[][] = new BigInteger[dimension][dimension];
for (int i = 0; i < dimension * dimension; i++) {
for (int j = 0; j <= i; j++) {
int k = i - j;
if (k < dimension && j < dimension) {
number[k][j] = BigInteger.valueOf(value++);
}
}
}
System.out.print("\t");
for (int i = 1; i <= dimension; i++) {
System.out.print(i + "\t");
}
System.out.println("\n");
for (int i = 0; i < dimension; i++) {
System.out.print((i + 1) + "\t");
for (int j = 0; j < dimension; j++) {
System.out.print(number[i][j] + "\t");
}
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment