\\ Find the maximum sum of relatively prime integers \\ less than or equal to ns ns = 30; \\ a is the list of values for smaller n \\ used to calculate an upper bound for the sum global(a = [1, 3, 6, 8, 13, 13, 20, 24, 30, 30, 41, 41, 54, 54, 55, 63, 80, 80, 99, 99, 103, 103, 126, 126, 146, 146, 159, 164, 193]); \\ m is the best result achieved so far \\ used to decide about checking possiblities \\ initialized to the value if ns is not in the list global(m = a[ns - 1]); \\ recursive algorithm used in calculation \\ n is an integer between 1 and ns \\ the algorithm decides if n should be in the list \\ cl is the list so far, which will include integers \\ between ns and n+1 mlist(n,cl) = { local(s, nn, p, cw, cwo); \\ calculate sum of list s = sum(i = 1, #cl, cl[i]); \\ deciding if 1 should be in the list is easy \\ 1 is always in the list \\ this provides an end to the recursion if (n == 1, \\ adding 1 to the end of the list and \\ calculating the sum gives s + 1 \\ if this list is better than all previous \\ lists, set m to the sum of this list m = max(m, s + 1); return); \\ decide on nn, the next number to process \\ nn and product of list should be relatively prime \\ this is if n is added to the list nn = n - 1; p = n * prod(i = 1, #cl, cl[i]); while (gcd(nn,p) > 1, nn--); \\ same as above, but n is not added to the list nno = n - 1; po = prod(i = 1, #cl, cl[i]); while (gcd(nno,po) > 1, nno--); \\ cw is the upper bound for the sum if n is included cw = s + n + a[nn]; \\ cwo is the upper bound for the sum if n is not included cwo = s + a[nno]; \\ if both upper bounds are less than or equal to m, \\ it is not possible to find a better list, so give up if (cw <= m, return); \\ the upper bound with n included is greater than m, so \\ try checking the list with n included mlist(nn,concat(cl,n)); \\ if the upper bound with n not included is greater than \\ m, try checking the list with n not included if (cwo > m, mlist(nn,cl)) }; \\ Calculation of sequence entries \\ write first two write("b186736.txt","1 1"); write("b186736.txt","2 3"); \\ set up list of entries a=[1, 3]; \\ set up previous value m=3; \\ generate b-file for(n=3,250,\ ns = n;\ mlist(ns,[]);\ write("b186736.txt",n," ",m);\ a = concat(a,m));