Simply trying to understand these lines of code

asked 2016-05-21 23:48:03 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

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;
        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;
            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;

    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 ... (more)

edit retag flag offensive close merge delete


I'll try to come back and read your post thoroughly. Meanwhile, this could be of interest

A.Alharbi gravatar imageA.Alharbi ( 2016-05-26 15:04:06 -0500 )edit