Determine if sum is a power of 2 [duplicate] - algorithm

This question already has answers here:
How to check if a number is a power of 2
(32 answers)
Closed 8 years ago.
What is the fastest (in asymptotic worst-case time complexity) algorithm for determining if a sum of arbitrary positive integers is a power of two?

One cute bit twiddling trick is to test if x&(x-1) is equal to 0.
Note that you need to decide what to do if x is equal to 0, this test will mark 0 as a power of 2 so you may want an exception for this case.

Subtract 1 from the sum and perform a bitwise AND with the original number. Powers of 2 will have a result of 0.

Related

Why converting from base 10 to base 2 is considered slow? [duplicate]

This question already has an answer here:
Computational complexity of base conversion
(1 answer)
Closed 7 years ago.
As stated here it is quadratic, but why?
I think the quadratic part is reading the integer from text. The standard algorithm looks like this:
v = 0
for each digit:
v = v * 10 + digit
It looks like this is just O(n) on the number of digits, but if you are working with arbitrary precision integers like this problem then the multiplication by 10 is also O(n), making the whole thing O(n^2).

How to check if number is summation of powers of 5 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How to check if a number is a power of 5?
I could think of below algorithm. Is there way to improve it? Any mathematical trick?
First check if last digit is 5.
If last digit is 5; divide it by 5.
If result of division is 1, then number is power of 5.
Else check if division result itself is power of 5 (i.e. go to step 1 with result as number).
You don't need to look at individual digits, you can just do it like this:
n = (int)(log(x) / log(5)); // get n = log5(x), truncated to integer
if (pow(5, n) == x) // test to see whether x == 5^n
// x is a power of 5
LIVE DEMO
There are only few power of five that fit in int/long range, so you just need to generate all of them and check one by one (less than 60 numbers), using a HashSet will have O(1) time complexity
Successive division until you reach the number undivided by 5 and check whether the result is equal to 1, isn't bad solution. It take log_5(n) operations, so it's O(lg n), it's very good time. For 9094947017729282379150390625 it's only 40 operations!
What I would do is first create an array of numbers that are powers of 5. You could use a range, and then say, for each value in the range, take that value to the fifth power, and push the new value into the array.
You would then find if n, the number you are looking for is included in the array.

Generate random number from 0 to n using a boolean random number generator [duplicate]

This question already has answers here:
Creating a random number generator from a coin toss
(4 answers)
Closed 8 years ago.
Given a boolean random number generator. How can I use it to generate random number in the range 0 to n.
Set the bits of the number one after the other. Say you use the generator 10 times. That'll give you 10 chances to have either '0' or '1' in each round. You'll now generate a random number between 0 to 1023 inclusive.
To get a random number from 0 to n you'll need to use the generator lg(n) times (lg = log base 2).

How does one find the median of 5 distinct values with 6 comparisons? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Code to calculate “median of five” in C#
Suppose I have five numbers in a list, say [3,2,1,4,5].
How do I find the median with 6 comparisons?
Take an optimal sorting network for N=5 which requires 9 comparisons and then prune the unneeded comparisons for outputs 0, 1, 3 and 4. There's an example of this which uses 6 comparisons in the answer to this question.

binary divisibility by 3 [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Check if a number is divisible by 3
Is it true that a binary number is divisible by 3 iff it has an even number of ones?
like 11000 is divisible by 3 whereas 1110 is not.
No - there is a trick but it's a little more complicated than that - you have to count the number of 1s at even positions and the number of 1s at odd positions. See e.g. Check if a number is divisible by 3.
No, that's wrong. For example 5_dec = 101_bin is not divisble by 3. To check for divisbility by three, you have to count the number of ones in even position and substract the number of ones in odd positions. If the difference is divisble by three, the original number is divisbilble by three (which, in turn, can be checked by reiterating the same rule).

Resources