23
\$\begingroup\$

Challenge :

Count the number of ones 1 in the binary representation of all number between a range.


Input :

Two non-decimal positive integers


Output :

The sum of all the 1s in the range between the two numbers.


Example :

4 , 7        ---> 8
4  = 100 (adds one)   = 1
5  = 101 (adds two)   = 3
6  = 110 (adds two)   = 5
7  = 111 (adds three) = 8

10 , 20     ---> 27
100 , 200   ---> 419
1 , 3       ---> 4
1 , 2       ---> 2
1000, 2000  ---> 5938

I have only explained the first example otherwise it would have taken up a huge amount of space if I tried to explain for all of them.


Note :

  • Numbers can be apart by over a 1000
  • All input will be valid.
  • The minimum output will be one.
  • You can accept number as an array of two elements.
  • You can choose how the numbers are ordered.

Winning criteria :

This is so shortest code in bytes for each language wins.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ OEIS A000788 \$\endgroup\$
    – Leaky Nun
    Commented Jun 5, 2018 at 15:50
  • 1
    \$\begingroup\$ May we take the input as some kind of range type (IntRange in Kotlin, Range in Ruby)? \$\endgroup\$
    – snail_
    Commented Jun 6, 2018 at 4:03
  • \$\begingroup\$ Fun fact: case 1000 - 2000 yields 5938, but lower the case by 1000, the result also drops by 1000: 0-1000 = 4938. Proof \$\endgroup\$
    – steenbergh
    Commented Nov 16, 2018 at 13:00
  • \$\begingroup\$ @steenbergh Consider 0-2000 + 1000. For 0<=i<1000, pair i with (2i,2i+1), then <0-2000,1000> is 2 times <0-1000> plus 1000 extra 1's. Removing a 0-1000 is, 1000-2000 is <0-1000> + 1000 \$\endgroup\$
    – l4m2
    Commented Mar 26 at 6:30

64 Answers 64

1 2
3
0
\$\begingroup\$

Prolog (SWI), 43 bytes

A*B*O:-A>B,O=0;- \A*B*S,O is S+popcount(A).

Try it online!

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

Aya, 15 bytes

[2|,10 2.&].FW

Apparently there's no to binary built-in, so it needs to be done manually. Takes inputs on the stack.

Example Session

Given that there's no try it online, I figured I'd provide an example console session, placing the program in a function for convenience. It would work outside the function and without the variable names, as taking input from the stack is an allowed default for stack languages.

aya> {a b, a b[2|,10 2.&].FW} :f
{a b, a b [2|, 10 2 .&] .F W} 
aya> 4 7 f
8 
aya> 10 20 f
27 
aya> 100 200 f
419 
aya> 1 3 f
4 
aya> 1 2 f
2 
aya> 1000 2000 f
5938 

Explained

[2|,10 2.&].FW­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‎⁡⁠⁣‏⁠‎⁡⁠⁤‏⁠‎⁡⁠⁣⁣‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁢⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁣⁤‏⁠‎⁡⁠⁤⁡‏‏​⁡⁠⁡‌⁢⁢​‎‎⁡⁠⁤⁢‏‏​⁡⁠⁡‌­
[2|,      ]     # ‎⁡Over each number in a range from the first input to the second
        .&      # ‎⁢  Convert the number
    10          # ‎⁣  From base 10
       2        # ‎⁤  To base 2
           .F   # ‎⁢⁡Flatten the resulting list
             W  # ‎⁢⁢And summate
💎

Created with the help of Luminespire.

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

MathGolf, 7 bytes

╒k(≥mâΣ

Takes two loose inputs in reversed order.

Try it online.

Explanation:

Unfortunately, MathGolf lacks a ranged-list builtin, so 4 bytes are used for that..

╒k(≥     # Push a list in the range [secondInput,firstInput]:
╒        #  Push a list in the range [1, (first) implicit input-integer]
 k       #  Push the second input-integer
  (      #  Decrease it by 1
   ≥     #  Remove that many leading items from the ranged-list
    m    # Map over each integer in this list:
     â   #  Convert it to a binary-list
      Σ  # Take the flattened sum of this list of lists of bits
         # (after which the entire stack is output implicitly as result)
\$\endgroup\$
0
\$\begingroup\$

Swift 5.9, 54 bytes

let f={($0+0...$1+0).reduce(0){$0+$1.nonzeroBitCount}}

f(_:_:) takes a lower bound and an upper bound.

There aren't really any fancy tricks here, just a simple call to reduce(_:_:) and some hints for the type checker in the form of +0s.

\$\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.