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.Thu, 10 Jul 2014 03:51:06 +0200How to do an "if" block in Sage?https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/ I am trying to do an if block in sage. For example, I have code:
for n in range(7):
if gcd(n,3)==3:
n=n/3;
n=n+1;
print(n)
This "should" only add one when n is divisible by 3, but seems to do this all the time. Any ideas?
Wed, 09 Jul 2014 23:36:24 +0200https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/Comment by calc314 for <p>I am trying to do an if block in sage. For example, I have code:</p>
<pre><code>for n in range(7):
if gcd(n,3)==3:
n=n/3;
n=n+1;
print(n)
</code></pre>
<p>This "should" only add one when n is divisible by 3, but seems to do this all the time. Any ideas?</p>
https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?comment=23324#post-id-23324For your first bit of code, if you print n before and after your "if" block, you will see that the value of n is only modified when n is divisible by 3.Thu, 10 Jul 2014 00:04:36 +0200https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?comment=23324#post-id-23324Comment by calc314 for <p>I am trying to do an if block in sage. For example, I have code:</p>
<pre><code>for n in range(7):
if gcd(n,3)==3:
n=n/3;
n=n+1;
print(n)
</code></pre>
<p>This "should" only add one when n is divisible by 3, but seems to do this all the time. Any ideas?</p>
https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?comment=23323#post-id-23323Can you put the code from your comment in your original question so we can see the formatting better?Thu, 10 Jul 2014 00:01:31 +0200https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?comment=23323#post-id-23323Comment by ClemFanJC07 for <p>I am trying to do an if block in sage. For example, I have code:</p>
<pre><code>for n in range(7):
if gcd(n,3)==3:
n=n/3;
n=n+1;
print(n)
</code></pre>
<p>This "should" only add one when n is divisible by 3, but seems to do this all the time. Any ideas?</p>
https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?comment=23320#post-id-23320Specifically, I am trying to sum up al multinomial coefficients that have two equal parts. I am using the code:
for n in range(21):
t=0;
for i in range(n+1):
t = t + Combinations(n,i).cardinality()*Combinations(n-i,i).cardinality();
t=3*t;
if gcd(n,3)==3:
t = t-2*Combinations(n, n/3).cardinality()*Combinations(2*n/3, n/3).cardinality();
print t
However, for n=3 the answer should be 18 not 15. Ideas?Wed, 09 Jul 2014 23:43:02 +0200https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?comment=23320#post-id-23320Answer by Rolandb for <p>I am trying to do an if block in sage. For example, I have code:</p>
<pre><code>for n in range(7):
if gcd(n,3)==3:
n=n/3;
n=n+1;
print(n)
</code></pre>
<p>This "should" only add one when n is divisible by 3, but seems to do this all the time. Any ideas?</p>
https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?answer=23327#post-id-23327Maybe you get confused by the fact that gcd(0,3) equals 3.
If I print each step, it works as expected.
for n in range(9):
print "n=",n
if gcd(n,3)==3:
print "?"
n=n/3
n=n+1
print "*",n
n= 0
?
* 1
n= 1
* 1
n= 2
* 2
n= 3
?
* 2
n= 4
* 4
n= 5
* 5
n= 6
?
* 3
n= 7
* 7
n= 8
* 8Thu, 10 Jul 2014 00:17:08 +0200https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?answer=23327#post-id-23327Comment by slelievre for <p>Maybe you get confused by the fact that gcd(0,3) equals 3.
If I print each step, it works as expected.</p>
<pre><code>for n in range(9):
print "n=",n
if gcd(n,3)==3:
print "?"
n=n/3
n=n+1
print "*",n
n= 0
?
* 1
n= 1
* 1
n= 2
* 2
n= 3
?
* 2
n= 4
* 4
n= 5
* 5
n= 6
?
* 3
n= 7
* 7
n= 8
* 8
</code></pre>
https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?comment=23336#post-id-23336If you indent by 4 spaces the code and outpout that you pasted in your answer, it will be displayed nicely in a code block. When editing your answer, you can also select the relevant lines and click the "code" button which has little digits "101 010", next to the "bold", "italics" etc buttons, and this will put your code in an appropriate block so that it will be displayed nicely.Thu, 10 Jul 2014 03:51:06 +0200https://ask.sagemath.org/question/23319/how-to-do-an-if-block-in-sage/?comment=23336#post-id-23336