// 27,344,230 sentences
import java.io.*;
import java.util.*;
import java.net.*;
//_____________________________________________________________
public class fail1
{ 
   String hobbitt = "in a hole in the ground there lived a hobbit";
   int digit = 0; // 1 2 3 4 5 6 7 8 9 
   int len = 10;
   String pi = getFile("pi.mil");
   String e = getFile("e.mil");
   String phi = getFile("phi.mil");
   int[] primes = new int[1000];
   int[] counteach = new int[13];
   String out = "";
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   fail1 bnk = new fail1();
   bnk.setup(args);
   bnk.start();
}
//_____________________________________________________________
fail1() throws Exception 
{
}
//___________________________________________________________________________
void setup(String[] args)
{
   digit = 0;
   if (args.length > 0) digit = Integer.parseInt(args[0]);
   int pos = 1;
   primes[0] = 2;
   for (int i = 2; i < 1000000; i++)
   {
      boolean isprime = true;
      for (int j = 2; j < i; j++)
      {
         if (i % j == 0)
         {
            isprime = false;
            break;
         }
      }
      if (isprime)
      {
         primes[pos++] = i;
         if (pos >= 1000) break;
      }
   }
}
//___________________________________________________________________________
void start()
{
   start3();
   long t1 = System.nanoTime();
   int count = 0;
   int[] cnts = new int[13];
   int[] cnts2 = new int[13];
   System.out.println(test(hobbitt));
   String[] words = getFile("wordsbooks.txt").split("\r\n");
   for (int i = 0; i < words.length - len; i++)
   {
      StringBuffer sentence = new StringBuffer();
      String[] look = new String[len];
      for (int j = 0; j < len; j++)
      {
         look[j] = words[i+j];
         if (j > 0) sentence.append(" ");
         sentence.append(look[j]);
      }
      int cnt = test(sentence.toString());
      cnts[cnt]++;
      cnts2[cnt]++;
      if (i % 1000 == 0) System.out.print(".");
      if (i % 100000 == 0)
      {

         System.out.print("\r\n" + count++ + " | ");
         for (int j = 0; j < cnts.length; j++)
         {
            System.out.print(cnts[j] + " ");
         }
         System.out.println("");
         for (int j = 0; j < cnts.length; j++)
         {
            System.out.print(counteach[j] + " ");
         }
         long t2 = System.nanoTime();
         System.out.println(" | " + ((t2-t1)/1e9));
         cnts2 = new int[13];
         counteach = new int[13];

      }
   }
   System.out.println("\r\n--------------------------------");
   for (int i = 0; i < cnts.length; i++)
   {
      System.out.print(cnts[i] + " ");
   }
   System.out.println("");
}
//___________________________________________________________________________
void start3()
{
   File f = new File("wordsbooks.txt");
   if(f.exists()) return;
   StringBuffer buf = new StringBuffer();
   String[] list = getFile("books.lst").split("\r\n");
   for (int i = 0; i <list.length; i++)
   {
      System.out.println(list[i]);
      String file = getUrl("http://www.textfiles.com/etext/FICTION/" + list[i]);
      String[] lines = file.split("\r\n");
      for (int j = 0; j < lines.length; j++)
      {
         String[] words = lines[j].split(" ");
         for (int k = 0; k < words.length; k++)
         {
            String str = clean(words[k].trim());
            if (!str.equals(""))
            {
               buf.append(str + "\r\n");
            }
         }
      }
   }
   saveFile("wordsbooks.txt",buf.toString(),false);
}
//___________________________________________________________________________
String clean(String str)
{
   StringBuffer buf = new StringBuffer();
   String str2 = str.toLowerCase();
   byte[] bb = str2.getBytes();
   for (int i = 0; i < bb.length; i++)
   {
      if ((bb[i] >= 'a') & (bb[i] <= 'z'))
      {
         buf.append((char)bb[i]);
      }
   }
   return(buf.toString());
}
//___________________________________________________________________________
String getFile(String file) 
{
   StringBuffer buf=new StringBuffer();String str;
  try
  {
   BufferedReader in = new BufferedReader (new FileReader (file));
   while((str=in.readLine())!=null)
   {
      buf.append(str+"\r\n");
   }
   in.close();
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return (buf.toString());
}
//___________________________________________________________________________
String getUrl(String name)
{
   StringBuffer buf = new StringBuffer();
  try
  {
   String line;
   URL url = new URL(name);
   InputStream is = url.openStream();
   BufferedReader br = new BufferedReader(new InputStreamReader(is));
   while ((line = br.readLine()) != null) 
   {
      buf.append(line + "\r\n");
   }
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return(buf.toString());
}
//___________________________________________________________________________
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);
  }
}
//___________________________________________________________________________
int test(String sentence)
{
   int cnt = 0;
   if (test1(sentence)) {counteach[1]++; cnt++;}
   if (test2(sentence)) {counteach[2]++; cnt++;}
   if (test3(sentence)) {counteach[3]++; cnt++;}
   if (test4(sentence)) {counteach[4]++; cnt++;}
   if (test5(sentence)) {counteach[5]++; cnt++;}
   if (test6(sentence)) {counteach[6]++; cnt++;}
   if (test7(sentence)) {counteach[7]++; cnt++;}
   if (test8(sentence)) {counteach[8]++; cnt++;}
   if (test9(sentence)) {counteach[9]++; cnt++;}
   if (test10(sentence)) {counteach[10]++; cnt++;}
   if (test11(sentence)) {counteach[11]++; cnt++;}
   if (test12(sentence)) {counteach[12]++; cnt++;}
   return(cnt);
}
//___________________________________________________________________________
boolean test1(String sentence)
{
   if (islikedigit(sentence.length()) > 0) return(true);
   return(false);
}
//___________________________________________________________________________
boolean test2(String sentence)
{
   String[] words = sentence.split(" ");
   String word3 = words[2 % words.length];
   String word6 = words[5 % words.length];
   String word10 = words[9 % words.length];
   int len3 = word3.length();
   int len6 = word6.length(); 
   int len10 = word10.length();
   int len = len3 + len6 + len10;
   if (islikedigit(len) > 0) return(true);
   return(false);
}
//___________________________________________________________________________
boolean test3(String sentence)
{
   if (islikedigit(place(sentence)) > 0) return(true);
   return(false);
}
//___________________________________________________________________________
boolean test4(String sentence)
{
   String[] words = sentence.split(" ");
   if ((islikedigit(place(words[2])) > 0) & (islikedigit(place(words[9])) > 0))
   {
      return(true);
   }
   return(false);
}
//___________________________________________________________________________
boolean test5(String sentence)
{
   String[] words = sentence.split(" ");
   for (int i = 0; i < words.length; i++)
   {
      if (islikedigit(words[i].length()) > 0)
      {
         if (islikedigit(place(words[i])) > 0) return(true);
      }
   }
   return(false);
}
//___________________________________________________________________________
boolean test6(String sentence)
{
   String[] words = sentence.split(" ");
   int val2 = place(words[2]);
   int val5 = place(words[5]);
   int val9 = place(words[9]);
   String str2 = Integer.toString(val2);
   String str5 = Integer.toString(val5);
   String str9 = Integer.toString(val9);
   String str = str2 + str5 + str9;
   str = reversestr(str);
   if (islikedigit(Integer.parseInt(str)) > 0) return(true);
   return(false);
}
//___________________________________________________________________________
boolean test7(String sentence)
{
   String[] words = sentence.split(" ");
   int val2 = place(words[2]);
   int val5 = place(words[5]);
   int val9 = place(words[9]);
   String str2 = Integer.toString(val2);
   String str5 = Integer.toString(val5);
   String str9 = Integer.toString(val9);
   String str = str2 + str5 + str9;
   str = reversestr(str);
   if (islikedigit(Integer.parseInt(str)) > 3) return(true);
   return(false);
}
//___________________________________________________________________________
boolean test8(String sentence)
{
   String[] words = sentence.split(" ");
   StringBuffer buf = new StringBuffer();
   for(int i = 0; i < words.length; i++)
   {
      int pla = place(words[i]);
      String str = Integer.toString(pla);
      buf.append(str);
   }
   int val = digitsstr(buf.toString());
   if (islikedigit(val) > 0) return(true);
   return(false);
}
//___________________________________________________________________________
boolean test9(String sentence)
{
   String[] words = sentence.split(" ");
   if (islikedigit(words.length) > 0) return(true);
   return(false);
}
//___________________________________________________________________________
boolean test10(String sentence)
{
   String[] words = sentence.split(" ");
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < words.length; i++)
   {
      String s = words[i].substring(0,1);
      buf.append(s);
   }
   int val = place(buf.toString());
   if (islikedigit(val) > 0) return(true);
   return(false);
}
//___________________________________________________________________________
boolean test11(String sentence)
{
   String[] words = sentence.split(" ");
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < words.length; i++)
   {
      int n = words[i].length();
      String s = words[i].substring(n-1,n);
      buf.append(s);
   }
   int val = place(buf.toString());
   if (islikedigit(val) > 0) return(true);
   return(false);
}
//___________________________________________________________________________
boolean test12(String sentence)
{
   String[] words = sentence.split(" ");
   for (int i = 1; i < words.length - 1; i++)
   {
      StringBuffer buf1 = new StringBuffer();
      StringBuffer buf2 = new StringBuffer();
      for (int j = 0; j < i; j++)
      {
         buf1.append(words[j] + " ");
      }
      for (int j = i; j < words.length; j++)
      {
         buf2.append(words[j] + " ");
      }
      int val1 = place(buf1.toString());
      int val2 = place(buf2.toString());
      int div = (int)(val1 / val2);
      if (islikedigit(div) > 0) return(true);
      div = (int)(val2 / val1);
      if (islikedigit(div) > 0) return(true);
   }
   return(false);
}
//___________________________________________________________________________
int islikedigit(int val)
{
   int cnt = 0;
   String str = Integer.toString(val);
   if (likedigit(val)) cnt++;
   if (likedigit(val*val)) cnt++;
   val = val % 1000000;
   if (val > 1000000 - str.length()) val = 1000000 - str.length();
   int pi1 = Integer.parseInt(pi.substring(val,val + str.length()));
   int e1 = Integer.parseInt(e.substring(val,val + str.length()));
   int phi1 = Integer.parseInt(phi.substring(val,val + str.length()));
   if (likedigit(pi1)) cnt++;
   if (likedigit(e1)) cnt++;
   if (likedigit(phi1)) cnt++;
   int pi2 = pi.indexOf(str);
   int e2 = e.indexOf(str);
   int phi2 = phi.indexOf(str);
   if (pi2 >= 0) if (likedigit(pi2)) cnt++;
   if (e2 >= 0) if (likedigit(e2)) cnt++;
   if (phi2 >= 0) if (likedigit(phi2)) cnt++;
   return(cnt);
}
//___________________________________________________________________________
boolean likedigit(int val)
{
   int digit1 = digit;
   if (digit1 == 0) digit1 = 1;
   if (val < 0)
   {
      val = 0 - val;
   }
   String str = Integer.toString(val);
   if (digits(digits(primes[val % 1000])) == digit) return(true);
   if (digits(val) == digit) return(true);
   if (digits(digits(val)) == digit) return(true); 
   if ((str.length() % digit1) == 0) return(true);
   boolean ok = true;
   String sdigit = Integer.toString(digit);
   for (int i = 0; i < sdigit.length(); i++)
   {
      String s = sdigit.substring(i,i+1);
      if (!s.equals(sdigit)) ok = false;
   }
   if (ok) return(true);
   String[] lcda = lcd(val).split(" ");
   String[] lcdb = lcd2(val).split(" ");
   if (lcda.length % digit == 0) return(true);
   if (lcdb.length % digit == 0) return(true);
   return(false);
}

//___________________________________________________________________________
int digitsstr(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(total);
}//___________________________________________________________________________
int digits(int number)
{
   String str = Integer.toString(number);
   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(total);
}
//___________________________________________________________________________
String lcd(int val)
{
   StringBuffer buf = new StringBuffer();
   int val2 = val;
   for (int i = 2; i <= val; i++)
   {
      String s = Integer.toString(i);
      for (int j = 0; j < 10; j++)
      {
         if (val2 % i == 0)
         {
            buf.append(s + " ");
            val2 /= i;
         } 
      }
   }
   return(buf.toString());
}
//___________________________________________________________________________
String lcd2(int val)
{
   StringBuffer buf = new StringBuffer();
   int val2 = val;
   for (int i = 2; i <= val; i++)
   {
      String s = Integer.toString(i);
      for (int j = 0; j < 10; j++)
      {
         if (val2 % i == 0)
         {
            if (j == 0) buf.append(s + " ");
            val2 /= i;
         } 
      }
   }
   return(buf.toString());
}
//___________________________________________________________________________
String reversestr(String str)
{
   StringBuffer buf = new StringBuffer();
   for (int i = str.length()-1; i >= 0; i--)
   {
      String s = str.substring(i,i+1);
      buf.append(s);
   }
   //int ans = Integer.parseInt(buf.toString());
   return(buf.toString());
}//___________________________________________________________________________
int reverse(int val)
{
   String str = Integer.toString(val);
   StringBuffer buf = new StringBuffer();
   for (int i = str.length()-1; i >= 0; i--)
   {
      String s = str.substring(i,i+1);
      buf.append(s);
   }
   int ans = Integer.parseInt(buf.toString());
   return(ans);
}
//__________________________________________________
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);
}
}//__________________________________________________________________________
