Algorithm - find closest two factors of an integer (minimal remainder) -
probably known issue, however, due lack of knowledge , bad english, wasn't able query such question properly.
goal:given positive integer n, find non-negative integers a,b,c such n = a*b + c c minimized. respecting following:
a <= b (b / a) < 2 c <= (a / 2) examples: n = 24 -> = 4, b = 6, c = 0 n = 25 -> = 5, b = 5, c = 0 n = 26 -> = 5, b = 5, c = 1 n = 27 -> = 5, b = 5, c = 2 n = 28 -> = 4, b = 7, c = 0 n = 29 -> = 4, b = 7, c = 1 n = 30 -> = 5, b = 6, c = 0 n = 31 -> = 5, b = 6, c = 1 n = 32 -> = 5, b = 6, c = 2
you don't language pseudocode.
function findfactors(num) { bestfactors = (0,0) bestremainder = num (x=num; x>0; x--) { (y=num; y>0; y--) { if (x*y <= num) { if (num % (x*y) < bestremainder) { bestfactors =(x,y) } } } } homecoming bestfactors } admittedly bigo of n-squared method little unruly. there's improve way recursion don't intend expend kind of brainpower right now.
algorithm
No comments:
Post a Comment