53
\$\begingroup\$

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.

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

33 Answers 33

1 2
0
\$\begingroup\$

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
    );
}
?>
\$\endgroup\$
0
\$\begingroup\$

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]
\$\endgroup\$
-2
\$\begingroup\$

PHP

echo preg_match('/666/', $n+1)?preg_replace('/666/', '667', $n+1):$n+1;
\$\endgroup\$
  • 5
    \$\begingroup\$ Given 123666123, won't this produce 123667123? It should produce 123667000 for that input. \$\endgroup\$ – RomanSt Apr 29 '14 at 14:35
1 2

Not the answer you're looking for? Browse other questions tagged or ask your own question.