import java.io.*;
import java.util.*;
import java.net.*;
import java.math.*;
//_____________________________________________________________
public class digits
{ 
   int all = 1000; // 100000000;
   String numk = "112011113322112141113101";
   int tota = 0;
   int totb = 0;
   int maxa = 0;
   int maxb = 0;
   int seed = 0;
   int tot37 = 0;
   int tot73 = 0;
   int tot777 = 0;
   StringBuffer buf = new StringBuffer();
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   digits bnk = new digits();
   bnk.start(args);
}
//_____________________________________________________________
digits() throws Exception 
{
}
//___________________________________________________________________________
void start(String[] args)
{
   long t1 = System.nanoTime();
   if (args.length > 0) seed = Integer.parseInt(args[0]);
   if (args.length > 1) all = Integer.parseInt(args[1]);
   tota = 0;
   totb = 0;
   maxa = 0;
   maxb = 0;
   tot37 = 0;
   tot73 = 0;
   tot777 = 0;
   startother(true);
   System.out.println(tota + " " + totb + " " + maxa + " " + maxb);
   buf.append("<table border='1'>\r\n");
   buf.append("<tr><th>up to</th><th>3,7</th><th>2,5</th>");
   buf.append("<th>max 3,7</th><th>max 2,5</th>");
   buf.append("</tr>\r\n");
   buf.append("<tr><td>std</td><td>" + tota + "</td>");
   buf.append("<td>" + totb + "</td>");
   buf.append("<td>" + maxa + "</td>");
   buf.append("<td>" + maxb + "</td>");
   buf.append("</tr>\r\n");
   tota = 0;
   totb = 0;
   maxa = 0;
   maxb = 0;
   tot37 = 0;
   tot73 = 0;
   tot777 = 0;
   System.out.println(tota + " " + totb + " " + maxa + " " + maxb);

   for (int j = 2; j <= 10; j++)
   {
      tota = 0;
      totb = 0;
      maxa = 0;
      maxb = 0;
      tot37 = 0;
      tot73 = 0;
      tot777 = 0;
      for (int i = 0; i < all; i++)
      {
         numk = getrandom(28,j);
         startother(false);
      }
      System.out.println((tota ) + " " + (totb ) + " " + maxa + " " + maxb);
      buf.append("<tr><td>" + (j-1) + "</td>");
      buf.append("<td>" + (tota / all) + "</td>");
      buf.append("<td>" + (totb / all) + "</td>");
      buf.append("<td>" + maxa + "</td>");
      buf.append("<td>" + maxb + "</td>");
      buf.append("</tr>\r\n");
   }
   buf.append("</table>\r\n");
   saveFile("digits.htm",buf.toString(),false);
   long t2 = System.nanoTime();
   System.out.println("seconds = " + ((t2-t1)/1e9));
}
//___________________________________________________________________________
void startother(boolean show)
{
   if (show) buf.append("---------------------------------------<br>\r\n");
   int cnt1 = startit(numk,"3", "7", show);
   int cnt2 = startit(reverse(numk),"3","7", show);
   int cnt3 = cnt1 + cnt2;
   int cnt4 = startit(numk,"2", "5", false);
   int cnt5 = startit(reverse(numk),"2","5", false);
   int cnt6 = cnt4 + cnt5;
   if (cnt3 > maxa) maxa = cnt3;
   if (cnt6 > maxb) maxb = cnt6;
   if ((tot37 == 0) | (tot73 == 0) | (tot777 == 0)) 
   {
      cnt3 = 0;
      cnt6 = 0;
   }
   if (cnt3 >= 3) tota++; // += cnt3;
   if (cnt6 >= 3) totb++; // += cnt6;
}
//___________________________________________________________________________
int startit(String num, String dig1, String dig2, boolean show)
{
   BigInteger bn1 = new BigInteger(num);
   BigInteger bn2 = new BigInteger (dig1);
   BigInteger bn3 = bn1.divide(bn2);
   String out = bn3.toString();
   if (out.indexOf("777") >= 0) tot777++;
   int cnt1 = find(dig1,dig2,out);
   int cnt2 = find(dig2,dig1,out);
   if (show) buf.append(out + " " + cnt1 + " " + cnt2 + "<br><br>\r\n");
   BigInteger bn4 = new BigInteger(dig2);
   BigInteger bn5 = bn1.divide(bn4);
   out = bn5.toString();
   if (out.indexOf("777") >= 0) tot777++;
   int cnt3 = find(dig1,dig2,out);
   int cnt4 = find(dig2,dig1,out);
   if (show) buf.append(out + " " + cnt3 + " " + cnt4 + "<br><br>\r\n");
   int cnt5 = cnt1 + cnt2 + cnt3 + cnt4;
   if (cnt1 + cnt3 >= 0) tot37++;
   if (cnt2 + cnt4 >= 0) tot73++;
   return(cnt5);
}
//___________________________________________________________________________
int find(String dig1, String dig2, String here)
{
   String lk = dig1 + dig2;
   int total = 0;
   for (int i = 0; i < here.length() - lk.length() + 1; i++)
   {
      String lk2 = here.substring(i,i+lk.length());
      if (lk.equals(lk2)) total++;
   }
   if (total >= 3) return(1);
   else return(0);
}
//___________________________________________________________________________
String reverse(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);
   }
   return(buf.toString());
}
//___________________________________________________________________________
String getrandom(int len, int max)
{
   Random rand = new Random(seed);
   StringBuffer buf = new StringBuffer();
   for (int i = 0; i < len; i++)
   {
      int rnd = rand.nextInt(max);
      buf.append(rnd);
   }
   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);
  }
}
}//__________________________________________________________________________
