Skip to content

Instantly share code, notes, and snippets.

@guweigang
Last active December 21, 2015 16:09
Show Gist options
  • Save guweigang/6331876 to your computer and use it in GitHub Desktop.
Save guweigang/6331876 to your computer and use it in GitHub Desktop.
<?php
function seq($width, $lastNum, $inc)
{
$num = $lastNum;
if($inc==true) {
if($lastNum+$width>10) {return false;}
} else {
if($lastNum-$width<0) {return false;}
}
$num = 0;
for($i=0;$i<$width;++$i) {
if($inc) {
$nextNum = $lastNum + $i;
} else {
$nextNum = $lastNum - $i;
}
$num += pow(10, $i) * $nextNum;
}
return $num;
}
function match($ID, $minWidth) {
$ID = intval($ID);
$width = ceil(log10($ID));
for($i=$width;$i>=$minWidth;--$i) {
for($j=0; $width-$j>=$i;++$j) {
$ID2 = intval($ID / pow(10, $j));
$widthNum = $ID2 % pow(10, $i);
$lastNum = $widthNum % 10;
$inc = seq($i, $lastNum, true);
$des = seq($i, $lastNum, false);
if($widthNum === $inc || $des === $widthNum) {
return $widthNum;
}
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment