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]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

fareast_mzh

打赏个金币

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值