Let's say I have a number $n$ and I want to find the divisors of $n$, I can do that using Divisors[n]. That will generate a list {...,...,...} which are the divisors of $n$. Now the question: how can I test if the sum of two divisors of the number $n$ add up to a perfect square?

For example the number $n=6$ as the following divisors {1,2,3,6} and we can notice that $1+3=2^2$ and $3+6=3^2$. So I want Mathematica to spit out two things:

  1. I want Mathematica to give True is there are two numbers in the divisors that add up to a perfect square;
  2. And I want Mathematica to find the two numbers that add up to the perfect square.
  • $\begingroup$ What have you tried? I see zero code in the question... $\endgroup$
    – ciao
    Apr 22 at 7:21
  • $\begingroup$ @ciao I have no idea how to code that in Mathematica. $\endgroup$
    – Jan
    Apr 22 at 7:23
  • 2
    $\begingroup$ wolfram.com/language/fast-introduction-for-programmers/en This is not a "do my work for me" stack. Give it a try, then if you have problems, post the code in a question. $\endgroup$
    – ciao
    Apr 22 at 7:25
  • $\begingroup$ Maybe this helps: Fastest square number test. $\endgroup$
    – Roman
    Apr 22 at 20:46

First take a outer product to get all the pairs

prod = Flatten[Outer[List, d, d], 1]

Then e.g. define a function like this

    IsSquare[{a_, b_}] := Module[{check},
  check = IntegerQ[Sqrt[a + b]];
  If[check == True, Print[{a, b}]];

and apply it to the outer product

IsSquare /@ prod

which will print all the pairs that satisfy your condition and returns a list with True or False. This now you can adapt to your specific need.


Only slightly different to @Andrzej , thought I'd give it a go

n = 2;
d0 = Divisors[n]
d = DeleteDuplicates[Sort /@ Tuples[%, 2]]
Sqrt /@ ((#[[1]] + #[[2]]) & /@ %)
IntegerQ /@ %
d[[#]] & /@ Position[%, True]

Little more clunky but maybe Tuples[] will help


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