Powers of a half that sum to one [closed] - algorithm

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
Call every subunitary ratio with its denominator a power of 2 a perplex.
Number 1 can be written in many ways as a sum of perplexes.
Call every sum of perplexes a zeta.
Two zetas are distinct if and only if one of the zeta has as least one perplex that the other does not have. In the image shown above, the last two zetas are considered to be the same.
Find all the numbers of ways 1 can be written as a zeta with N perplexes. Because this number can be big, calculate it modulo 100003.
Please don't post the code, but rather the algorithm. Be as precise as you can.
This problem was given at a contest and the official solution, written in the Romanian language, has been uploaded at https://www.dropbox.com/s/ulvp9of5b3bfgm0/1112_descr_P2_fractii2.docx?dl=0 , as a docx file. (you can use google translate)
I do not understand what the author of the solution meant to say there.

Well, this reminds me of BFS algorithms(Breadth first search), where you radiate out from a single point to find multiple solutions w/ different permutations.
Here you can use recursion, and set the base case as when N perplexes have been reached in that 1 call stack of the recursive function.
So you can say:
function(int N <-- perplexes, ArrayList<Double> currentNumbers, double dividedNum)
if N == 0, then you're done - enter the currentNumbers array into a hashtable
clone the currentNumbers ArrayList as cloneNumbers
remove dividedNum from cloneNumbers and add 2 dividedNum/2
iterate through index of cloneNumbers
for every number x in cloneNumbers, call function(N--, cloneNumbers, x)
This is a rough, very inefficient but short way to do it. There's obviously a lot of ways you can prune the algorithm(reduce the amount of duplicates going into the hashtable, prevent cloning as much as possible, etc), but because this shows the absolute permutation of every number, and then enters that sequence into a hashtable, the hashtable will use its equals() comparison to see that the sequence already exists(such as your last 2 zetas), and reject the duplicate. That way, you'll be left with the answer you want.
The efficiency of the current algorithm: O(|E|^(N)), where |E| is the absolute number of numbers you can have inside of the array at the end of all insertions, and N is the number of insertions(or as you said, # of perplexes). Obviously this isn't the most optimal speed, but it does definitely work.
Hope this helps!

Related

is there any efficent alogrithm to check if i have atleast one valid combination to win from a group of valid combination? [closed]

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
In my project, I have a requirement where they give a group of numbers and I need to check if they form some combination that is accepted.
Note: A combination is considered accepted if it exists in the group of valid combinations.
For example: suppose i have 3 numbers 1,2,3 so combination formed with them will be i.e {1} ,{2},{3},{1,2},{2,3},{1,3} and {1,2,3} note: {1,2} and {2,1} is same in my case and same applied to the rest.
and accepted valid combinations are {1,3} and {2,1} so i have 2 valid combinations in my set of combination ,hence i win.
is there any algorithm to find that out efficiently?
Note:
In input, they give with some valid combinations that are accepted as in the above example they provide me {1,3} and {2,1} if my numbers form any combination out of these combinations then I can win. and same applied to the rest statement referred to the ordering of elements i.e if I` have (3,1) then it is also accepted as the ordering of elements does not matter and 1,3 is accepted hence 3,1 is also accepted.
Any answer will be highly appreciated.
If the accepted set is relatively small (not N!) then one solution would be to create a trie and look for matches in it.
To look for matches you would only descend a link if the label associated with it is within your set. If you reach an end state you can stop searching.
To get the most benefits out of it you should sort the items in each accepted combination (that way you group all combinations that have '1' in them somewhere). I would guess that the best way to sort the values would be by how often they occur in the accepted set, but I don't have hard proof of that.
To build the trie the complexity if O(M * d) for M combinations of average lengh d. To check the complexity is also O(M * d) but should perform a lot better or more random cases (since you can eliminate more than one combination as soon as you don't find the next node).
Since M is there if you have a large number of accepted combinations then this might be slower.

Closest point to another point on a hypersphere [closed]

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
I have n (about 10^5) points on a hypersphere of dimension m (between 10^4 to 10^6).
I am going to make a bunch of queries of the form "given a point p, find the closest of the n points to p". I'll make about n of these queries.
(Not sure if the hypersphere fact helps at all.)
The simple naive algorithm to solve this is, for each query, to compare p to all other n points. Doing this n times ends up with a runtime of O(n^2 m), which is far too big for me to be able to compute.
Is there a more efficient algorithm I can use? If I could get it to O(nm) with some log factors that'd be great.
Probably not. Having many dimensions makes efficient indexing extremely hard. That is why people look for opportunities to reduce the number of dimensions to something manageable.
See https://en.wikipedia.org/wiki/Curse_of_dimensionality and https://en.wikipedia.org/wiki/Dimensionality_reduction for more.
Divide your space up into hypercubes -- call these cells -- with edge size chosen so that on average you'll have one point per cube. You'll want a map from hypercells to the set of points they contain.
Then, given a point, check its hypercell for other points. If it is empty, look at the adjacent hypercells (I'd recommend a literal hypercube of hypercells for simplicity rather than some approximation to a hypersphere built out of hypercells). Check that for other points. Keep repeating until you get a point. Assuming your points are randomly distributed, odds are high that you'll find a second point within 1-2 expansions.
Once you find a point, check all hypercells that could possibly contain a closer point. This is possible because the point you find may be in a corner, but there's some closer point outside of the hypercube containing all the hypercells you've inspected so far.

Find a number by the decimal part of its square root [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
I have a math problem consisting of two questions:
can we find a number N knowing only the decimal part of its square root up to a precision (only an approximation of the decimal part because the decimal part never ends)
is the answer unique? which mean that we won't find two integer whose square root decimal values are equal (the first 50 for example) .
Example:
if we have 0,4142135623730950488016887242097, can we find that it's the decimal part of square root of 2
or 0,418286444621616658231167581 for 1234567890
The answer for the second question is pretty easy because, let's say we have 50 decimals, the number of possible integer's square root is much more than the 10^50-1 possible values of the decimals parts, so there whill be more than one answer.
I am very grateful for your help or any research track.
You answered the second question yourself already. No there is no unique solution.
For the first question i don't know a quick mathematical solution, but some non-performant programming solutions:
Option A: The brute force method:
iterate over all integers, and compare the square root of each with your number.
Option B: More tricky brute force method, which is more performant, but still slow:
Iterate the integers from 1 to M
Add your decimal part to each of them
Take the power of two and see how close the next integer value is
if the next integer value is very close, take the square root of it to counter check the result
stop as soon as you found the correct integer
Option C: caching:
precalculate your decimal parts for all integers and store them in a HashMap.
use the HashMap to find the results quickly
Consider: since you have a very big amount of data, different decimal parts could result in the same hash value, which would break this option.

Need algorithm for choosing sets of integer from list of such sets [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a list whose members are sets of 5 numbers chosen from the
integers 1 to 600 [or 0 to 599 for storage purposes].
I need to choose a sublist of this list such that among the sets in this
sublist, each integer in the 1 to 600 range appears exactly once, so a
sublist of 120 elements. My list has either 4200 or 840 elements in
it--I'll find out by running whether the bigger number is necessary.
I need any one such sublist.
This sounds like a standard problem to me, but I have no idea how to
search. Can someone help with providing an algorithm, please?
From Set Cover Problem
The greedy algorithm for set covering chooses sets according to one rule: at each stage, choose the set that contains the largest number of uncovered elements
Wikipedia seems to say that this algorithm works the best under plausible complexity assumptions.
I would boil it down to these steps:
Pick an element from the list (the first one, probably)
Pick the next element you come across where all 5 numbers are not yet represented in the sub-list
If you reach the end, go back to the beginning of the list and lower the criteria of step #2 to 4 numbers
Repeat steps 2 & 3 until you have covered all integers
Depending on the programming language you're using, there are ways of making this pretty quick.
Edit: the poster has explained that each integer must be used exactly once
So, what you really need to do is just continue adding elements until the element contains an integer that is already present in your subset. The "exactly" criterion takes precedent over the "not yet in the subset" criterion. You'll break out of the loop when you hit 120 subsets.
You may also want to keep track of the order in which you add elements to your subset, and when you hit a dead end (e.g., each of the elements remaining in the superset contains an integer that is already present in your subset) you backtrack one element and continue.
In order to backtrack and remember what combinations do not work, you will need to keep a list of "banned collections", and each time you decide whether to add a new element you should first make sure it's not in this list of banned collections. The best way (that I've found) to do this in Ruby is to store the Hash of the collection rather than the collection itself. This provides an inexpensive way to evaluate whether the prospective collection has already been tried and has led to a dead-end.
Good luck!

How many fewer questions can I ask to guess a number? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
If I ask a person to select a number between 1 and 1200 in his mind. If I can only ask questions for which he will only reply with YES or NO, how many questions will I need to ask before I arrive at the answer for the number He had selected in my mind ?
I am looking for the less possible number of questions. Any proven solution would be appreciable.
To determine which of the numbers between 1 and n are chosen, you will need to ask at least log2 n questions. There is no possible way to do better.
The intuition for this answer is as follows. Suppose that you ask a total of k questions. The maximum number of different possible answers you can receive to those questions, even if they're dependent on one another, is 2k. Since there are n possible numbers that could be picked, you need to choose k such that
2k ≥ n
Which happens precisely when
k ≥ log2 n
In other words, you have to ask at least log2 n questions to be able to even have enough different possible outcomes to associate each possible number with some possible outcome. Since the number of questions must always be a natural number, the minimum number of questions you can ask must be at least ⌈log2 n⌉
This is purely a lower bound on the answer. At this point, we can't rule out the possibility that maybe you need far more questions than this to get the answer. However, the fact that we know about the binary search algorithm means that we know that you never need more than ⌈log2 n⌉ questions to get the answer, since this is the number of questions you'd ask if you were doing a binary search. This means that the binary search algorithm has to optimal, since there is no possible way of asking a smaller number of questions.
Hope this helps!
The log base 2 of 1200, rounded up to an integer: that's 11. Basically, every question cuts the possible range in half, so you just continue with a binary search until the possible range has length 1.
Ask for all the bits in the number. 11 questions are enough for that.
Edit: I would argue, that it's impossible to do better, due to the bijectivity between the binary and decimal representation - at least for the worst case.
This also a classical example of adversary arguments method which is used to find lower bound complexity. In our case the person who knows the number is the adversary. So he will wisely change his real answer when you ask a new question. How does he determine in his answer in each step? Let, for example the number is between 1-100.
You ask: is n>=50?. He may say YES OR NO both will be equally well for him since intervals are equal. Let assume he says yes.
Then you say a number between 50<=N<=100, lets say you ask: is n>=80.Then he should say NO even if the number he picked is larger than 80 because that 50<=n<=80 is larger interval.Now the number may be between 50 and 80
Maintaining this way, he will guarentee the maximum number of questions, that is logn since the interval size is decreasing like in binary search

Resources