package org.mokai.ninjastar; import java.math.BigDecimal; import java.math.RoundingMode; public class NinjaStarNumbers { public static void main(String[] args){ //put the number of iterations you want to print here. My laptop has 16gb ram and 2000 took 5min int iterations = 200; //put how many iterations to print the table to. After about 20 my formatting breaks down. int tableDisplay = 20; tableDisplay++; BigDecimal[][] table; BigDecimal[][] sequences; table = generateTable(iterations); sequences = generateSequences(table, iterations); printTable(table, tableDisplay); printSequences(table, sequences, tableDisplay); printValues(sequences, iterations); } //end main static public void printTable(BigDecimal[][] table, int iterations){ System.out.println("TL_h(n). The number of new triangles produced at level h in iteration n. rows h, columns n."); printTableln(table, iterations, 0); System.out.println(); for(int k = 1; k < iterations-1;k++){ printTableln(table, iterations, k); System.out.println(); } System.out.println(); } //end printTable static public void printValues(BigDecimal[][] sequences, int iterations){ System.out.println("Then at n = " + (iterations - 1) + " ..."); System.out.println("Sum area of colored triangles = " + sequences[4][iterations - 1]); System.out.println("by the way ........... 80/17 ~= " + BigDecimal.valueOf(80).divide(BigDecimal.valueOf(17), 1000, BigDecimal.ROUND_HALF_UP)); System.out.println(); System.out.println("The total area of the ninjastar = " + sequences[3][iterations - 1].stripTrailingZeros()); System.out.println(); System.out.println("And total colored area / total area = " + sequences[5][iterations - 1]); System.out.println("by the way .................. 8/17 ~= " + BigDecimal.valueOf(8).divide(BigDecimal.valueOf(17), 1000, BigDecimal.ROUND_HALF_UP)); System.out.println(); } //end printValues static public void printSequences(BigDecimal[][] table, BigDecimal[][] sequences, int iterations){ System.out.println("Total number of new triangles produced in each iteration n: "); printTableln(table, iterations, 0); System.out.println(); printTableln(sequences, iterations, 0); System.out.println(); System.out.println(); System.out.println("Total number of triangles present in each iteration n: "); printTableln(table, iterations, 0); System.out.println(); printTableln(sequences, iterations, 1); System.out.println(); System.out.println(); System.out.println("Area of each colored triangle produced in iteration n: "); System.out.print(sequences[2][1]); for(int n = 2; n < iterations; n++) System.out.print(", " + sequences[2][n]); System.out.println(); System.out.println(); System.out.println("Area of whole ninja star at iteration n: "); System.out.print(sequences[3][1]); for(int n = 2; n < iterations; n++) System.out.print(", " + sequences[3][n].stripTrailingZeros()); System.out.println(); System.out.println(); System.out.println("Total area of colored triangles contained within the ninja star at iteration n: "); System.out.print(sequences[4][1]); for(int n = 2; n < iterations; n++) System.out.print(", " + sequences[4][n]); System.out.println(); System.out.println(); System.out.println("The fraction of area in the ninja star that is colored at iteration n: "); System.out.print(sequences[5][1].stripTrailingZeros()); for(int n = 2; n < iterations; n++) System.out.printf(", %.5f", sequences[5][n].stripTrailingZeros().doubleValue()); System.out.println(); System.out.println(); System.out.println("Total number of new triangles produced in each iteration n(list): "); System.out.print(sequences[0][1]); for(int n = 2; n < 200; n++) System.out.print(", " + sequences[0][n]); System.out.println(); System.out.println(); System.out.println("Total number of triangles present in each iteration n(list): "); System.out.print(sequences[1][1]); for(int n = 2; n < 200; n++) System.out.print(", " + sequences[1][n]); System.out.println(); System.out.println(); } //end printSequence static public void printTableln(BigDecimal[][] table, int n, int k){ for(int i = 0; i < n; i++){ if(table[k][i] == null){ table[k][i] = BigDecimal.valueOf(0); } if(i <= 6) System.out.printf("%5s", table[k][i]); else if(i <= 10) System.out.printf("%8s", table[k][i]); else if(i <= 16) System.out.printf("%11s", table[k][i]); else if(i <= 20) System.out.printf("%13s", table[k][i]); else if(i <= 36) System.out.printf("%20s", table[k][i]); else System.out.printf("%30s", table[k][i]); } //end for } // endPrintTableLn static public BigDecimal[][] generateTable(int nMax){ BigDecimal[][] tl; //TL_k(n) refers to the number of of Triangles produced at Level k //of iteration n tl = new BigDecimal[nMax][nMax]; tl[0][0] = BigDecimal.valueOf(0); tl[0][1] = BigDecimal.valueOf(1); tl[0][2] = BigDecimal.valueOf(2); tl[1][0] = BigDecimal.valueOf(1); tl[1][1] = BigDecimal.valueOf(1); tl[1][2] = BigDecimal.valueOf(3); for(int n = 3; n < nMax; n++){ tl[0][n] = tl[0][n-1].add(BigDecimal.valueOf(1)); tl[1][n] = tl[1][n-1].multiply(BigDecimal.valueOf(2)); } for(int k = 2; k < nMax;k++){ tl[k][0] = tl[k-1][0].add(BigDecimal.valueOf(1)); for(int n = k + 1; n < nMax; n++) tl[k][n] = add(tl, 1, k-1, k-1).multiply(BigDecimal.valueOf(3)).multiply(tl[1][n-k]); } return tl; } //end generateTable static public BigDecimal[][] generateSequences(BigDecimal[][] tl, int nMax){ BigDecimal[][] sequences = new BigDecimal[9][nMax]; //Total number of new triangles at iteration n for(int n = 1; n < nMax; n++){ sequences[0][n] = add(tl, 1, n, n); } //Total number of triangles present at iteration n sequences[1][1] = BigDecimal.valueOf(1); for(int n = 2; n < nMax; n++){ sequences[1][n] = sequences[1][n-1].add(sequences[0][n]); } //Area of each triangle produced in iteration n sequences[2][1] = BigDecimal.valueOf(1); for(int n = 2; n < nMax; n++){ sequences[2][n] = sequences[2][n-1].multiply(BigDecimal.valueOf(0.25)); } //Area of whole ninja star at iteration n sequences[3][1] = BigDecimal.valueOf(4); for(int n = 2; n < nMax; n++){ sequences[3][n] = sequences[3][n-1].add(sequences[2][n].multiply(tl[1][n]).multiply(BigDecimal.valueOf(4))); } //Total colored area in the ninja star at iteration n sequences[4][0] = BigDecimal.valueOf(0); for(int n = 1; n < nMax; n++){ sequences[4][n] = sequences[4][n-1].add(sequences[0][n].multiply(sequences[2][n])); } //The percentage of area in the ninja star that is colored sequences[4][0] = BigDecimal.valueOf(0); for(int n = 1; n < nMax; n++){ sequences[5][n] = sequences[4][n].divide(sequences[3][n], 1000, RoundingMode.HALF_UP); } return sequences; } // end generateSequences /** * * * @param BigDecimal arr[][] * @param int a * @param int b * @param int c * @return sum from index a to b over cth row or column in a 2D array */ public static BigDecimal add(BigDecimal arr[][], int a, int b, int c){ BigDecimal sum = new BigDecimal("0"); for(int i = a; i <= b; i++){ if(arr[i][c] == null){ arr[i][c] = BigDecimal.valueOf(0); } sum = sum.add(arr[i][c]); } //System.out.println(sum); return sum; } // end add } //end NinjaStar