How to put a condition for printing in mathematics

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
6 replies · 2K views
76Ahmad
Messages
46
Reaction score
0
Hi All ,

what I am trying to do is, after getting so many solution I just want to print only the intger solution:

For[n = 0, n ≤ 10, n++
Do[Print[{n, (n - Sqrt[Sqrt[
2]Sqrt[3n^2 - 9n +
8] + 3n - 4])/2,
OTHER SOLUTION, (n - Sqrt[-Sqrt[2]
Sqrt[3n^2 - 9n + 8] + 3n - 4])/2}]]]


there will be 10'th solution since n <= 10

put i only want to print if one or both of the Sqrt's are give an integer value.



Please help
thanks
 
Physics news on Phys.org
Use {} to put both results in a list and then use Select[list,IntegerQ] to keep only integers.

In[3]:= For[n=0,n≤10,n++ ,
Print[Flatten[{n,Select[{(n-Sqrt[Sqrt[2]Sqrt[3n^2-9n+8]+3n-4])/2,(n-Sqrt[-Sqrt[2] Sqrt[3n^2-9n+8]+3n-4])/2},IntegerQ]}]]
]

From In[3]:= {0,0}
From In[3]:= {1,0}
From In[3]:= {2,0,1}
From In[3]:= {3,0,1}
From In[3]:= {4}
From In[3]:= {5}
From In[3]:= {6}
From In[3]:= {7}
From In[3]:= {8,1,3}
From In[3]:= {9}
From In[3]:= {10}
 
I tryed to run like you write but its not runing with me.. I do not know wy?

But with your solution i can see its still printing for n =4,5,6,7,9,10 while these is not integer.
 
Ok its run now :) thanks

put what about the printing problem?
 
the out put should be like

{0,0}
{1,0}
{2,0,1
{3,0,1}
{8,1,3}
 
Then use Length[] to count how many solutions there are and If to only Print if there are solutions.

In[5]:=
For[n=0,n≤10,n++ ,
solutions=Select[{(n-Sqrt[Sqrt[2]Sqrt[3n^2-9n+8]+3n-4])/2,(n-Sqrt[-Sqrt[2] Sqrt[3n^2-9n+8]+3n-4])/2},IntegerQ];
If[Length[solutions]>0,Print[Flatten[{n,solutions}]]]
]

From In[5]:= {0,0}
From In[5]:= {1,0}
From In[5]:= {2,0,1}
From In[5]:= {3,0,1}
From In[5]:= {8,1,3}
 
Thaaaaaaaanks Bill its work 100%

you are really fantastic :)