{{ message }}

Instantly share code, notes, and snippets.

# mendelgusmao/triangle.php

Created Jun 17, 2011
Generates a numeric pyramid based on a string or a sequence of numbers


Rnd() Trace
"; flush(); foreach (\$num as \$in) { \$res = array(); if (!preg_match("/[0-9]/", \$in)) { \$in = strtolower(\$in); \$in = preg_replace("/[^a-z]/", "", \$in); \$in = correspondentes(\$in); } \$tri = triangulo(\$in, 0, \$op); echo("
"); foreach (\$tri as \$ln) echo "\$ln
"; echo "
"; flush(); if (\$trace) { echo("
");

foreach(\$tri as \$ln) {
\$tra = todos(\$ln);

foreach(\$tra as \$lna)
\$res[\$ln][] = \$lna . (in_array(\$lna, \$tri)
? " +"
: (strpos(\$lna, "0") > 0
? " -"
: ""));

//if (\$i++ == count(\$lna) + 1) break;
}

print_r(\$res);

echo("
"); } echo("
"); } // ------------------------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------------------------ function triangulo(\$x, \$n = 0) { \$ope = \$op == 1 ? "*" : "+"; \$z[] = \$x; while (\$x > 9) { \$y = ""; \$s = strlen(\$x) - 1; \$x = (string) \$x; while(\$s--) { \$e = \$x{\$s + 1} + \$x{\$s}; \$y .= \$e < 10 ? \$e : triangulo(\$e, 1); } \$z[] = \$x = strrev(\$y); } return \$n ? \$z[1] : array_reverse(\$z); } function correspondentes(\$s) { \$x = ""; \$y = strlen(\$s); while (\$y--) \$x .= ord(substr(\$s, \$y, 1)) - 96; return strrev(\$x); } function todos(\$prova) { \$base = strlen(\$prova) + 1; \$off = \$base - 2; \$ini = str_repeat(1, \$base); \$fim = str_repeat(9, \$base); for (\$i = \$ini; \$i <= \$fim; \$i++) { \$tri = triangulo(\$i); \$res = \$tri[\$off]; // if (/*!strpos(\$i, "0") && !(\$i % 10 == 0) &&*/ \$res == \$prova) if (\$res == \$prova) \$x[] = \$i; } return \$x; } ?>
to join this conversation on GitHub. Already have an account? Sign in to comment