Saturday, 7 September 2013

Find the number using binary search?

Find the number using binary search?

How can I find unknown number between specific numbers?
Min number=8
Max Number=50
increment number=5
x=? needs to be find.
how many times it increments= 5 untill goes to the max number. and cannot
pass the max number
Conditions;
1. Start with number has to be either 2 or 50 or between 2-50...
2. For example, If the x=8 than numbers goes like First
cycle=(8,13,18,23,28,33) Second cycle=(38,43,***48***,53,58)... stops on
48. It can not exceed the Max number value.
I want to find the x with given values...
I try to use binary search but, it doesn't help me. because you need to
know the actual value to say lower or higher to find the number.
Binary search code for reference:
function findIndex(values, target) {
return binarySearch(values, target, 0, values.length - 1);
};
function binarySearch(values, target, start, end) {
if (start > end) { return -1; } //does not exist
var middle = Math.floor((start + end) / 2);
var value = values[middle];
if (value > target) { return binarySearch(values, target, start,
middle-1); }
if (value < target) { return binarySearch(values, target, middle+1, end); }
return middle; //found!
}
findIndex([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 20);

No comments:

Post a Comment