19
\$\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\$
  • 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 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 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 at 15:35
  • 4
    \$\begingroup\$ The existence of DecimalBinary Two Bit Numbers™️ is a fascinating nerd-snipe problem! \$\endgroup\$ – Sisyphus Oct 1 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\$ – Jonathan Allan Oct 1 at 2:39

37 Answers 37

1
2
2
\$\begingroup\$

Husk, 11 10 bytes

#2§¤eṁ^3ḋd

Try it online!

Explanation

  §            Apply to input the two functions
         d       Convert to list of digits
        ḋ        And convert to list of binary digits
   ¤e          Then apply to both values
     ṁ^3         Cube each and sum
#2             How many 2s are there in the list?
| improve this answer | |
\$\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!

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ @Kaddath beats me by almost 100 bytes :-) \$\endgroup\$ – Zsolt Szilagy Oct 18 at 13:19
2
\$\begingroup\$

Scala, 63 bytes

n=>Seq(n.toString,n.toBinaryString).exists(_.matches("10*10*"))

Try it online!

Thanks to @Tomer Shetah for pointing out that lamdbas do not require their type definition (see comments), which saves 10 bytes :)

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ You can do it in -10 bytes. Try it online! \$\endgroup\$ – Tomer Shetah Oct 20 at 9:08
  • \$\begingroup\$ I am unsure about whether it is accepted for a Scala solution to provide an anonymous expression that cannot be called without binding it to some function name and signature to make it actually callable. You exclude the val f: Int => Boolean = from your byte-counts, although it is required to call the expression. I include the binding via the (shorter) def a(n:Int)= alternative. So my question in a nutshell: is for example _.length a valid solution, or only one of val a: String => Int = _.length or def a(s: String)=s.length ? Perhaps this is a topic for a meta discussion. \$\endgroup\$ – cubic lettuce Oct 21 at 15:07
  • 1
    \$\begingroup\$ It has already been discussed. Please read this post \$\endgroup\$ – Tomer Shetah Oct 22 at 5:16
1
\$\begingroup\$

Java 8, 67 bytes

n->(" "+n+" "+n.toString(n,2)+" ").replace("0","").contains(" 11 ")

Try it online.

Explanation:

n->                       // Method with Integer parameter and boolean return-type
  (" "                    //  Have a leading space
   +n                     //  Appended with the input
   +" "                   //  Appended with another space
   +n.toString(n,2)       //  Appended with the input as binary-String
   +" ")                  //  Appended with a trailing space
        .replace("0","")  //  Then remove all "0"s from this string
        .contains(" 11 ") //  Check if what remains contains 11 surrounded with spaces
| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Surely n.toString(2) suffices? \$\endgroup\$ – Neil Oct 3 at 0:27
  • \$\begingroup\$ @Neil No, I'm afraid not. The first n is used as a static call. Non-golfed, it would be Integer.toString(n,2), which is a shorter variant of Integer.toBinaryString(n). What you suggest is short for Integer.toString(2), which would result in "2", since the Integer.toString(int) method will use base-10 by default. \$\endgroup\$ – Kevin Cruijssen Oct 3 at 11:38
  • \$\begingroup\$ Ah, I thought there would be a non-static toString(radix) method for some reason. \$\endgroup\$ – Neil Oct 3 at 15:51
1
\$\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!
| improve this answer | |
\$\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
| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ This fails for 200 \$\endgroup\$ – Jo King Oct 7 at 1:32
  • \$\begingroup\$ Ah yeah I see, I'll fix that now \$\endgroup\$ – ZippyMagician Oct 8 at 15:28
  • \$\begingroup\$ It should now be fixed. Thanks for pointing that out \$\endgroup\$ – ZippyMagician Oct 8 at 15:36
  • \$\begingroup\$ Doesn't this fail on -1 and -3 now? \$\endgroup\$ – Jo King Oct 8 at 22:36
  • \$\begingroup\$ ah... yes, you are correct. I'll fix this later \$\endgroup\$ – ZippyMagician Oct 9 at 2:02
1
\$\begingroup\$

Groovy, 35 bytes

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

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.

| improve this answer | |
\$\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.