3

I'm having a trouble with my math:

Assume that we have a function: F(x,y) = P; And my question is: what would be the most efficient way of counting up suitable (x,y) plots for this function ? It means that I don't need the coordinates themself, but I need a number of them. P is in a range: [0 ; 10^14]. "x" and "y" are integers. Is it solved using bruteforce or there are some advanced tricks(math / programming language(C,C++)) to solve this fast enough ?

To be more concrete, the function is: x*y - ((x+y)/2) + 1.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Dmitri
  • 2,451
  • 6
  • 35
  • 55

2 Answers2

10

x*y - ((x+y)/2) + 1 == P is equivalent to (2x-1)(2y-1) == (4P-3).

So, you're basically looking for the number of factorizations of 4P-3. How to factor a number in C or C++ is probably a different question, but each factorization yields a solution to the original equation. [Edit: in fact two solutions, since if A*B == C then of course (-A)*(-B) == C also].

As far as the programming languages C and C++ are concerned, just make sure you use a type that's big enough to contain 4 * 10^14. int won't do, so try long long.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
2

You have a two-parameter function and want to solve it for a given constant.

This is a pretty big field in mathematics, and there are probably dozens of algorithms of solving your equation. One key idea that many use is the fact that if you find a point where F<P and then a point F>P, then somewhere between these two points, F must equal P.

One of the most basic algorithms for finding a root (or zero, which you of course can convert to by taking F'=F-P) is Newton's method. I suggest you start with that and read your way up to more advanced algorithms. This is a farily large field of study, so happy reading!

Wikipedia has a list of root-finding algorithms that you can use as a starting place.

csl
  • 10,937
  • 5
  • 57
  • 89
  • 1
    Newton's method and most other root-finding algorithms aren't really focused on integer-valued functions, but of course you can still use many them (for instance, simply solve the floating-point equation, then round the solution to integer, try if it is still a solution for the integer equation, if not try the adjacent values and so on). – leftaroundabout Jun 03 '11 at 09:49
  • 1
    The "key idea" assumes a functions that's continuous. A classic counterexample is `y=1/x`. There's no point where y=0, even though y=-1 at x=-1 and y=+1 at x=+1. – MSalters Jun 03 '11 at 10:20
  • IMO this answer has almost nothing to do with the original question. The fact that `x, y` are integers matters very much. – Serge Dundich Jun 03 '11 at 12:10
  • @MSalters: Take any _constant_ C, then even for y=1/x you can find a value for y=C, but singularities are special, yes. – csl Jun 03 '11 at 15:27
  • @Serge: He asks how to plot them, a very general question, not how to solve Diophantine equations. – csl Jun 03 '11 at 15:27