Find a number by the decimal part of its square root [closed] - algorithm

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.

Related

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.

Which number appeared once? [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 6 years ago.
Improve this question
Given a list of 2n-1 numbers: all between 1 to n, all but one occur twice. Determine the number that occurs only once. Multiple ways preferred.
I think the problem is at fault, how can you determine which number without knowing the list of numbers?
[O(1) space, O(n) time]: Just take the XOR of all the numbers. Since all the numbers occur two times except one, XOR of those numbers will be zero and the single occurring number will be the result.
[O(1) space, O(n) time]: As said by user3386109 in comments, we can sum all the given numbers and compare that to the sum of numbers in the range [1, n] which will be n*(n+1) (since all numbers are supposed to occur twice). The difference of the two numbers is the answer.
[O(n) space, O(n) time]: Create an array of size n and keep the count of all the elements in the array at their corresponding positions. At the end, traverse the array, and find the number whose count is only 1.

Powers of a half that sum to one [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 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!

Pseudocode - Largest to smallest integer [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
Write a program that will read in five positive integers (one at a time) and print out the largest and smallest number of the five numbers. The program should read the numbers one at a time.
Mind you this is pseudocode and not to be done in any language.
My question is how would I go about setting this up so that the 5 integers save as values so I can display them.
Don't want the answer, just a start.
How would you solve the same problem if you had to only report the largest number? The pseudocode would be something like the following
consider the first number to be largest
for each of the rest of the number
if it is larger then the current largest
assign to largest
How would you do it if there were two?
consider the first number to be largest
if second number is larger then the largest
consider the second number to be largest, first to be 2nd largest
else
consider the first number to be largest, second to be 2nd largest
for each of the rest of the numbers
if it is larger then the largest
consider current largest to be 2nd largest and this number to be largest
else if it is larger then the 2nd largest
consider it to be 2nd largest
But if there are three or more this can get ugly. How do we keep N largest number? Clearly, we need a list of N sorted number. I will leave it to you how to maintain that list, but here's a pseudocode using that approach
populate the top-list with first N numbers from input, ensure the top-list is sorted
for each of the rest of the numbers
if the number is larger then any number in the top-list
insert it at the right place in top list, pushing out the smallest element of the top list
The question now is: is this better than sorting the list and picking up the top N and bottom N elements?
The answer is that "it depends". Can you figure out some circumstances where one approach is better then the other?
As you read the numbers, keep track of the currently largest and smallest numbers, and update the values as the input is coming in. This has the advantage that it works for even long sequences of numbers. I mean something like this:
min = 0
max = 0
while input:
read number from input
if number < min:
min = number
if number > max:
max = number

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!

Resources