import java.io.*;
import java.util.*;
import java.math.*;
//_____________________________________________________________
public class groups
{ 
   StringBuffer bufout = new StringBuffer();
   StringBuffer mysubset = new StringBuffer();
   String gen11  = "BRAJYO BRA ALHYM AO HJMYM VAO HARE";
   Random rand = new Random(37);
   int max = 0;
   String theval = "37"; 
   String pi = getFile2("pi.lst");
   int cntfnd = 1;
   String out = "";
   int max2 = 0;

//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   groups bnk = new groups();
   bnk.start(args);
}
//_____________________________________________________________
groups() throws Exception
{
}
//_____________________________________________________________
void start(String[] args) 
{
   System.out.println("This proves pattern 74.");
   saveFile("groups.txt","",false);
   int vv = Integer.parseInt("73");
   if (args.length > 0) vv = Integer.parseInt(args[0]);
   int domany = 100000;
   if (args.length > 1) domany = Integer.parseInt(args[1]);
   rand = new Random(vv);
   long t1 = System.nanoTime();
   start2(0,true);
   for (int i = 1; i < domany; i++)
   {
      pi = rndm();
      gen11 = mix(gen11);
      start2(i,false);
   }
   long t2 = System.nanoTime();
   System.out.println("\r\n" + (cntfnd-1) + " " + ((t2-t1)/1e9) + "\r\n");
   bufout.append("\r\n" + (cntfnd-1) + " " + ((t2-t1)/1e9) + "\r\n");
   saveFile("groups.txt",bufout.toString(),true);
}
//_____________________________________________________________
void start2(int ii, boolean first)
{
   StringBuffer buf = new StringBuffer();
   int total = 0;
   boolean ok = true;
   for (int j = 0; j <= 2; j++)
   {
      boolean found = false;
      for (int i = 0; i < Integer.parseInt(theval); i++)
      {
         if (startit(j,i))
         {
            buf.append(i + "\t");
            total += i;
            if (i != j) ok = false;
            found = true;
            break;
         }
         else
         {
            System.out.print("");
         }
      }
      if (!found)
      {
          total += Integer.parseInt(theval);;
          buf.append(">" + theval + "\t");
      }
   }
   String s = Integer.toString(ii+1) + " |\t" + buf.toString() + "| " + total;
   if (first)
   {
      max = total;
      if (ok) s += " FOUND ";
      s += "\r\n";
      System.out.print(s);
      saveFile("groups.txt",s,true);

   }
   else
   {
      if (total <= max) 
      {
         if (ok) 
         {
            s += " FOUND " + cntfnd++;
            s += "\r\n";
            System.out.print(s);
            saveFile("groups.txt",s,true);
         }
      }
   }
}
//___________________________________________________________________________
boolean startit(int wh, int wh3)
{
   String str3 = "";
   int cnt = 0;
   String[] gen = gen11.split(" ");
   for (int i = 0; i < gen.length; i++)
   {
      StringBuffer buf2 = new StringBuffer();
      String st = getsubset(gen[i].length());
      String[] ss = st.split("\r\n");
      for (int j = 0; j < ss.length; j++)
      {
         StringBuffer buf4 = new StringBuffer();
         String[] lk = ss[j].split(",");
         for (int k = 0; k < lk.length; k++)
         {
            int val2 = Integer.parseInt(lk[k]);
            String s = gen[i].substring(val2,val2+1);
            buf4.append(s);  
         }
         String str = buf4.toString();
         int pla = place(str);
         int val = value(str);
         int num = 1;
         if (wh == 0) num = val - pla;
         if (wh == 1) num = val + pla;
         if (wh == 2) num = val;
         if (num > max2) max2 = num;
         String numstr = Integer.toString(num);
         numstr = digits(numstr);
         num = Integer.parseInt(numstr);
         buf2.append(pi.substring(num+wh3,num+1+wh3));
         cnt++; 
     }
     String str2 = buf2.toString();
     str3 += str2;
   }
   if (check(str3,theval))
   {
      return(true);
   }
   return(false);
}
//___________________________________________________________________________
boolean check(String num, String fac)
{
   BigInteger bg1 = new BigInteger(num);
   BigInteger bg2 = new BigInteger(fac);
   BigInteger bg3 = new BigInteger("0");
   BigInteger bg4 = bg1.mod(bg2);
   if (bg4.equals(bg3)) return(true);
   return(false);  
}
//___________________________________________________________________________
String mix(String str)
{
   StringBuffer buf = new StringBuffer();
   String[] str2 = str.split(" ");
   for (int i = 0; i < str2.length; i++)
   {
      if (i > 0) buf.append(" ");
      buf.append(mixit(str2[i]));
   }
   return(buf.toString());
}
//___________________________________________________________________________
String mixit(String str)
{
   byte[] bb = str.getBytes();
   for (int i = 0; i < 10; i++)
   {
      int pos1 = rand.nextInt(str.length());
      int pos2 = rand.nextInt(str.length());
      byte bc = bb[pos1];
      bb[pos1] = bb[pos2];
      bb[pos2] = bc;
   }
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < bb.length; i++)
   {
      buf.append((char)bb[i]);
   }
   return(buf.toString());
}
//___________________________________________________________________________
String rndm()
{
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < 70000; i++)
   {
      String s = Integer.toString(rand.nextInt(10));
      buf.append(s);
   }
   return(buf.toString());
}
//___________________________________________________________________________
String getsubset(int len)
{
   mysubset = new StringBuffer();
   subit(len, 0, "");
   return(mysubset.toString());
}
//___________________________________________________________________________
void subit(int val, int start, String str)
{
   for (int i = start; i < val; i++)
   {
      String str2 = str + Integer.toString(i) + ",";
      mysubset.append(str2 + "\r\n");
      subit(val, i+1, str2);
   }
}
//__________________________________________________
int value(String str)
{
   int total = 0;
   out = "";
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 20;
      else if (st == 'l') v = 30;    
      else if (st == 'm') v = 40;
      else if (st == 'n') v = 50;
      else if (st == 'o') v = 60;
      else if (st == 'p') v = 70;
      else if (st == 'q') v = 80;
      else if (st == 'r') v = 90;
      else if (st == 's') v = 100;
      else if (st == 't') v = 200;
      else if (st == 'u') v = 300;
      else if (st == 'v') v = 400;
      else if (st == 'w') v = 500;
      else if (st == 'x') v = 600;
      else if (st == 'y') v = 700;
      else if (st == 'z') v = 800;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 20;
      else if (st == 'L') v = 30;
      else if (st == 'M') v = 40;
      else if (st == 'N') v = 50;
      else if (st == 'S') v = 60;
      else if (st == 'I') v = 70;
      else if (st == 'F') v = 80;
      else if (st == 'E') v = 90;
      else if (st == 'Q') v = 100;
      else if (st == 'R') v = 200;
      else if (st == 'J') v = 300;
      else if (st == 'O') v = 400;
      total += v;
      if (i > 0) out += " + ";
      out += Integer.toString(v);
   }
   return(total);
}
//__________________________________________________
int place(String str)
{
   int total = 0;
   out = "";
   for (int i = 0; i < str.length(); i++)
   {
      int v = 0;
      String st2 = str.substring(i,i+1);
      byte[] st3 = st2.getBytes();
      byte st = st3[0];
      if (st == 'a') v = 1;
      else if (st == 'b') v = 2;
      else if (st == 'c') v = 3;
      else if (st == 'd') v = 4;
      else if (st == 'e') v = 5;
      else if (st == 'f') v = 6;
      else if (st == 'g') v = 7;
      else if (st == 'h') v = 8;
      else if (st == 'i') v = 9;
      else if (st == 'j') v = 10;
      else if (st == 'k') v = 11;
      else if (st == 'l') v = 12;    
      else if (st == 'm') v = 13;
      else if (st == 'n') v = 14;
      else if (st == 'o') v = 15;
      else if (st == 'p') v = 16;
      else if (st == 'q') v = 17;
      else if (st == 'r') v = 18;
      else if (st == 's') v = 19;
      else if (st == 't') v = 20;
      else if (st == 'u') v = 21;
      else if (st == 'v') v = 22;
      else if (st == 'w') v = 23;
      else if (st == 'x') v = 24;
      else if (st == 'y') v = 25;
      else if (st == 'z') v = 26;
      else if (st == 'A') v = 1;
      else if (st == 'B') v = 2;
      else if (st == 'G') v = 3;
      else if (st == 'D') v = 4;
      else if (st == 'H') v = 5;
      else if (st == 'V') v = 6;
      else if (st == 'Z') v = 7;
      else if (st == 'C') v = 8;
      else if (st == 'T') v = 9;
      else if (st == 'Y') v = 10;
      else if (st == 'K') v = 11;
      else if (st == 'L') v = 12;
      else if (st == 'M') v = 13;
      else if (st == 'N') v = 14;
      else if (st == 'S') v = 15;
      else if (st == 'I') v = 16;
      else if (st == 'F') v = 17;
      else if (st == 'E') v = 18;
      else if (st == 'Q') v = 19;
      else if (st == 'R') v = 20;
      else if (st == 'J') v = 21;
      else if (st == 'O') v = 22;
      total += v;
      if (i > 0) out += " + ";
      out += Integer.toString(v);
   }
   return(total);
}
//___________________________________________________________________________
void saveFile(String file, String str, boolean append) 
{
  try
  {
   BufferedWriter out = new BufferedWriter(new FileWriter(file, append));
   out.write(str);
   out.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
}
//___________________________________________________________________________
String getFile2(String file) 
{
   StringBuffer buf=new StringBuffer();String str;
  try
  {
   BufferedReader in = new BufferedReader (new FileReader (file));
   while((str=in.readLine())!=null)
   {
      buf.append(str);
   }
   in.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return (buf.toString());
}
//___________________________________________________________________________
String digits(String str)
{
   int total = 0;
   for (int i = 0; i < str.length(); i++)
   {
      String s = str.substring(i,i+1);
      int v = Integer.parseInt(s);
      total += v;
   }
   return(Integer.toString(total));
}
}//__________________________________________________________________________
