Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How to put a condition for printing in mathematics

  1. Jun 24, 2011 #1
    Hi All ,

    what im 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
     
  2. jcsd
  3. Jun 24, 2011 #2
    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}
     
  4. Jun 24, 2011 #3
    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.
     
  5. Jun 24, 2011 #4
    Ok its run now :) thanks

    put what about the printing problem?
     
  6. Jun 24, 2011 #5
    the out put should be like

    {0,0}
    {1,0}
    {2,0,1
    {3,0,1}
    {8,1,3}
     
  7. Jun 24, 2011 #6
    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}
     
  8. Jun 24, 2011 #7
    Thaaaaaaaanks Bill its work 100%

    you are really fantastic :)
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook