-2
\$\begingroup\$

The task is to find the shortest solution to the following problem:

Given an integer \$n\$ (\$0<n<10^7\$) find \$n+C(n)\$, where \$C(n)\$ is the count of set bits in the binary representation of \$n\$.

Whitespace must not be included in the final count.

My current solution (78 bytes, C++14):

#include"iostream"
int n;main({std::cin>>n;std::cout<<n+__builtin_popcount(n);}

To clarify: some solutions from here will not work, as they subtract from the original \$n\$, so some methods cannot be reused.

\$\endgroup\$
5
  • \$\begingroup\$ Are you asking for tips to shorten your code? \$\endgroup\$ Commented Oct 8, 2024 at 16:37
  • \$\begingroup\$ not really. I have already shortened it enough, just seeing how short it could possibly be \$\endgroup\$ Commented Oct 8, 2024 at 16:38
  • \$\begingroup\$ Does this answer your question? I believe using that would be more non-whitespace bytes (\$81 - 2 = 79\$ : Try it online) than yours given your explicit requirements (unless I'm missing something like cheaper I/O). \$\endgroup\$ Commented Oct 8, 2024 at 17:36
  • \$\begingroup\$ Since you said you didn't want help, I edited the tags to be more usual for the kind of challenge you posted. That said, this might be a duplicate of what Jonathan Allan posted. In the future please consider using the sandbox before posting a challenge. Good luck! \$\endgroup\$ Commented Oct 8, 2024 at 20:20
  • 1
    \$\begingroup\$ I don't think that adding n to its number of set bits is a completely random addition. Without a popcount built-in, there's at least one way of doing this which turns out shorter than just adding n to the result of the other challenge. \$\endgroup\$ Commented Oct 9, 2024 at 7:36

5 Answers 5

2
\$\begingroup\$

Uiua, 5 bytes

+/+⊸⋯

Try it!

+ add the /+ sum of the ⋯ bits ⊸ by the input.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Alternate 5: /+⊂⟜⋯ \$\endgroup\$ Commented Oct 8, 2024 at 20:01
2
\$\begingroup\$

JavaScript (ES6), 23 bytes

f=n=>n&&f(n^(n&=-n))-~n

Try it online!

Explanation

Recursion seems like the most code-golf-friendly way of doing this in JS. However, counting the set bits and adding n to the total is not the shortest method because we need to either use n before the recursion starts or remember its original value for the final step.

For example:

n=>n+(g=k=>k&&-~g(k&k-1))(n) // 28 bytes
f=(n,k=n)=>n?-~f(n&n-1,k):k  // 27 bytes

Hence the idea of doing both tasks at the same time in the recursion each time a set bit is encountered, as described below.

Commented

f = n =>    // f is a recursive function taking n
n &&        // stop if n = 0
f(          // otherwise, do a recursive call with:
  n ^       //   n without
  (n &= -n) //   its least significant set bit
            //   which is saved in n
            //   (e.g. 6 AND -6 --> 2, 6 XOR 2 --> 4)
)           // end of recursive call
- ~n        // add 1 + the value of the extracted bit
\$\endgroup\$
1
\$\begingroup\$

Vyxal, 3 bytes

b∑+

Try it Online!

  + # Add
 ∑  # Sum
b   # Of bits
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 4 bytes

Three different minor alternatives:

Explanation:

b     # Convert the (implicit) input-integer to a binary-string
 S    # Convert it to a list of bits
  O   # Sum those bits
   +  # Add it to the (implicit) input-integer
      # (after which the result is output implicitly)

b     # Convert the (implicit) input-integer to a binary-string
 1ö   # Convert it to base-1, which sums its digits
   +  # Add it to the (implicit) input-integer
      # (after which the result is output implicitly)

b     # Convert the (implicit) input-integer to a binary-string
 Iª   # Convert the binary-string to a list of bits, and append the input-integer
   O  # Sum this list of bits with appended integer together
      # (after which the result is output implicitly)
\$\endgroup\$
0
\$\begingroup\$

Python 3, 28 bytes

lambda n:bin(n).count('1')+n

Try it online!

Python 3.10, 24 bytes

lambda n:n.bit_count()+n

As suggested by @squareroot12621

\$\endgroup\$
1
  • 1
    \$\begingroup\$ lambda n:n.bit_count()+n (24 bytes) works for Python 3.10+. \$\endgroup\$ Commented Oct 8, 2024 at 21:21

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.