Home

pi.php (3.55kB)


<?php

if (!$_REQUEST['len']) {

     require($_SERVER['DOCUMENT_ROOT']."/ui/engine.php");

     $GLOBALS['pgtitle'] = "Pi Generator";

     $GLOBALS['subtitle'] = "<a href=\"viewsrc.php?filename=pi.php\">View Source</a>";

     $GLOBALS['js'] = "

           function createRequestObject() {  //Shamelessly copied and modified from http://mikeoncode.blogspot.com/2006/02/ajax-project-to-get-you-going.html

                 var ro;

                 var browser = navigator.appName;

                 if(browser == \"Microsoft Internet Explorer\"){

                       ro = new ActiveXObject(\"Microsoft.XMLHTTP\");

                 }

                 else{

                       ro = new XMLHttpRequest();

                 }

                 return ro;

           }

                 

           var http = createRequestObject();

                 

           function sndReq() {

                 piurl = 'pi.php?len='+document.getElementById('len').value+'&init='+document.getElementById('init').value+'&dpg='+document.getElementById('dpg').value+'&gpl='+document.getElementById('gpl').value;

                 document.getElementById('output').innerHTML = '<img src=\"working.gif\" alt=\"Working, please wait...\" />';

                 http.open('GET', piurl);

                 http.onreadystatechange = handleResponse;

                 http.send(null);

           }

                 

           function handleResponse() {

                 if(http.readyState == 4){

                       var response = http.responseText;

                       document.getElementById('output').innerHTML = '<p style=\"text-align: center\"><a href=\"'+piurl+'\">Download!</a></p><pre>'+response+'</pre>';

                 }

           }";

     printheader();

     ds("About", "This is a web-based, PHP version of <a href=\"http://jwcxz.com/projects/pi\">pi.py</a>, a simple utility to print out the digits of &pi; with digit grouping.  If the text overflows, click the <em>Download!</em> link to view the text completely.  I have written a <a href=\"http://blog.jwcxz.com?p=102\">post on my blog</a> about this online tool.  You can also <a href=\"viewsrc.php?filename=pi.php\">view the PHP sourcecode</a> of this script.");

     

     ds("Configuration", "</p>

           <form action=\"javascript:sndReq()\" name=\"pigen\">

                 <p>First Digit (starting with 1): <input type=\"text\" name=\"init\" id=\"init\" value=\"1\"/></p>

                 <p>Length: <input type=\"text\" name=\"len\" id=\"len\" value=\"500\" /></p>

                 <p>Digits per Group: <input type=\"text\" name=\"dpg\" id=\"dpg\" value=\"10\" /></p>

                 <p>Groups per Line: <input type=\"text\" name=\"gpl\" id=\"gpl\" value=\"5\" /></p>

                 <p><input type=\"submit\" name=\"submit\" value=\"Generate!\" /></p>

           </form>");

     

     ds("Output", "<div style=\"width: 80%; background-color: #CCFFCC; text-align: center; padding: 30px; margin: 0px auto 0px\" id=\"output\">&nbsp;</div>");

     

     printfooter();

}

else {

     $len  = ( $_REQUEST['len']  && $_REQUEST['len']  <= 100000  ) ? $_REQUEST['len']            : 500;

     $init = ( $_REQUEST['init'] && $_REQUEST['init'] <  1000000 ) ? $_REQUEST['init'] - 1      : 0;

     $dpg  = ( $_REQUEST['dpg']  && $_REQUEST['dpg']  <  1000000 ) ? $_REQUEST['dpg']            : 10;

     $gpl  = ( $_REQUEST['gpl']  && $_REQUEST['gpl']<1000000/$dpg) ? $_REQUEST['gpl']            : 5;

     

     $f = fopen("pidb.txt", 'r');

     fseek($f, $init);

     $pistr = fread($f, $len);

     fclose($f);

     

     $n = $dpg;

     while ( $n < strlen($pistr) ) {

           $pistr = substr($pistr, 0, $n)."\n".substr($pistr, $n);

           $n += $dpg + 1;

     }

     $piarr = explode("\n", $pistr);

     

     $maxln = strlen((count($piarr)+1)/5);

     function mkln($n, $maxln) {

           return $n.str_repeat(" ",$maxln-strlen($n));

     }

     for ($i = 0; $i <= count($piarr) +1; $i++) {

           if     ( $i % $gpl == 0 and $i == count($piarr) )

                 $pi .= $piarr[$i-1];

           elseif ( $i % $gpl == 0 )

                 $pi .= $piarr[$i-1] . "\n" . mkln($i/$gpl+1, $maxln);

           else

                 $pi .= $piarr[$i-1] . " ";

     }

     header("Content-type: text/plain");

     echo $pi;

}