Skip to content

Instantly share code, notes, and snippets.

@joriki
Created May 10, 2016 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joriki/61860c6d85853d39f6b470ec0c42e290 to your computer and use it in GitHub Desktop.
Save joriki/61860c6d85853d39f6b470ec0c42e290 to your computer and use it in GitHub Desktop.
Compute OEIS sequence A144311; see http://math.stackexchange.com/questions/1779109.
import java.util.ArrayList;
import java.util.List;
public class Question1779109 {
final static int [] p = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
final static int maxLength = 2000;
final static int ndirect = 7;
static int max;
public static void main (String [] args) {
for (int n = ndirect;;n++) {
recurseDirectly (n,0,new int [n]);
System.out.println (n + " : " + (max - 1));
}
}
static void recurseDirectly (int n,int np,int [] residues) {
if (np < ndirect) {
for (residues [np] = 0;residues [np] < p [np] - 1;residues [np]++)
if (residues [np] != 1)
recurseDirectly (n,np + 1, residues);
}
else {
List<Integer> remainsList = new ArrayList<Integer> ();
outer:
for (int r = 1;r <= maxLength;r++) {
for (int i = 0;i < ndirect;i++) {
int res = (r + residues [i]) % p [i];
if (res == 1 || res == p [i] - 1)
continue outer;
}
remainsList.add (r);
}
int [] remains = new int [remainsList.size ()];
for (int i = 0;i < remains.length;i++)
remains [i] = remainsList.get (i);
recurse (n,0,ndirect,remains,new boolean [n],new int [n],residues);
}
}
static void recurse (int n,int d,int nused,int [] remains,boolean [] used,int [] primes,int [] residues) {
outer:
for (;;) {
max = Math.max (max,remains [d]);
for (int i = ndirect;i < nused;i++) {
int r = (residues [i] + remains [d]) % primes [i];
if (r == 1 || r == primes [i] - 1) {
d++;
continue outer;
}
}
break;
}
for (int i = ndirect;i < n;i++)
if (!used [i])
for (int r = -1;r <= 1;r += 2) {
int res = p [i] - ((r + remains [d] - 1) % p [i] + 1);
if (res != 1 && res != p [i] - 1) {
residues [nused] = res;
primes [nused] = p [i];
used [i] = true;
recurse (n,d + 1,nused + 1,remains,used,primes,residues);
used [i] = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment