23
\$\begingroup\$

Let's start by defining a Two Bit Number™️:

  • It is a positive integer
  • When expressed as a binary string it has exactly 2 true bits OR
  • When expressed as a decimal number, it has exactly 2 of the numeral one, and all other numerals are zero.

Or as a sentence

A Two Bit Number™️ is a number which contains exactly 2 of the numeral 1 and no other numerals besides 0, when expressed as a decimal string or a binary number.

So here area all the Two Bit Numbers™️ between 0 and 256

Dec  Bin       Type
3    00000011  Binary
5    00000101  Binary
6    00000110  Binary
9    00001001  Binary
10   00001010  Binary
11   00001011  Decimal
12   00001100  Binary
17   00010001  Binary
18   00010010  Binary
20   00010100  Binary
24   00011000  Binary
33   00100001  Binary
34   00100010  Binary
36   00100100  Binary
40   00101000  Binary
48   00110000  Binary
65   01000001  Binary
66   01000010  Binary
68   01000100  Binary
72   01001000  Binary
80   01010000  Binary
96   01100000  Binary
101  01100101  Decimal
110  01101110  Decimal
129  10000001  Binary
130  10000010  Binary
132  10000100  Binary
136  10001000  Binary
144  10010000  Binary
160  10100000  Binary
192  11000000  Binary

The challenge:

  • Write some code which accepts a number and outputs true or false (or some indicator of true or false) if it is a Two Bit Number™️.
  • Input will always be an integer, but it may not always be positive.
  • It can be in any language you like.
  • It's code golf, so the fewest bytes wins.
  • Please include links to an online interpreter for your code (such as tio.run).

Test Cases

Binary Two Bit Numbers™️:

  • 3
  • 9
  • 18
  • 192
  • 288
  • 520
  • 524304

Decimal Two Bit Numbers™️:

  • 11
  • 101
  • 1001
  • 1010
  • 1100
  • 1000001
  • 1100000000
  • 1000000010

Non Two Bit Numbers™️:

  • 0
  • 1
  • 112 (any numerals over 1 prevent it being a binary
  • 200
  • 649
  • -1
  • -3

Fun fact: I was not able to find any DecimalBinary Two Bit Numbers™️ checking up to about 14 billion, and I have a hypothesis that such a number does not exist, but I have no mathematical proof. I'd be interested to hear if you can think of one.

\$\endgroup\$
19
  • 5
    \$\begingroup\$ I've sketched out a somewhat clumsy proof that no DB2B number exists. Not sure if it makes sense though \$\endgroup\$
    – Jo King
    Sep 30, 2020 at 14:07
  • 5
    \$\begingroup\$ @JoKing How do you get from 10^x+10^y = 2^x+2^b to 5^x = 2^b/10^y? It looks like you may have incorrectly treated the former expression as though it said * instead of +, and then divided both sides by 2^x*10^y. \$\endgroup\$
    – Lynn
    Sep 30, 2020 at 14:43
  • 4
    \$\begingroup\$ Input will always be an integer, but it may not always be positive. How are negative numbers encoded? With a minus in front? With a 1 in front? Using two's complement? \$\endgroup\$
    – Stef
    Sep 30, 2020 at 15:35
  • 5
    \$\begingroup\$ The existence of DecimalBinary Two Bit Numbers™️ is a fascinating nerd-snipe problem! \$\endgroup\$
    – Sisyphus
    Oct 1, 2020 at 0:38
  • 5
    \$\begingroup\$ You have -1 as not a Two Bit number (it would be in 2-s complement), you have -3 as not a Two Bit number (it would be with a minus sign character prepended). You do not have -11 as an example (again would be with a minus sign). I can only imagine that your rule is to convert to a base representation using digits in \$[-b+1..0]\$ e.g. -123 base 10 is [-1,-2,-3] \$= -1 \times 10^2 + -2 \times 10^1 + -1 \times 10^0\$. Without a spec it seems we can say all negative numbers are not Two Bit Numbers, so why, are there example ones and why do we even need to handle them? \$\endgroup\$ Oct 1, 2020 at 2:39

40 Answers 40

1
2
2
\$\begingroup\$

Japt, 17 14 12 bytes

[¢Us]d_e0 ¥B

Try it

[¢Us]         - input to string base 2 , 10
     d_       - if any :
          ¥B  - are == 11 after 
       e0     - removing 0
  • Saved 3 stealing from @ovs Python answer, upvote him!
\$\endgroup\$
2
\$\begingroup\$

Pyth, 22 16 bytes

-6 bytes thanks to @FryAmTheEggman

}11,v-Q\0v-.BQ\0

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can save some bytes by using - to remove zeros (it will automatically convert to string) and using } to test on a list rather than doing two equality comparisons. \$\endgroup\$ Oct 1, 2020 at 16:16
2
\$\begingroup\$

Python, 79 Bytes

lambda n:any(sum((ord(c)-48)**4 for c in f.format(n))==2for f in["{}","{0:b}"])

Explanation

I format the int as both binary and decimal. For each character, I subtract the '0' character, then raise it to the power of 4. This maps '0' to 0, '1' to 1, '2' to 16, and other digits and the '-' character to numbers greater than 16. Then, I check if the summation equals 2.

\$\endgroup\$
2
\$\begingroup\$

Octave, 67 64 bytes

@(x,p=@(b,z=dec2base(x*(x>0),b)-48)all(z<2)&sum(z)==2)p(2)|p(10)

Try it online!

If in doubt, make the code more convoluted. Somehow that tends to save bytes...

@(x,                               % Main anonymous function with 'x' as input
    p=                             % Second input 'p' with default value (no second input is given when calling function) which
      @(b,                         % consists of another anonymous function which takes base as input
          z=                       % From which it creates a second input 'z' with default value
            dec2base(       ,b)    % Which runs dec2base (convert from integer to string) using provided base
                     x             % On the input to the main anonymous function
                      *(x>0)       % Multiplied by (x>0) to return false for any negative integer passed in.
                               -48 % And converts from a string to an array of integers (one per digit)
      )
      all(z<2)&                    % Two-bit numbers must only contain 0 or 1, so need all elements in array of digits <2.
               sum(z)==2           % Sum all digits. Two-digit number if sum is 2 (two 1's)
)
p(2))||                            % Run two-bit number check in base 2
       p(10)                       % Run two-bit number check in base 10
       

First attempt,

@(x,p=@(z)all(z<50)&&sum(z-48)==2)x>0&&p(dec2bin(x))||p(num2str(x))

Try it online!

@(x,                               % Main anonymous function with 'x' as input
    p=                             % Second input 'p' with default value (no second input is given when calling function)
      @(z)                         % Default value consists of another anonymous function to check if string is two-bit
          all(z<50)&&              % Two-bit numbers must only contain '0' or '1', so need all elements in string <'2'(50).
                     sum(z-48)==2  % Convert all characters from '0'/'1' to 0/1 and sum. Two-digit if sum is 2 (two 1's)
)
x>0&&                              % Short-circuit to return false for any negative integer passed in.
     p(dec2bin(x))||               % Convert to binary string and check if two-digit, or...
                    p(num2str(x))  % Convert to decimal string and check if two-digit
\$\endgroup\$
2
\$\begingroup\$

T-SQL, 93 bytes

Returns 1 for true, 0 for false

DECLARE @y INT=@,@x INT=9WHILE @>0SELECT
@x+=@%2,@/=2PRINT
IIF(11in(@x,replace(@y,0,'')),1,0)

Try it online

\$\endgroup\$
2
\$\begingroup\$

PHP, 157 bytes

echo$argn>0&&(2==array_reduce(str_split($argn),fn($c,$i)=>$c+=$i?($i==1?1:9):0)||2==array_reduce(str_split(decbin($argn)),fn($c,$i)=>$c+=$i?($i==1?1:9):0));

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ @Kaddath beats me by almost 100 bytes :-) \$\endgroup\$ Oct 18, 2020 at 13:19
2
\$\begingroup\$

Groovy, 35 34 bytes

f={1.bitCount(it)==2|it==~'10*'*2}

Try it online!

Note that this treats negative numbers as 2-bit if its binary representation has exactly two 1 bits, e.g. Integer.MIN_VALUE+1 == 10000000000000000000000000000001 would be 2-bit.

\$\endgroup\$
1
  • \$\begingroup\$ Reduced by one byte by replacing '10*10*' with '10*'*2 \$\endgroup\$
    – M. Justin
    Apr 3, 2021 at 22:12
2
\$\begingroup\$

Perl 5, 45 bytes

sub($n){$_=sprintf"=$n=%b=",$n;s/0//g;/=11=/}

Try it online!

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

Arn, 12 9 12 bytes

⁼←±Î¡Oû¨¨K²$

Try it!

Old answer:

©ou…ö6úË»˜½$

Explained

Unpacked: [+\;b--:!1#]&2

  [         Begin sequence
            ENTRY ONE
    +\        Fold with sum
        _     Variable initialized to STDIN; implied
      ;b      In binary
            ENTRY TWO
    --        Subtract one from
          _   Implied
        :!    Split on every
          1     
      #       Length
  ]         End sequence
&           Contains
  2         Literal two
\$\endgroup\$
2
  • \$\begingroup\$ Doesn't this fail on -1 and -3 now? \$\endgroup\$
    – Jo King
    Oct 8, 2020 at 22:36
  • \$\begingroup\$ ah... yes, you are correct. I'll fix this later \$\endgroup\$ Oct 9, 2020 at 2:02
1
\$\begingroup\$

Excel, 169 bytes

=LET(n,LEN(A1),e,SUBSTITUTE(A1,1,""),d,(n-LEN(e)=2)*(LEN(SUBSTITUTE(e,0,""))=0),b,CONCAT(DEC2BIN(MOD(INT(A1/512^{0,1,2}),512))),t,(LEN(b)-LEN(SUBSTITUTE(b,1,"")))=2,d+t)

Excel (future release), 166 bytes

=LET(s,LAMBDA(x,y,SUBSTITUTE(x,y,"")),n,LEN(A1),e,s(A1,1),d,(n-LEN(e)=2)*(LEN(s(e,0))=0),b,CONCAT(DEC2BIN(MOD(INT(A1/512^{0,1,2}),512))),t,(LEN(b)-LEN(s(b,1)))=2,d+t)

This uses LAMBDA which isn't widely available yet. Not much savings.

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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