Last active
January 20, 2025 01:14
-
-
Save elazar/4433535c230d3333d047db58994ad28c to your computer and use it in GitHub Desktop.
Power of Two Triangle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* To run this: | |
* | |
* 1. Compile it. | |
* | |
* javac MultiplicativeTriangle.java | |
* | |
* 2. Run it. | |
* | |
* java MultiplicativeTriangle | |
*/ | |
public class MultiplicativeTriangle { | |
// Calculate the number of columns for a given row | |
public static int columns(int row) { | |
return 2 * row - 1; | |
} | |
// Generate spaces as a string | |
public static String space(int n) { | |
return n > 0 ? " ".repeat(n) : ""; | |
} | |
public static void main(String[] args) { | |
int rows = 7; | |
// Precompute powers of 2 up to the maximum value required | |
int[] powers = new int[rows + 1]; | |
for (int power = 0; power <= rows; power++) { | |
powers[power] = (int) Math.pow(2, power); | |
} | |
// Compute the maximum row width (for centering) | |
int maxRowWidth = 0; | |
for (int power = 0; power < rows; power++) { | |
maxRowWidth += String.valueOf(powers[power]).length() * 2; | |
} | |
maxRowWidth += String.valueOf(powers[rows - 1]).length() + (columns(rows) - 1); | |
// Generate and print each row | |
for (int row = 1; row <= rows; row++) { | |
int columns = columns(row); | |
// Compute the current row's width | |
int currentRowWidth = 0; | |
for (int i = 0; i < row - 1; i++) { | |
currentRowWidth += String.valueOf(powers[i]).length() * 2; | |
} | |
currentRowWidth += String.valueOf(powers[row - 1]).length() + (columns - 1); | |
// Center the row | |
int middle = (maxRowWidth - currentRowWidth) / 2; | |
int before = (int) Math.ceil(middle); | |
int after = (int) Math.floor(middle); | |
// Print leading spaces | |
System.out.print(space(before)); | |
// Print the values for the row | |
for (int column = 1; column <= columns; column++) { | |
int power = column < row ? column - 1 : columns - column; | |
System.out.print(powers[power]); | |
if (column < columns) { | |
System.out.print(" "); | |
} | |
} | |
// Print trailing spaces and row terminator | |
System.out.print(space(after) + " @\n"); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function columns(int $row): int { | |
return 2 * $row - 1; | |
} | |
function space(int $n): string { | |
return $n > 0 ? str_repeat(' ', $n) : ''; | |
} | |
$rows = 7; | |
$powers = array_map(fn(int $power) => 2**$power, range(0, $rows)); | |
$width = fn(int $row): int => array_sum(array_map('strlen', array_slice($powers, 0, $row - 1))) * 2 + strlen($powers[$row - 1]) + (columns($row) - 1); | |
for ($row = 1; $row <= $rows; $row++) { | |
$columns = columns($row); | |
$length = $width($row); | |
$middle = ($width($rows) - $length) / 2; | |
$before = ceil($middle); | |
$after = floor($middle); | |
echo space($before); | |
for ($column = 1; $column <= $columns; $column++) { | |
$power = $column < $row ? $column - 1: $columns - $column; | |
echo $powers[$power], ' '; | |
} | |
echo space($after) . " @\n"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment