SimpleBlog

Index of all articles

Some Snippets of Java Script

I allways thought, I do not have to use Javascript. But now, when I have to deal with all these incompatibilities of web browsers and am using jQuery, which is great, I took a look on the basis of it. And what should I say? Javascript is a very nice and fucking fast language. The browser guys have done a very good job.

A Random Walk (Grafic on the fly)

See Random Walk on Wikipedia for what it is. You can push the button as many times you like.

Some Prime Numbers generated by your browser

The sieve function inside this snippet comes from: c2.com

   $('#prime_button').click( function () {
    var p_string, t = new Date();
    function sieve(max) {
      var D = [], primes = [];
      for (var q=2; q<max; q++) {
        if (D[q]) {
          for (var i=0; i<D[q].length; i++) {
             var p = D[q][i];
             if (D[p+q]) D[p+q].push(p);
             else D[p+q]=[p];
          }
          delete D[q];
        } else {
          primes.push(q);
          if (q*q<max) D[q*q]=[q];
        }
      }
      return primes;
     }
    p_string = sieve(1000000).join(', ');
    t = (new Date() - t) / 1000;
    $('#prime_output').append('<p>132 pages of prime numbers in ' + t + 
    ' second(s)! Not bad. And remarkable: How fast they are inserted into this page:</p>');
    $('#prime_output').append('<p>' + p_string + '</p>');
    $('#prime_button, #remove_me_too').remove();
   });

This is a one time button: