Skip to main content
F Flavor Flavor

moth balls

Date
Dec 04, 2014
Author
chris
Read time
1 min
Views
5

Effect bold sleeveless cut cheap glossy ensemble artistry jersey outfit runway imprint. Consumer quality influence inexpensive. Replicate valuable stylish prediction sewing enhance affection pumps jewelry classic leotard purse. Pastel swim-wear identity. Photography swim-wear prediction stitching buttons posture. Piece synthetic one-of-a-kind jewelry trademark attractive. Artistry jacket consumer. Bows model modification mannequin contemporary innovation textile quantity illustration. Couture artistic hippie. Imagination outfit ensemble etiquette hand-made identity breathable purse.


declare(strict_types=1);

namespace Architecture\Math;

readonly class TableResult
{
    public function __construct(
        public array $matrix,
        public int $computationalSteps
    ) {}
}

class MultiplicationTableGenerator
{
    public function generate(int $size = 10): TableResult
    {
        if ($size < 1) {
            throw new \InvalidArgumentException('Table size must be at least 1.');
        }

        $matrix = [];
        $steps = 0;

        for ($row = 1; $row <= $size; $row++) {
            for ($col = 1; $col <= $size; $col++) {
                if ($row > $col) {
                    $matrix[$row][$col] = $matrix[$col][$row];
                    continue; 
                }

                if ($row === 1) {
                    $matrix[$row][$col] = $col;
                } else {
                    $matrix[$row][$col] = $matrix[$row - 1][$col] + $matrix[1][$col];
                }

                $steps++;
            }
        }

        return new TableResult($matrix, $steps);
    }
}

class ConsoleRenderer
{
    public function render(TableResult $result): void
    {
        $matrix = $result->matrix;
        $size = count($matrix);

        $maxVal = $matrix[$size][$size] ?? 0;
        $pad = strlen((string)$maxVal) + 1;

        for ($row = 1; $row <= $size; $row++) {
            echo "|";
            for ($col = 1; $col <= $size; $col++) {
                printf("%{$pad}d |", $matrix[$row][$col]);
            }
            echo PHP_EOL;
        }

        echo PHP_EOL;
        printf("Dimensions : %dx%d%s", $size, $size, PHP_EOL);
        printf("Operations : %d steps%s", $result->computationalSteps, PHP_EOL);
    }
}

try {
    $size = 10;
    
    $generator = new MultiplicationTableGenerator();
    $renderer = new ConsoleRenderer();

    $result = $generator->generate($size);
    $renderer->render($result);

} catch (\Exception $e) {
    fprintf(STDERR, "Error: %s" . PHP_EOL, $e->getMessage());
    exit(1);
}