Skip to content

Instantly share code, notes, and snippets.

@dansouza
Last active December 21, 2019 21:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dansouza/c27c943139faf8180acdee542e17317b to your computer and use it in GitHub Desktop.
Save dansouza/c27c943139faf8180acdee542e17317b to your computer and use it in GitHub Desktop.
outputs an addition and multiplication table for kids that is shorter to memorize because it doesn't have repeated values
#!/usr/bin/php
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
# outputs an addition table for kids that is shorter to memorize because
# it doesn't have repeated values
function fill(&$table, $x, $sign, $y) {
$min = min($x, $y);
$max = max($x, $y);
if (isset($table[$min][$max])) {
return;
}
if ($sign == '+') {
$table[$min][$max] = ($min + $max);
} else if ($sign == '*') {
$table[$min][$max] = ($min * $max);
} else {
die("invalid sign: $sign");
}
}
function dump($table, $sign = '+') {
foreach ($table as $x => $list) {
foreach ($list as $y => $value) {
print "{$x}{$sign}{$y}={$value}\n";
}
echo "\n";
}
}
foreach (['*', '+'] as $sign) {
$table = array();
for ($x = 1; $x <= 10; $x++) {
for ($y = 1; $y <= 10; $y++) {
fill($table, $x, $sign, $y);
}
}
dump($table, $sign);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment