import java.io.*;
import java.math.*;
//_____________________________________________________________
public class pi73
{ 
//_____________________________________________________________
public static final void main(String[] args) throws Exception
{
   pi73 bnk = new pi73();
   bnk.start(args);
}
//___________________________________________________________________________
void start(String[] args)
{
   int val = 8;
   if (args.length > 0) val = Integer.parseInt(args[0]);
   byte[] file = getBytes("pi.lst");
   int[][] out2 = new int[val][100];
   for (int max = 0; max < out2.length; max++)
   {
      StringBuffer buf = new StringBuffer();
      for (int j = 0; j < (max+1) * 100; j++)
      {
         buf.append((char)file[j]);
         for (int i = 1; i < out2[0].length; i++)
         {
            BigInteger bg1 = new BigInteger(buf.toString());
            BigInteger bg2 = new BigInteger(Integer.toString(i));
            BigInteger bg3 = new BigInteger("0");
            BigInteger bg4 = bg1.mod(bg2);
            if (bg4.equals(bg3)) 
            {
               out2[max][i]++;
            }
         }
      } 
   }  
   StringBuffer buf2 = new StringBuffer();
   for (int maxs = 0; maxs < out2.length; maxs++)
   {
      int max = (maxs + 1) * 100;
      int maxperc = 0;
      int maxpos = 0;
      int perc73 = 0;
      for (int i = 1; i < out2[0].length; i++)
      {
         double perc = 100.0 * out2[maxs][i] / (max / i);
         int per = (int) perc;
         if (per > maxperc)
         {
            maxperc = per;
            maxpos = i;
         }
         if (i == 73) perc73 = per;
      }
      buf2.append("<table border='1'>\r\n");
      buf2.append("<tr><th colspan='4'>first 1 to " + Integer.toString(max) + " digits of PI</th></tr>\r\n");
      buf2.append("<tr><th>divide by</th><th>number evenly divisible by</th><th>should be</th><th>percent</th></tr>\r\n");
      for (int i = 1; i < out2[0].length; i++)
      {
         double perc = 100.0 * out2[maxs][i] / (max / i);
         int per = (int)perc;
         buf2.append("<tr><td>");
         if (maxpos == i) System.out.println(maxs + " " + i + " " + per);
         //else if (i == 73) System.out.println(maxs + " " + per);
         //else if (per >= perc73) System.out.println("\t" + maxs + " " + i + " " + per);
         if (maxpos == i) buf2.append("<font size='7' color='red'>");
         buf2.append(i);
         if (maxpos == i) buf2.append("</font>");
         buf2.append("</td><td>" + out2[maxs][i] + "</td><td>" + (max / i));
         buf2.append("</td><td>");
         if (maxpos == i) buf2.append("<font size='7' color='red'>");
         buf2.append(per);
         if (maxpos == i) buf2.append("</font>");
         buf2.append("</td></tr>\r\n");
      }
      buf2.append("</table><br><br><br>\r\n");
   }
   saveFile("pi73.htm",buf2.toString(),false);
}
//___________________________________________________________________________
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);
  }
}
//___________________________________________________________________________
public static 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());
}
//___________________________________________________________________________
public static byte[] getBytes(String name) 
{
   byte[] b = new byte[0];
  try
  {
   File file = new File(name);
   b = new byte[(int) file.length()];
   FileInputStream fileInputStream = new FileInputStream(file);
   fileInputStream.read(b);
  }
  catch (Exception e)
  {
   System.out.println(e);
  }
   return(b);
}
}//__________________________________________________________________________
