3
$\begingroup$

Find $a$,$b$ if $\frac{1}{a}+\frac{1}{b}=\frac{1}{2008}$, where $a$ and $b$ are positive integers.


When I set the Range to 10 000, Mathematica never seems to stop.

When I set the Range to 1000 000, there was insufficient memory available to complete the computation.

list1 = Range[10000];
f[{a_, b_}] := 1/a + 1/b;
Pick[Tuples[list1, 2], f[#] & /@ Tuples[list1, 2], 1/2008]

Is there anyone who can solve this fuss? I have 16GB of RAM. I thought that is enough to find the answer below 10000

$\endgroup$
11
  • $\begingroup$ Have you tried evaluating Reduce[1/a + 1/b == 2008 && a > 0 && b > 0, {a, b}, Integers]? $\endgroup$ Commented May 27, 2020 at 10:37
  • $\begingroup$ @J.M., I just try your method , but only get False. $\endgroup$
    – kile
    Commented May 27, 2020 at 10:42
  • 5
    $\begingroup$ Are you sure your problem isn't $1/a + 1/b = 1/2008$? I've seen this sort of a problem being mentioned in that form somewhere recently... (and you can find all solutions to that problem with Solve[1/a + 1/b == 1/2008 && 0 < a <= b, {a, b}, Integers]. $\endgroup$
    – kirma
    Commented May 27, 2020 at 10:45
  • $\begingroup$ @kirma which forum are you talking about? Can you provide me a link or something else? $\endgroup$
    – kile
    Commented May 27, 2020 at 10:47
  • 1
    $\begingroup$ @kile It was probably this: math.stackexchange.com/questions/3123923/… - nonetheless, largest number you can form with positive integers on $1/a + 1/b$ is $2$ when assigning $1$ to both $a$ and $b$... $\endgroup$
    – kirma
    Commented May 27, 2020 at 10:51

2 Answers 2

3
$\begingroup$

This works but is a terrible way of solving the problem:

list1 = Range[10000];
f[{a_, b_}] = 1/a + 1/b;
Select[Tuples[list1, 2], f[#] == 1/2008 &]

(*    {{3012, 6024}, {4016, 4016}, {6024, 3012}}    *)
$\endgroup$
9
  • $\begingroup$ How long does it take for your computer to find out the results? My computer runs for 5 mins and still can't return a result even if I used your code. $\endgroup$
    – kile
    Commented May 27, 2020 at 11:36
  • 1
    $\begingroup$ @kile Tuples creates a big list, which can easily cause your machine to run out of RAM, which makes this drastically slower. 16 GB of RAM is not enough to do this comfortably. There's also experimental function called SubsetCases in v12.1 but it doesn't really scale well to such large sets. $\endgroup$
    – kirma
    Commented May 27, 2020 at 13:42
  • $\begingroup$ @Roman How much RAM does it need? My PC just locks on it. Is there a way to solve it, in smaller pieces? I suppose it is an all (10000) or nothing question? $\endgroup$
    – prog9910
    Commented May 27, 2020 at 14:22
  • 1
    $\begingroup$ Tuples[list1, 2] // ByteCount gives 1600000208 which is 1.6 GB, shouldn't be a problem. $\endgroup$
    – Roman
    Commented May 27, 2020 at 14:41
  • 1
    $\begingroup$ As I said this is a terrible way of solving your problem. Use Solve or iterate over the lists without storing them or ... $\endgroup$
    – Roman
    Commented May 27, 2020 at 14:42
3
$\begingroup$

This way seems to be pretty quick:

Solve[1/a + 1/b == 1/2008, b]
(*{{b -> (2008 a)/(a - 2008)}}*)

Reduce[b == (2008 a)/(a - 2008) && 0 < a < b && a ∈ Integers && b ∈ Integers]

(*(a == 2009 && b == 4034072) || (a == 2010 && 
   b == 2018040) || (a == 2012 && b == 1010024) || (a == 2016 && 
   b == 506016) || (a == 2024 && b == 254012) || (a == 2040 && 
   b == 128010) || (a == 2072 && b == 65009) || (a == 2259 && 
   b == 18072) || (a == 2510 && b == 10040) || (a == 3012 && 
   b == 6024)*)
$\endgroup$
1
  • 1
    $\begingroup$ Also FindInstance[a*b == 2008 (a + b), {a, b}, Integers, 6] if you just want 6 solutions for example. $\endgroup$
    – flinty
    Commented May 27, 2020 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.