Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
how to find all prime numbers between 1 and 10^9 , i know we can use Sieve_of_Eratosthenes for smaller range, but what when range is too large equivalent to 10^6 ?
Up to 10^9 is not really a big deal. First, only look at odd numbers (because there is only one even prime). Second, use a bit array, so you only need 500 million bits or about 62 Megabyte. Even straightforward code should do that in a few seconds at most.
If you go further, you'd do a sieve for numbers from 1 to 10^9, then from 10^9 + 1 to 2 * 10^9 and so on. Above 10^13 it gets interesting and you need to put a bit more effort into it.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I also noticed that multiples of 0.0625 such as 1.25 keep giving zero but I was unable to find an explanation
as per CostantinoGrana
"Do you know how IEEE 754 works? The numbers which you say "give 0"
are multiples of a not too negative negative power of 2. So when you
store them in little endian, the 32 less significant bits, that are
all 0, are the first thing you find in memory as int. Your int is 32
bits wide, thus 0."
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Time Complexity of Juggling algorithm for array rotation(Suppose 'd' times) is computed as O(n), where n is the size of the array. But for any number of rotation(i.e. for any value of 'd'), the algorithm runs exactly for n times. So, shouldn't the time complexity of the algorithm be "Theta(n)" ? It always loops for n times in any case.If not, can anyone provide a test case where it doesn't run for n times?
It is unclear what you ask, but if we look at https://www.geeksforgeeks.org/array-rotation/ we see that it is described as O(n) time but if we want to rotate zero steps it could be done in O(1) time, so it doesn't always take n times - i.e. Theta(n) would be wrong; but O(n) is correct.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
What is the best way to count the number of 1's in an 32-bit integer x in C without using for or while loops, and without using constants greater than 0xFF?
What I thought of is shifting x 24 to the right and count how many 1's in the shifted integer and store that in a variable count. And then, shifting x 16 to the right and increment count by the number of 1's in the shifted integer, and so on.
So, any ideas of a better solution?
You can tabulate the number of 1's in all d bits numbers. This takes a table of 2^d entries, each not exceeding the value d (<255).
Now you can cut your number in slices of d bits and lookup the counts for all slices.
A good compromise between space/number of operations is probably with d=4 (8 slices, table size=16).
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Is there a generic way to represent a number containing n bits using n-1 bits; e.g. showing 1001 which has 4 bits using XXX where X = {0|1} with 3 bits. Also, the mapping should be able to retrieve the original binary back without any collision.
This page is the most relevant reference that I have found so far which tries to count the number of bits, but not reduce the number of bits.
Edit: I knew this sounds impossible, but I'm curious if there exists a workaround to do so!
There are 2^n possible values with n bits, and 2^(n-1) with (n-1) bits. So you can't convert from the former to the latter losslessly.
If it were at all possible, you could also recursively represent your n-1 bits using n-2 bits, etc. Everything would be representable with 0 bits :)
You get misleaded by the page you linked, which explains that x &= x-1 removes a 1 in a bit string.
10100
& 10011
= 10000
You will be very clever if you manage to establish a one-to-one mapping between the 16 numbers
0000,0001,0010,0011,0100,0101,0110,0111,1000,1001,1010,1011,1100,1101,1110,1111
and the eight numbers
000,001,010,011,100,101,110,111.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How would I do this in 1 loop since I need to loop again and close every second door? Do they want me to loop through the program 100 times? Should I be using pointers ?
Yes, you should loop through the program 100 times if you want to simulate this behavior.
But if you want to know the final condition(Open/Close) then you can have better algorithm:
As every perfect square number only have odd number of factor, if number is perfect square then final condition of door is open otherwise door is close.
If you are interested see perfect square number and Why perfect squares only have odd numbers of factor .