How to put a condition for printing in mathematics

Click For Summary

Discussion Overview

The discussion revolves around how to conditionally print integer solutions from a mathematical computation in a programming context. Participants explore methods to filter results based on whether they yield integer values, focusing on a specific code implementation.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant seeks help to print only integer solutions from a loop that generates results based on a mathematical expression.
  • Another participant suggests using a list and the Select function to filter results, specifically using IntegerQ to retain only integer values.
  • A participant reports issues with the proposed solution, noting that it still prints non-integer results for certain values of n.
  • After some troubleshooting, the participant confirms the solution works and inquires about further refining the output.
  • Another suggestion includes using Length to count solutions and conditionally print results only if there are valid solutions.
  • One participant expresses gratitude for the assistance received, indicating the solution is effective.

Areas of Agreement / Disagreement

Participants generally agree on the approach to filter integer solutions, but there are moments of uncertainty regarding the implementation and output correctness. The discussion reflects a collaborative effort to refine the code without reaching a definitive consensus on the best method initially.

Contextual Notes

Some limitations in the code implementation were noted, such as the initial failure to filter out non-integer results, which required further adjustments. The discussion also highlights dependencies on specific programming functions and syntax.

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 :)
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 21 ·
Replies
21
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 15 ·
Replies
15
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K