import java.io.*;
import java.util.*;
import java.net.*;
//_____________________________________________________________
public class piephi
{ 
   static StringBuffer bufout = new StringBuffer();
   int big = 50000000;
   static String gen11  = "BRAJYO BRA ALHYM AO HJMYM VAO HARE"; 

   //String pi = getFile2("pi.txt");
   //String e = getFile2("e.txt");
   //String phi = getFile2("phi.txt");

   String pi = getFile2("pi.lst");
   String e = getFile2("e.lst");
   String phi = getFile2("phi.lst");

   String rnd = "";
   String genmix = "";
   int seed = 0;
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   long t1 = System.nanoTime();
   piephi bnk = new piephi();
   saveFile("output.txt","",false);
   bufout.append(gen11 + "\r\n");
   for (int i = 0; i < 5; i++)
   {
      bnk.start(i);
      System.out.println("");
      bufout.append("\r\n");
   }
   gen11 = "in the begi nning man evo lved from the big bang";
   File file = new File("nonsense.txt");
   if (file.exists()) gen11 = getFile2("nonsense.txt");
   bufout.append("\r\n" + gen11 + "\r\n");
   for (int i = 0; i < 5; i++)
   {
      bnk.start(i);
      System.out.println("");
      bufout.append("\r\n");
   }

   saveFile("output.txt",bufout.toString(),false);
   long t2 = System.nanoTime();
   System.out.println("seconds = " + ((t2-t1)/1e9));
}
//___________________________________________________________________________
void start(int which)
{
   System.out.println("This proves pattern 61.");
   int maxx = 170;
   int[] vl = new int[8];
   for (int i = 0; i < maxx; i++)
   {
      StringBuffer buf = new StringBuffer();
      seed = i;
      rnd = random(big);
      genmix = mix(gen11);
      int[] vl2 = new int[8];
      String val2 = "0";
      String val3 = "0";
      if (which == 0) val2 = e;
      else if (which == 1) val2 = phi;
      else if (which == 2)
      {
         val2 = e;
         val3 = phi;
      }
      else if (which == 3);
      else if (which == 4) val2 = rnd;
      vl2[0] = make(gen11,pi,val2,val3);
      vl2[1] = make(genmix,pi,val2,val3);
      int min = big;
      int minpos = -1;
      for (int j = 0; j < 2; j++)
      {
         if (vl2[j] < min)
         {
             min = vl2[j];
             minpos = j;  
         }
      }
      if (minpos >= 0) vl[minpos]++;
      if (which == 0) buf.append("pi e      ");
      if (which == 1) buf.append("pi phi    ");
      if (which == 2) buf.append("pi e phi  ");
      if (which == 3) buf.append("pi        ");
      if (which == 4) buf.append("pi random ");
      buf.append((i+1) + " " + minpos + " : ");
      for (int j = 0; j < 2; j++)
      {
         buf.append(vl[j] + " ");
      }
      buf.append("| ");
      for (int j = 0; j < 2; j++)
      {
         buf.append(vl2[j] + " ");
      }
      buf.append("\r\n");
      System.out.print(buf.toString());
      bufout.append(buf.toString());
   }
}
//___________________________________________________________________________
int make(String look, String number, String number2, String number3)
{
   byte[] lk = look.replace(" ","").getBytes();
   int total = 0;
   byte[] bb = number.getBytes();
   byte[] bb2 = number2.getBytes();
   byte[] bb3 = number3.getBytes();
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < big; i++)
   {
       int val = bb[i % bb.length] - '0';
       int val2 = bb2[i % bb2.length] - '0';  
       int val3 = bb3[i % bb3.length] - '0';
       total += val + val2 + val3;
       int pos = total % lk.length;
       buf.append((char)lk[pos]);
   }
   String out = buf.toString();
   String[] words = look.split(" ");
   int max = 0;
   for (int i = 0; i < words.length; i++)
   {
      int loc = out.indexOf(words[i]);
      if (loc >= 0)
      {
         if (loc > max)
         {
            max = loc;
         }       
      }
      else
      {
         max = big;
      }
   }
   return(max);
}
//___________________________________________________________________________
String mix(String str)
{
   Random rand = new Random(seed);
   StringBuffer buf = new StringBuffer();
   String[] words = gen11.split(" ");
   for (int i = 0; i < words.length; i++)
   {
      byte[] bb = words[i].getBytes();
      for(int j = 0; j < 100; j++)
      {
         int rnd1 = rand.nextInt(bb.length);
         int rnd2 = rand.nextInt(bb.length);
         byte old = bb[rnd1];
         bb[rnd1] = bb[rnd2];
         bb[rnd2] = old;
      }
      String out = new String(bb);
      if (i > 0) buf.append(" ");
      buf.append(out);
   }
   return(buf.toString());
}
//___________________________________________________________________________
String random(int count)
{
   Random rand = new Random(seed);
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < count; i++)
   {
      int val = rand.nextInt(10);
      buf.append(Integer.toString(val));
   }
   return(buf.toString());
}
//___________________________________________________________________________
public static 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());
}
//___________________________________________________________________________
public static 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);
  }
}
}//__________________________________________________________________________
