Skip to content

Instantly share code, notes, and snippets.

@masayuki5160
Created February 16, 2016 13:49
Show Gist options
  • Save masayuki5160/6fceb218ecf025ec3ca2 to your computer and use it in GitHub Desktop.
Save masayuki5160/6fceb218ecf025ec3ca2 to your computer and use it in GitHub Desktop.
プログラマ脳を鍛える数学パズルQ02
<?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");
}
}
}
}
}
@masayuki5160
Copy link
Author

結果

$ php -f q02.php
base=5931,strrev = 1395, calc_res = 1395, expression=5_9_31

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment