0

I have this code for evaluating some fractions:

fmax = 100;
Do[
 If[GCD[f, g, h] != 1, Continue[]];
 If[Mod[f^2 + g^2 + h^2, f g + g h + h f] == 0, Print[(f g+g h+h f)/(f^2+g^2+h^2)]],
 {f, fmax}, {g, f + 1, fmax + 1}, {h, g + 1, fmax + 2}
 ]

However, I want to stop it from evaluating the same fraction twice (there are a lot of $\frac{1}{2}$'s appearing, but I only need one of each possible fraction).

Is there an easy way to modify the code to do this?

If not, how can I just make sure that NO $\frac{1}{2}$'s appear. That would be just as helpful if you can show me how to do it.

Thanks!

user45220
  • 145
  • 4

2 Answers2

3
l = Flatten[ Table[{i, j, k}, {i, 100}, {j, i + 1, 101}, {k, j + 1, 102}], 2];
l1 = Select[l,GCD @@ # == 1 && Mod[Norm[#]^2, Tr[Times @@@ Subsets[#, {2}]]] == 0 &];
DeleteDuplicates[Tr[Times @@@ Subsets[#, {2}]]/Norm[#]^2 & /@ l1]

(* {1/2, 1/14, 1/10, 1/5} *)
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
3

Pretty simple, although maybe not obvious in this kind of code: Use Memoization. Here, you could just use your own version of print which redefines itself after it has printed a fraction and does nothing anymore:

print[frac_] := (Print[frac]; print[frac] = Null);

fmax = 100;
Do[If[GCD[f, g, h] != 1, Continue[]];
 If[Mod[f^2 + g^2 + h^2, f g + g h + h f] == 0, 
  print[(f g + g h + h f)/(f^2 + g^2 + h^2)]], {f, fmax}, {g, f + 1, 
  fmax + 1}, {h, g + 1, fmax + 2}]

Mathematica graphics

halirutan
  • 112,764
  • 7
  • 263
  • 474