projecteuler#4 Largest palindrome product

* Palindrome.php

/**
 * Largest palindrome product
 * https://projecteuler.net/problem=4
 * A palindromic number reads the same both ways.
 * The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
 * Find the largest palindrome made from the product of two 3-digit numbers.
-----------------------------------------------------------------------------------------
The palindrome can be written as:
abccba
Which then simpifies to:
 100000a + 10000b + 1000c + 100c + 10b + a
And then:
 100001a + 10010b + 1100c
Factoring out 11, you get:
11(9091a + 910b + 100c)
So the palindrome must be divisible by 11.
Seeing as 11 is prime, at least one of the numbers must be divisible by 11.
 */

<?php

class Palindrome {

    /** @var  int */
    private $width;

    public function __construct($width) {
        $this->width = $width;
    }

    public static function pow(int $m, int $p) {
        if ($p === 0) {
            return 1;
        }
        $t = $m;
        $res = 1;
        for (; $p > 0; $p >>= 1) {
            if ($p & 0x01 !== 0) {
                $res *= $t;
            }
            $t *= $t;
        }
        return $res;
    }

    private function int2digitArray(int $num) : array {
        $a = [];

        while ($num > 0) {
            $digit = $num % 10;
            array_unshift($a, $digit);
            $num = floor($num / 10);
        }
        return $a;
    }

    private function isPalindrome(array $a) : bool {
        for ($i = 0, $j = count($a)-1; $i < $j; $i++, $j--) {
            if ($a[$i] !== $a[$j]) {
                return false;
            }
        }
        return true;
    }

    public function intIsPalindrome(int $num) : bool {
        $a = $this->int2digitArray($num);
        return $this->isPalindrome($a);
    }

    public function largest(int &$maxI, int &$maxJ) {
        $low = pow(10, $this->width-1);
        $m = 10 * $low - 1;

        $max = $product = 0;
        // $i = 999, $j = 990
        for ($i = $m; $i >= $low; $i--) {
            for ($j = $m-9; $j >= $low; $j -= 11) {
                // printf("i=%d, j=%d\n", $i, $j);
                $product = $i * $j;

                if ($product > $max) {
                   //  echo $product . PHP_EOL;
                    if (self::intIsPalindrome($product)) {
                        $max = $product;
                        $maxI = $i;
                        $maxJ = $j;
                    }
                }
            }
        }
        return $max;
    }

}

$p = new Palindrome(3);
$i = $j = 0;
echo $p->largest($i, $j).PHP_EOL;  // 906609
printf("[%d, %d]\n", $i, $j);  // [993, 913]

猜你喜欢

转载自blog.csdn.net/fareast_mzh/article/details/82959824