# Avoid the evil integers! [closed]

You are developing some code to generate ID numbers. Policy requires that no ID numbers include the digit sequence 666.

Create a function (or your language's equivalent) which takes a positive integer parameter and returns the next integer that does not include 666 when that integer is expressed in decimal. (60606 is fine, 66600 is not.)

Your code must not use a loop that adds one until it finds a result that fits the rules.

f(1) returns 2.
f(665) returns 667.
f(665999999) returns 667000000 without having looped a million times.
(Following examples added since the question was first posed.)
f(666666666) also returns 667000000.
f(66600) returns 66700.
f(456667) returns 456670.


UPDATE:
Replacing 666 with 667 won't work if there's more than one 666 in the input.

• What about something like 456667? Should that return 456670, or are we only worried about a leading 666? – Kyle Kanos Apr 29 '14 at 14:18
• I think this would be better as code-golf than popularity-contest since there are such straightforward solutions. – Nate Eldredge Apr 29 '14 at 16:08
• @nyuszika7h but the result should be 66700. – Martin Ender Apr 29 '14 at 17:07
• as a real challenge, there should've also been a requirement to not have "666" anywhere in the code – DLeh Apr 30 '14 at 19:18
• Depending on your regex implementation, you could use '6{3}' to detect 666. – celtschk May 1 '14 at 19:33

PHP, 115 characters

<?php
function n($n){$p=strpos(++$n,'666');return($p>=0?substr($n,0,$p). 667 .str_repeat(0,-strlen($n)+$p+3):$n);} ?>  ungolfed: <?php function n($n)
{
$p = strpos(++$n, '666');

return($p >= 0 ? substr($n, 0, $p) . 667 . str_repeat(0, -strlen($n) + $p + 3) :$n
);
}
?>


# Javascript (E6) 47 64

Even if it's not golf

Assuming positive 32 bit integers. NB +a is string concat, -a is numeric sub

F=x=>-(~x+'').replace(/666(.*)/,(s,a)=>667+a-a)


Usage

;[ F(6666), F(1), F(665), F(665999999), F(666666666), F(66600), F(456667) ]

[6670, 2, 667, 667000000, 667000000, 66700, 456670]


# PHP

echo preg_match('/666/', $n+1)?preg_replace('/666/', '667',$n+1):\$n+1;

• Given 123666123, won't this produce 123667123? It should produce 123667000 for that input. – RomanSt Apr 29 '14 at 14:35