0
|
3407 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
GROUPS:

# Can anyone see why this formats oddly?

Posted 10 years ago
 Hi everyoneCan anyone see why when a solution varible equals 1 the formating doesn't print, yet for all other numbers it seems fine.  Here is the program in its entirety. Do[a = Solve[(x^2 + y^2 + z^2)/3 == n^2 && y > 0 && x > 0 && z > 0 &&       x <= y && y <= z, {x, y, z}, Integers]; a = {x, y, z} /. a;   a = Flatten[a];   If[Length[a] > 3, a = Delete[a, {{-1}, {-2}, {-3}}];    a = Riffle[a, n, {4, -1, 4}]; a = Partition[a, 4];    If[Length[a] > 1,     Print["There are ", Length[a], " solutions when n = ", n]];    If[Length[a] == 1,    Print["There is ", Length[a], " solution when n = ", n]]; Do[Print[\!$$\*SuperscriptBox[\(a[\([q, 1]$$]\), $$"\<2\>"$$]\), " + ", \!$$\*SuperscriptBox[\(a[\([q, 2]$$]\), $$"\<2\>"$$]\), " + ", \!$$\*SuperscriptBox[\(a[\([q, 3]$$]\), $$"\<2\>"$$]\) " = ", "3x", \!$$\*SuperscriptBox[\(n$$, $$"\<2\>"$$]\)], {q, 1, Length[a]}]], {n,   3, 20}]As you can see there is no superscript printed when the number is 1.  Thanks in advancePaul.
3 Replies
Sort By:
Posted 10 years ago
 Great.  That's along the lines of the approach I was thinking of. --David
Posted 10 years ago
 Thanks David, I took a look at those suggestions and using a mixture of them I have solved the printing format. Do[a = Solve[(x^2 + y^2 + z^2)/3 == n^2 && y > 0 && x > 0 && z > 0 &&       x <= y && y <= z, {x, y, z}, Integers]; a = {x, y, z} /. a;   a = Flatten[a];   If[Length[a] > 3, a = Delete[a, {{-1}, {-2}, {-3}}];    a = Riffle[a, n, {4, -1, 4}]; a = Partition[a, 4];    If[Length[a] > 1,     Print[TextCell[      Row[{"There are ", Length[a], " solutions when n = ", n}]]]];   If[Length[a] == 1,    Print[TextCell[     Row[{"There is ", Length[a], " solution when n = ", n}]]]];   Do[Print[TextCell[     Row[{ExpressionCell[a[[q, 1]]]^2, " + ",        ExpressionCell[a[[q, 2]]]^2, " + ",        ExpressionCell[a[[q, 3]]]^2, " = 3x",        ExpressionCell[n]^2}]]], {q, 1, Length[a]}]], {n, 3, 20}]All is well thank you.Paul.
Posted 10 years ago
 When Mathematica evauates an expression--any expression--it performs first any appropriate simplifications.  Since 1 to any non-zero power (even if it is a string!) is 1, Mathematica first does this simplification. By the way, you might want to learn a bit about things like TextCell and ExpressionCell (along with Defer) and CellPrint so you can use Mathematica's powerful formatting abilities rather than using the ancient Print function.