Created
February 16, 2016 13:49
-
-
Save masayuki5160/6fceb218ecf025ec3ca2 to your computer and use it in GitHub Desktop.
プログラマ脳を鍛える数学パズルQ02
This file contains hidden or 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
<?php | |
error_reporting(0);// division by zeroのワーニング回避 | |
$expression = array("+","-","*","/",""); | |
for($num = 1000; $num <= 9999;$num++){ | |
foreach($expression as $i){ | |
// 1つめの間 | |
foreach($expression as $j){ | |
// 2つ目の間 | |
foreach($expression as $k){ | |
// 3つめの間 | |
$tmp_expression = substr($num,0,1). $i. substr($num, 1,1). $j. substr($num,2,1). $k. substr($num, 3,1); | |
$calc_res = eval("return {$tmp_expression};"); | |
if($calc_res == strrev($num) && strlen($calc_res) >= 4 && (!empty($i) || !empty($j) || !empty($k))){ | |
print("base=".$num.",strrev = ".strrev($num).", calc_res = {$calc_res}, expression={$tmp_expression}\n"); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
結果
$ php -f q02.php
base=5931,strrev = 1395, calc_res = 1395, expression=5_9_31