ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 17 Feb 2020 04:33:07 +0100looping of equality functionhttps://ask.sagemath.org/question/49898/looping-of-equality-function/ Hi, I have the equation `x^4+y^4=z^3`. I want to run each variable from `-100` till `100` to see which numbers satisfy this equation. Here is what I have done:
x,y,z= var('x y z')
for x in range(-100, 100):
for y in range(-100, 100):
for z in range(-100, 100):
x^4+y^4==z^3
print(x,y,z)
There is definitely something wrong with the coding that's not giving me the desired answer. Can someone enlighten me, please.
Thu, 13 Feb 2020 04:52:34 +0100https://ask.sagemath.org/question/49898/looping-of-equality-function/Comment by John Palmieri for <p>Hi, I have the equation <code>x^4+y^4=z^3</code>. I want to run each variable from <code>-100</code> till <code>100</code> to see which numbers satisfy this equation. Here is what I have done:</p>
<pre><code>x,y,z= var('x y z')
for x in range(-100, 100):
for y in range(-100, 100):
for z in range(-100, 100):
x^4+y^4==z^3
print(x,y,z)
</code></pre>
<p>There is definitely something wrong with the coding that's not giving me the desired answer. Can someone enlighten me, please.</p>
https://ask.sagemath.org/question/49898/looping-of-equality-function/?comment=49909#post-id-49909It was clear what answers you were hoping for, but you said it wasn't giving the desired answer. So again: what answers was it giving?Thu, 13 Feb 2020 17:28:25 +0100https://ask.sagemath.org/question/49898/looping-of-equality-function/?comment=49909#post-id-49909Comment by John Palmieri for <p>Hi, I have the equation <code>x^4+y^4=z^3</code>. I want to run each variable from <code>-100</code> till <code>100</code> to see which numbers satisfy this equation. Here is what I have done:</p>
<pre><code>x,y,z= var('x y z')
for x in range(-100, 100):
for y in range(-100, 100):
for z in range(-100, 100):
x^4+y^4==z^3
print(x,y,z)
</code></pre>
<p>There is definitely something wrong with the coding that's not giving me the desired answer. Can someone enlighten me, please.</p>
https://ask.sagemath.org/question/49898/looping-of-equality-function/?comment=49900#post-id-49900First, use `if`. Second, what answer are you getting? If you are using this code in a Python file (as opposed to a Sage file), then the `^` symbol will be interpreted as in Python as bitwise exclusive or (https://docs.python.org/3/reference/expressions.html#binary-bitwise-operations). In a Sage file, `^` is converted to `**`.Thu, 13 Feb 2020 07:11:35 +0100https://ask.sagemath.org/question/49898/looping-of-equality-function/?comment=49900#post-id-49900Comment by Sha for <p>Hi, I have the equation <code>x^4+y^4=z^3</code>. I want to run each variable from <code>-100</code> till <code>100</code> to see which numbers satisfy this equation. Here is what I have done:</p>
<pre><code>x,y,z= var('x y z')
for x in range(-100, 100):
for y in range(-100, 100):
for z in range(-100, 100):
x^4+y^4==z^3
print(x,y,z)
</code></pre>
<p>There is definitely something wrong with the coding that's not giving me the desired answer. Can someone enlighten me, please.</p>
https://ask.sagemath.org/question/49898/looping-of-equality-function/?comment=49902#post-id-49902the answers I am planning to obtain is the list of numbers for x, y, z that satisfies the equation x^4+y^4=z^3.Thu, 13 Feb 2020 09:53:18 +0100https://ask.sagemath.org/question/49898/looping-of-equality-function/?comment=49902#post-id-49902Comment by Sha for <p>Hi, I have the equation <code>x^4+y^4=z^3</code>. I want to run each variable from <code>-100</code> till <code>100</code> to see which numbers satisfy this equation. Here is what I have done:</p>
<pre><code>x,y,z= var('x y z')
for x in range(-100, 100):
for y in range(-100, 100):
for z in range(-100, 100):
x^4+y^4==z^3
print(x,y,z)
</code></pre>
<p>There is definitely something wrong with the coding that's not giving me the desired answer. Can someone enlighten me, please.</p>
https://ask.sagemath.org/question/49898/looping-of-equality-function/?comment=49958#post-id-49958oh sorry... it was giving an error code every time I ran the code.Mon, 17 Feb 2020 04:33:07 +0100https://ask.sagemath.org/question/49898/looping-of-equality-function/?comment=49958#post-id-49958Answer by jipilab for <p>Hi, I have the equation <code>x^4+y^4=z^3</code>. I want to run each variable from <code>-100</code> till <code>100</code> to see which numbers satisfy this equation. Here is what I have done:</p>
<pre><code>x,y,z= var('x y z')
for x in range(-100, 100):
for y in range(-100, 100):
for z in range(-100, 100):
x^4+y^4==z^3
print(x,y,z)
</code></pre>
<p>There is definitely something wrong with the coding that's not giving me the desired answer. Can someone enlighten me, please.</p>
https://ask.sagemath.org/question/49898/looping-of-equality-function/?answer=49904#post-id-49904Defining `x,y,z` to be variables and then using them as integers is not really appropriate. The indentation was also missing, along with an if.
You do not need to declare the variables if you use them in a range:
sage: for x in range(-100,101):
....: for y in range(-100,101):
....: for z in range(-100,101):
....: if x^4+y^4 == z^3:
....: print(x,y,z)
You can also use more advanced iterators and shorten the code:
sage: import itertools
sage: for x,y,z in itertools.product(srange(-100,101),repeat=3):
....: if x^4 + y^4 == z^3:
....: print(x,y,z)Thu, 13 Feb 2020 10:30:16 +0100https://ask.sagemath.org/question/49898/looping-of-equality-function/?answer=49904#post-id-49904