41
\$\begingroup\$

Write a program or function that takes in a positive integer. You can assume the input is valid and may take it as a string. If the number is any of

123
234
345
456
567
678
789

then output a truthy value. Otherwise, output a falsy value. For example, the inputs

1
2
3
12
122
124
132
321
457
777
890
900
1011
1230
1234

must all result in falsy output. (The input will not have leading zeroes so you needn't worry about things like 012.)

The shortest code in bytes wins.

\$\endgroup\$
6
  • \$\begingroup\$ Oh, strings are allowed? What about digit arrays? \$\endgroup\$
    – Dennis
    Aug 30, 2016 at 3:30
  • \$\begingroup\$ @Dennis No. Let's keep it to plain strings or plain ints. \$\endgroup\$ Aug 30, 2016 at 3:30
  • 6
    \$\begingroup\$ If I take string input, should I handle 012? \$\endgroup\$
    – lynn
    Aug 30, 2016 at 4:30
  • 1
    \$\begingroup\$ @Lynn No. 012 would be falsy but you can assume it is not input. \$\endgroup\$ Aug 30, 2016 at 4:45
  • 1
    \$\begingroup\$ @BradGilbertb2gills No. It should just satisfy the linked definition of truthy/falsy - meta.codegolf.stackexchange.com/questions/2190/… \$\endgroup\$ Aug 30, 2016 at 4:46

81 Answers 81

1 2
3
1
\$\begingroup\$

JavaScript (Node.js), 17 bytes

n=>n==n[0]*111+12

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Pip, 8 bytes

^a=@a+,3

Attempt This Online!

Explanation

^a=@a+,3
^a       ; Split argument into list of digits
  =      ; That list numerically equals
      ,3 ; Range(3)
   @a+   ; plus first digit of argument
\$\endgroup\$
0
\$\begingroup\$

PHP, 35 bytes

<?=($n=$argv[1]-99)<790&$n%111==24;

exploiting that the modulo of a negative number is never positive in PHP

\$\endgroup\$
0
\$\begingroup\$

C 61 Bytes

f(char*v){puts(v[3]==0&v[0]+1==v[1]&v[1]+1==v[2]?"T":"F");}
g(v){puts(v<790&v%111==12?"T":"F");}
main(c, v)char**v;{
    f(v[1]);
    g(atoi(v[1]));
}

61 Bytes only includes the function. f(...) 36 Bytes using the trick from Geobits in g(...)

\$\endgroup\$
0
\$\begingroup\$

GNU Octave : 74 bytes

golfed:

function r=g(a)t=conv(a-'0',[1,-1],'valid')==1;r=(sum(t)==2).*prod(t);end;

ungolfed:

function r=golf_123(a)
  t=conv(a-'0',[1,-1],'valid')==1;
  r=(sum(t)==2).*prod(t);
endfunction

Explanation: Treat the string as an integer vector, any two consecutive digits will then have derivative given the finite difference filter [1,-1] equal to 1 at each valid convolution output. The prod command calculates product, so it will short circuit to 0 if at least one entry is 0. The sum(t)==2 disqualifies any run of increasing digits longer or shorter than 3.

\$\endgroup\$
0
\$\begingroup\$

JAVA 7, 57 bytes

boolean f(int x){return (int)Math.log(x)-1==3&x%111==12;}
\$\endgroup\$
1
  • \$\begingroup\$ You can remove the space in return (int) \$\endgroup\$
    – Cyoce
    Dec 5, 2016 at 15:45
0
\$\begingroup\$

SpecBAS - 35 bytes

1 INPUT n: ?(n-12)/111 in [1 TO 7]

Uses the same formula as the Powershell answer, returns 0 for False and 1 for True.

My original version was 59 bytes (and seems more "honest" for being my own work). Input as number and converted to string to avoid "012" showing as True.

1 INPUT n: n$=STR$ n: ?LEN n$=3 AND POS(n$,"0123456789")>0
\$\endgroup\$
0
\$\begingroup\$

C# 6 (36 bytes)

bool f(int x)=>x<790&0==(x+321)%111;
\$\endgroup\$
1
  • \$\begingroup\$ Is f necessary? \$\endgroup\$
    – Cyoce
    Dec 4, 2016 at 0:22
0
\$\begingroup\$

C, 71 bytes

Taking the input as int:

f,d,c;F(n){for(f=c=0;n;)d=n%10,c++,f|=d-(n/=10)%10-1&&n;return!f&c==3;}

Test main:

int main() {
  printf("%d\n", F(123));
  printf("%d\n", F(234));
  printf("%d\n", F(345));
  printf("%d\n", F(456));
  printf("%d\n", F(567));
  printf("%d\n", F(678));
  printf("%d\n", F(789));
  printf("%d\n", F(1));
  printf("%d\n", F(2));
  printf("%d\n", F(3));
  printf("%d\n", F(12));
  printf("%d\n", F(122));
  printf("%d\n", F(124));
  printf("%d\n", F(321));
  printf("%d\n", F(457));
  printf("%d\n", F(777));
  printf("%d\n", F(890));
  printf("%d\n", F(900));
  printf("%d\n", F(1011));
  printf("%d\n", F(1230));
  printf("%d\n", F(1234));
}
\$\endgroup\$
0
\$\begingroup\$

Ruby, 114 bytes

i = gets.chomp.to_i
l = [123, 234, 345, 456, 567, 678, 789]
y=false
l.each {|x| if i==x then y=true end;}
puts y

I don't know how many bytes this answer is, but I'm guessing it's not that small.

\$\endgroup\$
2
  • \$\begingroup\$ Do you need those spaces? \$\endgroup\$
    – Blue
    Sep 1, 2016 at 12:46
  • 1
    \$\begingroup\$ You can remove all but one space (puts y)..each can be .map. false can be p. if i==x then y=true end; can be i==x&&y=1 \$\endgroup\$
    – Cyoce
    Dec 4, 2016 at 0:09
0
\$\begingroup\$

Clojure, 48 bytes

(defn f [n] (= n ((set (range 123 900 111)) n)))

Test:

user=> (map (fn [n] [n (f n)]) [123 234 345 456 567 678 789])
([123 true]
 [234 true]
 [345 true]
 [456 true]
 [567 true]
 [678 true]
 [789 true])

user=> (map (fn [n] [n (f n)]) [1 2 3 12 122 124 132 321 457 777 890 900 1011 1230 1234])
([1 false]
 [2 false]
 [3 false]
 [12 false]
 [122 false]
 [124 false]
 [132 false]
 [321 false]
 [457 false]
 [777 false]
 [890 false]
 [900 false]
 [1011 false]
 [1230 false]
 [1234 false])
\$\endgroup\$
1
  • \$\begingroup\$ I swear I didn't copy your answer! This can be significantly reduced. See my answer which is the same idea, but without all of the extra bits. \$\endgroup\$ Feb 14, 2017 at 20:43
0
\$\begingroup\$

PHP 5.6, 48 bytes

<?=($a=$argv[1])==join("",range($a[0],$a[0]+2));

Wanted to try something a little different from the other PHP answers (and a different approach to most of the posted answers). Take the first number of the input string, add two to it and create a range array, then join those together to create a new string and then return if they're equal or not.

Run with:

echo '<?=($a=$argv[1])==join("",range($a[0],$a[0]+2));' | php -- 123
\$\endgroup\$
0
\$\begingroup\$

><> (Fish), 28 Bytes

l3-0=?v0n;>
?+1-@:<;n1^?+2-!

Input is via an execution argument to add each digit to stack, left to right.

Checks if the length of the stack (number) is three, then that there's a difference of one between the third and second digits, then that there's a difference of two between the first and third digits.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Jelly, 8 bytes

Not as short as Dennis's answer (duh), but I'm quite proud of myself for getting the results I wanted out of Jelly.

9Rṡ3
De¢

Try it online!

Explanation:

9R    Push 1-9 to the stack
ṡ3    Slice up the stack in parts of 3, overlapping
      This gives us [1,2,3], [2,3,4], ..., [7,8,9]
D     Convert input to list ( '123' -> [1, 2, 3])
e¢    This checks to see if the decimal list is in the list of our previous link.
\$\endgroup\$
0
\$\begingroup\$

Clojure, 25 bytes

(set (range 123 790 111))

Basically the Haskell answer. Creates a range which includes all the truthy answers, then converts it into a set. Sets are functions in Clojure (they implement the function interface and can be called).

Returns the number as the truthy return (every number is truthy in Clojure), and nil for the falsey value.

Use:

(let [s (set (range 123 790 111))]

    (s 123)
    123

    (s 234)
    234

    (s 993)
    nil
\$\endgroup\$
0
\$\begingroup\$

SmileBASIC, 27 bytes

INPUT N?N-12==N DIV&H64*111

Using Dennis's algorithm.

If forcing the user to add &H to their input is OK, this works for 24 bytes:

INPUT N?N-18==(N>>8)*273
\$\endgroup\$
0
\$\begingroup\$

QBIC, 21 bytes

;[7|~!12+111*a$=A|_Xq

This prints 1 if a match is found, nothing otherwise.

Explanation

;              Get A$ from the command line
[7|            FOR a=1; a <=7 ; a++ (for each of the 7 sequences)
  12+111*a     Generate the number 123, 234, 345 by mult. 111 x index, plus 12
 !        $    Cast that to string B$
~          =A  And IF that matches A$
|_Xq           THEN quit, printing 1
               IF and FOR loop implicitly closed
\$\endgroup\$
0
\$\begingroup\$

AWK, 33 28 27 Bytes

{$0=($1-12)/111~/^[1-7]$/}1

Prints 1 for corresponds to one of the desired values, 0 otherwise.

Updated answer is based on TimmyD's powershell answer.

Example usage:

awk '{$0=($1-12)/111~/^[1-7]$/}1' <<< 789

Prints 1

awk '{$0=($1-12)/111~/^[1-7]$/}1' <<< 12

Prints 0

Since this is using division rather than modulus, the result will be an integer for the desired values, but that integer needs to be between 1 and 7.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 21 bytes. Echoes for truthy, ignores for falsey. \$\endgroup\$
    – user100411
    Mar 30, 2021 at 9:11
0
\$\begingroup\$

Befunge-93, 57 53 bytes

#@ #.0 &# ::57*2+3*%34*-#<_# 34*`!#<_# 98*34**`#<_1.@

Try it online!

Explanation

#@ #.0        skip the terminator and output command. Adds a meaningless zero to 
                the stack.
&# ::         take in an input and add two more copies to the stack
57*2+3*%      adds input mod 111 ((5*7+2)*3) to the stack
34*-#<_#      compares 12 to the result, if they are the same, continue to move 
                right, else move left
34*`!#<_#     checks to see that the input is greater (`) than 12. If it is, 
                continue to move right, else move left
98*34**`#<_   check that 864 (9*8*3*4) is greater than the input. (the reason for 
                this is that 864 was an easy number to create that was greater than 
                the last possible number (789) and was less than the next number 
                divisible by 111 (900)
1.@           if you pass all the checks, output 1, otherwise...
@.0># &#      you will move back through the code heading left. (you will do 
                the operations, but that will not matter.) Skip the input 
                command. Output zero and end the code.
\$\endgroup\$
0
\$\begingroup\$

Thue, 84 bytes

123z::=~1
234z::=~1
345z::=~1
456z::=~1
567z::=~1
678z::=~1
789z::=~1
a::=:::
::=
az

Outputs 1 when given one of the target values, and no output when given something else. The z at the end is needed to prevent false positives with numbers like 1230

\$\endgroup\$
0
\$\begingroup\$

Thunno, \$ 8 \log_{256}(96)\approx \$ 6.58 bytes

dz[11dA=

Attempt This Online!

Port of Emigna's 05AB1E answer.

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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