Skip to main content
GameDev.net gamedev.net
🔒 Locked

numbers problem

Started by CProgrammer Jun 26, 2007 at 3:13 PM 5 replies 1.1k views
Original Post
CProgrammer
CProgrammer
Im trying to find a way to do this: Given a number z; Find x, y so that: z = x*y and |x-y| is minimal. Any ideas what the best approach would be. Seems to me that this could be as impossible as finding prime factors computationally but I thought Id ask, maybe someone has an idea.
ToohrVyk
ToohrVyk
For reals, x = y = sqrt(z)
For integers, this is as hard as a primality test.
baldurk
baldurk
I'm assuming that you want integer solutions, since otherwise it's fairly trivial as x = y = sqrt(z).

At a guess, I'd say you could simply start at floor(sqrt(z)) and march down until you find a factor, which then gives you y as z/x. I don't know if that's an optimal solution. Probably not.
CProgrammer
CProgrammer
Hmm I figured this would be problematic. I may just have to find another approach, funny how presumably small things can limit you :)
Daerax
Daerax
I assume you are using the computer integer datatype so the set of all possible numbers you may choose has a maximal element. You need not abandon that method if you are only going to be choosing from a subset of said set. Say if size of this subset is sufficiently small, |N| < 10^12? With M, the maximal, increasing with relaxing of time constraints.

There are a number of options including use of factorisation algorithims or sorted table of lists of factors.
CProgrammer
CProgrammer
Interesting.
The sorted table seems like a viable option.

Thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.