On the numerical evaluation of factorials I

Introduction

This is the first post of what will be a very long series about calculating factorials and it is even a long way to the first calculation of a factorial.
At first let us build something for the actual calculations that is able to do it with arbitrary precision, do it fast enough and does not need any special software. A good compromise would be Javascript: it is build-in in every browser, rarely switched off if used locally, a lot of tutorials are
available for free and for money, and the author has a lot of that stuff already written. The last argument is the winning one, of course.
Oh, and: yes, this implementation will be with FFT!

All things will get tested in the last stable version of Firefox (currently 15.0.1).

So, to get a good start, we need something to type/paste our lines in, run them, and also something to show the result. A very basic thing is the following:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
          "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Scratchpad</title>
<!-- <script type="text/javascript" src="already_done.js"></script> -->
<script type="text/javascript">
// prints string "s" with a HTML-linebreak at the end added
function PRINT(s){
  document.getElementById("out").innerHTML += s+"
";
}
// prints string "s" without a HTML-linebreak at the end added
function PRINT1(s){
  document.getElementById("out").innerHTML += s;
}
// prints string "s" and breaks it after every character number "wrap"
function PRINT_WRAP(s,wrap){
  var ret = "";
  var a = "";
  var s = s.toString();
  var w   = wrap || 80;
  for(var i=s.length;i>=0;i=i-w){
    a = s.slice(0,w);
    s = s.substring(w);
    ret +=  a  + "
";
  }
  document.getElementById("out").innerHTML += ret + "
";
}
function runIt(){
  with(Math){
    eval(document.forms.testform.shell.value);
  }
}
></script></pre>
<h1>Scratchpad</h1>
<form id="testform" class="testform" action="" name="testform"><textarea name="shell" rows="20" cols="80"></textarea>

<button onclick="runIt()" name="abutton" type="button">
 <strong>Run It</strong>
</button>
<button style="margin-left: 3em;" onclick="document.getElementById(&quot;out&quot;).innerHTML = &quot;&quot;;" name="bbutton" type="button">
 <strong>Clear Output</strong>
</button><span id="inform" style="margin-left: 5%;"></span></form>
<pre>
<span class="result">Result:</span></pre>
<div id="out" class="out"></div>
</body>
</html>

If the size of the textarea is too small change the numbers in “cols” for the width and “rows” for the height to your delight.
If all works as promised the result should look like this screenshot.

The empty Scratchpad

The empty Scratchpad

Probably not exactly like that (different fonts, sizes, or whatever) but the structure should be the same: Headline saying “Scratchpad” on top, a field to type something in, two buttons in a row saying “Run It” and “Clear Output”, and a field for the result (collapsed to a black line if it contents
nothing) with the word “Result” printed on top, roughly centered.
So far, so good? Ok, but what if something runs wrong? Well, Firefox has an error-console, to be found under Tools→Web Developer→Error Console (Ctrl+Shift+J). Switch on at least “JS”, “Content” and “Errors”. The error caused by the missing letter T in the function name “PRINT” gives this picture.

Scratchpad with an error showing

Scratchpad with an error showing

Clicking on the field shows up a window with the source and the relevant
line highlighted.

Scratchpad with error and the sourcefile highlighted

Scratchpad with error and the sourcefile highlighted

As you can see: that is not very helpful here. It is more helpful if the error is in a script-environment or even better an external script.
Here I have put in an obscure error which can happen if the editor has a broken code highlighting or even none at all: a variable has the same name as a reserved identifier:

Scratchpad with error caused by reserved identifier

Scratchpad with error caused by reserved identifier

Clicking on the link in the error-console makes more sense now and gives:

Scratchpad with error caused by a reserved identifier shown in sourcefile

Scratchpad with error caused by a reserved identifier shown in sourcefile

Switching on “Warnings” in the error-console might give a lot of useful hints, too.

The external file named “already_done.js” must be in the same directory as the HTML-file. That external file is the place where we put all the things in, that are complete.

Next: a very basic but working big integer implementation

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s