Last active
December 21, 2019 21:19
-
-
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
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
#!/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