ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 26 May 2016 15:04:06 -0500Simply trying to understand these lines of codehttp://ask.sagemath.org/question/33505/simply-trying-to-understand-these-lines-of-code/ Very new to sage, and trying to decipher all that's going on in the following code:
ndirect = 4;
p = [];
for i in range (0,25):
p.append (Primes ().unrank (i));
def calculate144311 (n):
return recurseDirectly (n,0,[0] * n) - 1;
def recurseDirectly (n,np,residues):
result = 0;
if (np < ndirect):
residues [np] = 0;
lim = p [np] - 1;
while residues [np] < lim:
if residues [np] != 1:
result = max (result,recurseDirectly (n,np + 1, residues));
residues [np] = residues [np] + 1;
else:
remains = [];
for r in range (1,2000):
for i in range (0,ndirect):
toAdd = true;
res = (r + residues [i]) % p [i];
if res == 1 or res == p [i] - 1:
toAdd = false;
break;
if (toAdd):
remains.append (r);
result = max (result,recurse (n,0,ndirect,remains,[false] * n,[0] * n,residues));
return result;
def recurse (n,d,nused,remains,used,primes,residues):
result = 0;
repeat = true;
while (repeat):
result = max (result,remains [d]);
repeat = false;
for i in range (ndirect,nused):
r = (residues [i] + remains [d]) % primes [i];
if r == 1 or r == primes [i] - 1:
d += 1;
repeat = true;
break;
for i in range (ndirect,n):
if not used [i]:
pi = p [i];
for r in [-1,1]:
res = pi - ((r + remains [d] - 1) % pi + 1);
if res != 1 and res != pi - 1:
residues [nused] = res;
primes [nused] = pi;
used [i] = true;
result = max (result,recurse (n,d + 1,nused + 1,remains,used,primes,residues));
used [i] = false;
return result;
for i in range (ndirect,17):
print (i,calculate144311(i));
Here's my current grasp of what's going on. My hope is that someone will guide me through my misunderstandings and help fully get how to code similarly in sage:
The final 2 lines of code are what sage is running; everything before given the information that it'll use to run it. It will create a list of outputs for "calculate 144311" using the inputs in the range of ndirect (defined in line 1 as it is used throughout the code) up to whatever number I choose as a stopping point (in this case, 17).
The code first looks at "def calculate 144311" (def meaning "define"?) and is told to run "recurseDirectly with three inputs, "n, 0, and 0 times n? (need some help here). It then subracts 1 from... all three?
Since recurseDirectly is defined as having inputs of "n, np, and residues" this begin with values given by "calculate 144311". First, the code sets the result to 0. We then begin an if then with "np (which started at 0) < ndirect" being the qualifier. If True, it runs the "if". False runs the "else".
The if begins by setting the residues of np to 0 (how does this relate to "residues" above?), sets lim to the product of p and np minus 1 (it appears from above the p begins as an empty set and is then populated with the first "i" primes up to a max of 25?).
As part of the "if", while runs a loop as long as residues of np are less than lim. This while contains its own if then. if residues doesn't equal 1, then if sets to the result to the max of... I don't get what it's doing here. Need help. It then adds 1 to the residues of np.
Now we move to the else:
"remains" starts as an empty set. The "for" command loops the following as long as r and i are within the range (do these count up by one in this case?). toAdd starts true. res... well, I don't know what's happening here. Another if: res =1 or the product of p and i minus 1, toAdd then becomes false. Don't know what break does. Then, if toAdd is True, remains has r added as an element of its set. Result is then set to the max of the following... which I don't get. However, this triggers "recurse". And a result is returned... meaning?
Next, recurse.
This has 7 inputs gained from above. Result again begins at 0. Repeat begins true. While it's true, there's a loop. Result becomes the bigger of itself or the remains of d. repeat becomes false. For loops code while i is between ndirect and nused (counting up by 1?). r becomes... something.
I'm going to stop there as this is already too long.
Would love to be able to code something like this myself, but I need some guidance understanding what's going on.
Thanks!Sat, 21 May 2016 23:48:03 -0500http://ask.sagemath.org/question/33505/simply-trying-to-understand-these-lines-of-code/Comment by A.Alharbi for <div class="snippet"><p>Very new to sage, and trying to decipher all that's going on in the following code:</p>
<pre><code>ndirect = 4;
p = [];
for i in range (0,25):
p.append (Primes ().unrank (i));
def calculate144311 (n):
return recurseDirectly (n,0,[0] * n) - 1;
def recurseDirectly (n,np,residues):
result = 0;
if (np < ndirect):
residues [np] = 0;
lim = p [np] - 1;
while residues [np] < lim:
if residues [np] != 1:
result = max (result,recurseDirectly (n,np + 1, residues));
residues [np] = residues [np] + 1;
else:
remains = [];
for r in range (1,2000):
for i in range (0,ndirect):
toAdd = true;
res = (r + residues [i]) % p [i];
if res == 1 or res == p [i] - 1:
toAdd = false;
break;
if (toAdd):
remains.append (r);
result = max (result,recurse (n,0,ndirect,remains,[false] * n,[0] * n,residues));
return result;
def recurse (n,d,nused,remains,used,primes,residues):
result = 0;
repeat = true;
while (repeat):
result = max (result,remains [d]);
repeat = false;
for i in range (ndirect,nused):
r = (residues [i] + remains [d]) % primes [i];
if r == 1 or r == primes [i] - 1:
d += 1;
repeat = true;
break;
for i in range (ndirect,n):
if not used [i]:
pi = p [i];
for r in [-1,1]:
res = pi - ((r + remains [d] - 1) % pi + 1);
if res != 1 and res != pi - 1:
residues [nused] = res;
primes [nused] = pi;
used [i] = true;
result = max (result,recurse (n,d + 1,nused + 1,remains,used,primes,residues));
used [i] = false;
return result;
for i in range (ndirect,17):
print (i,calculate144311(i));
</code></pre>
<p>Here's my current grasp of what's going on. My hope is that someone will guide me through my misunderstandings and help fully get how to code similarly in sage:</p>
<p>The final 2 lines of code are what sage is running; everything before given the information that it'll use to run it. It will create a list of outputs for "calculate 144311" using the inputs in the range of ndirect (defined in line 1 as it is used throughout the code) up to whatever number I choose as a stopping point (in this case, 17). </p>
<p>The code first looks at "def calculate 144311" (def meaning "define"?) and is told to run "recurseDirectly with three inputs, "n, 0, and 0 times n? (need some help here). It then subracts 1 from... all three?</p>
<p>Since recurseDirectly is defined as having inputs of "n, np, and residues" this begin with values given by "calculate 144311". First, the code sets the result to 0. We then begin an if then with "np (which started at 0) < ndirect" being the qualifier. If True, it runs the "if". False runs the "else".</p>
<p>The if begins by setting the residues of np to 0 (how does this relate to "residues" above?), sets lim to the product of p and np minus 1 (it appears from above the p begins as an empty set and is then populated with the first "i" primes up to a max of 25?).</p>
<p>As part of the "if", while runs ...<span class="expander"> <a>(more)</a></span></p></div>http://ask.sagemath.org/question/33505/simply-trying-to-understand-these-lines-of-code/?comment=33559#post-id-33559I'll try to come back and read your post thoroughly. Meanwhile, this could be of interest http://doc.sagemath.org/html/en/reference/repl/sage/misc/trace.htmlThu, 26 May 2016 15:04:06 -0500http://ask.sagemath.org/question/33505/simply-trying-to-understand-these-lines-of-code/?comment=33559#post-id-33559