Note that there are some explanatory texts on larger screens.

plurals
  1. POBinarySearch limits for numbers within range
    text
    copied!<p>This is my code:</p> <pre><code>SortedDictionary&lt;int,int&gt; Numbers = new SortedDictionary&lt;int,int&gt;(); List&lt;int&gt; onlyP = new List&lt;int&gt;(Numbers.Keys); int Inferior = int.Parse(toks[0]); int Superior = int.Parse(toks[1]); int count = 0; int inferiorindex = Array.BinarySearch(Numbers.Keys.ToArray(), Inferior); if (inferiorindex &lt; 0) inferiorindex = (inferiorindex * -1) - 1; int superiorindex = Array.BinarySearch(Numbers.Keys.ToArray(), Superior); if (superiorindex &lt; 0) superiorindex = (superiorindex * -1) - 1; count = Numbers[onlyP[superiorindex]] - Numbers[onlyP[inferiorindex]]; </code></pre> <p>So what I'm trying to do is this: I've got a sorted dictionary with powers as keys, and a normal iteration as values. I've to print how many numbers of the keys fit within a specified range.</p> <p>Example: Some entries of the dict: [1,1],[4,2],[8,3],[9,4],[16,5],[25,6],[27,7],[32,8] Limits: 2 and 10 Numbers within 2 - 10 : 4, 8, 9 = 3 numbers.</p> <p>With BinarySearch I'm trying to quickly find the numbers I want and then substract Potencias[onlyP[superiorindex]] - Potencias[onlyP[inferiorindex]] to find how many numbers are within the range. Unfortunately it's not working for all the cases, and it sometimes gives less numbers than the actual amount. How can this be fixed? Thanks in advance.</p> <p>[EDIT] Examples of the problems: If I select limits: 4 and 4... it returns 0, but the answer is 1. limits: 1 and 10^9 (the whole range) returns 32669... But the answer is 32670. The algorithm is ignoring powers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload