ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 23 Sep 2020 02:28:45 -0500search for values in discrete rangehttps://ask.sagemath.org/question/53543/search-for-values-in-discrete-range/I am trying to search for solutions to a pair of equations but only using a few values. I know which values give the possible answers and I want to skip the computation time associated with checking every single value up within an interval. I am using the simple code:
for a in srange(0,200):
for b in srange(1,a+1):
if (a+b).is_square() and (a^2+b^2).is_square():
print(a,b)
which gives me the solutions I want but it computes every integer solution from 0 to 200. If I know the answers, can I search for the range like
for a in range(9,21,28,36,40,84,112,133,156,160)
and then the same as above, and it will print:
28, 21
40, 9
112, 84
156,133
160,36
Any idea how to make that work? This is not homework or anything, just doing a bit of research and I'm a new. Thanks!!Tue, 22 Sep 2020 19:29:57 -0500https://ask.sagemath.org/question/53543/search-for-values-in-discrete-range/Comment by FrédéricC for <p>I am trying to search for solutions to a pair of equations but only using a few values. I know which values give the possible answers and I want to skip the computation time associated with checking every single value up within an interval. I am using the simple code:</p>
<pre><code>for a in srange(0,200):
for b in srange(1,a+1):
if (a+b).is_square() and (a^2+b^2).is_square():
print(a,b)
</code></pre>
<p>which gives me the solutions I want but it computes every integer solution from 0 to 200. If I know the answers, can I search for the range like </p>
<p>for a in range(9,21,28,36,40,84,112,133,156,160)</p>
<p>and then the same as above, and it will print:</p>
<p>28, 21</p>
<p>40, 9</p>
<p>112, 84</p>
<p>156,133</p>
<p>160,36</p>
<p>Any idea how to make that work? This is not homework or anything, just doing a bit of research and I'm a new. Thanks!!</p>
https://ask.sagemath.org/question/53543/search-for-values-in-discrete-range/?comment=53546#post-id-53546Then syntax is
for a in (9,21,28,36,40,84,112,133,156,160):Wed, 23 Sep 2020 01:44:54 -0500https://ask.sagemath.org/question/53543/search-for-values-in-discrete-range/?comment=53546#post-id-53546Answer by Emmanuel Charpentier for <p>I am trying to search for solutions to a pair of equations but only using a few values. I know which values give the possible answers and I want to skip the computation time associated with checking every single value up within an interval. I am using the simple code:</p>
<pre><code>for a in srange(0,200):
for b in srange(1,a+1):
if (a+b).is_square() and (a^2+b^2).is_square():
print(a,b)
</code></pre>
<p>which gives me the solutions I want but it computes every integer solution from 0 to 200. If I know the answers, can I search for the range like </p>
<p>for a in range(9,21,28,36,40,84,112,133,156,160)</p>
<p>and then the same as above, and it will print:</p>
<p>28, 21</p>
<p>40, 9</p>
<p>112, 84</p>
<p>156,133</p>
<p>160,36</p>
<p>Any idea how to make that work? This is not homework or anything, just doing a bit of research and I'm a new. Thanks!!</p>
https://ask.sagemath.org/question/53543/search-for-values-in-discrete-range/?answer=53545#post-id-53545Just create the list of values to be searched, and loop on that:
A=(9,21,28,36,40,84,112,133,156,160)
for a in A:
for b in srange(1,a+1):
if (a+b).is_square() and (a^2+b^2).is_square():
print(a,b)
gives
28 21
40 9
112 84
156 133
160 36
Wed, 23 Sep 2020 01:44:39 -0500https://ask.sagemath.org/question/53543/search-for-values-in-discrete-range/?answer=53545#post-id-53545Comment by brennan for <p>Just create the list of values to be searched, and loop on that:</p>
<pre><code>A=(9,21,28,36,40,84,112,133,156,160)
for a in A:
for b in srange(1,a+1):
if (a+b).is_square() and (a^2+b^2).is_square():
print(a,b)
</code></pre>
<p>gives</p>
<pre><code>28 21
40 9
112 84
156 133
160 36
</code></pre>
https://ask.sagemath.org/question/53543/search-for-values-in-discrete-range/?comment=53547#post-id-53547Thank you so much!Wed, 23 Sep 2020 02:28:45 -0500https://ask.sagemath.org/question/53543/search-for-values-in-discrete-range/?comment=53547#post-id-53547