How to put a condition for printing in mathematics

In summary, To print only the integer solutions from a given set of solutions, use the {} to put the results in a list, and then use Select[list, IntegerQ] to keep only the integer solutions. To only print the solutions if there are any, use Length[] to count the solutions and If to only print if there are solutions.
  • #1
76Ahmad
48
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
  • #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}
 
  • #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.
 
  • #4
Ok its run now :) thanks

put what about the printing problem?
 
  • #5
the out put should be like

{0,0}
{1,0}
{2,0,1
{3,0,1}
{8,1,3}
 
  • #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}
 
  • #7
Thaaaaaaaanks Bill its work 100%

you are really fantastic :)
 

Related to How to put a condition for printing in mathematics

1. How do I write a conditional statement in math?

To write a conditional statement in math, use the "if-then" format. For example, "if x is even, then x+1 is odd." This statement means that when the condition (x is even) is met, the result (x+1 is odd) will also be true.

2. What is the purpose of using a condition for printing in mathematics?

The purpose of using a condition for printing in mathematics is to specify which numbers or expressions should be printed based on certain criteria. This allows for more precise and organized mathematical statements.

3. Can I use multiple conditions for printing in mathematics?

Yes, multiple conditions can be used for printing in mathematics. This is known as a compound statement, where two or more conditions are connected by logical operators such as "and" or "or". For example, "if x is less than 10 and greater than 5, then print x."

4. How do I read a conditional statement in math?

To read a conditional statement in math, start with the "if" part and determine what condition needs to be met. Then, move on to the "then" part and see what result will follow if the condition is met. For example, "if x is positive, then x squared is also positive."

5. What happens if the condition for printing in mathematics is not met?

If the condition for printing in mathematics is not met, then the statement will not be printed. This means that the result will only be shown if the condition is satisfied. If the condition is not satisfied, the statement will be skipped over and the program will continue to the next line of code.

Similar threads

  • Introductory Physics Homework Help
Replies
21
Views
1K
  • Precalculus Mathematics Homework Help
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • General Math
Replies
8
Views
2K
  • Calculus and Beyond Homework Help
Replies
2
Views
428
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
862
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Precalculus Mathematics Homework Help
Replies
4
Views
430
Back
Top