Find a String Naive Hill Climbing
copy const target = 'snippetzone' .split ``; const leng = target.length ; const alphabet = 'abcdefghijklmnopqrstuvwxyz' .split ``; function randomString( ) { let str = [ ] ; for ( let i = 0 ; i < leng; i++ ) { str.push ( randomChar( ) ) ; } return str; } function randomChar( ) { return alphabet[ Math .floor ( Math .random ( ) * alphabet.length ) ] ; } let iterations = 0 ; const search = randomString( ) ; const indices = [ ] ; for ( var i = 0 ; i < leng; i++ ) indices.push ( i) ; let found = false ; function loop( ) { for ( let i = 0 ; i < 10 ; i++ ) { if ( indices.length > 0 ) { let ii = Math .floor ( Math .random ( ) * indices.length ) ; let index = indices[ ii] ; search[ index] = randomChar( ) ; if ( search[ index] == target[ index] ) { indices.splice ( ii, 1 ) ; } console.log ( search.join ( ',' ) ) ; iterations++; } else { console.log ( 'found after' , iterations, 'iterations' ) ; found = true ; break ; } } if ( ! found) { requestAnimationFrame( loop) ; } } loop( ) ;
Try it out…
This is a port of an old snippet of mine. It naively and randomly finds a target string. In this case the string “snippetzone”.
It’s fun to see it randomly perform better/worse…
e,f,k,w,q,s,o,h,n,r,f
e,f,k,w,q,s,o,h,n,r,u
k,f,k,w,q,s,o,h,n,r,u
k,f,k,w,q,o,o,h,n,r,u
k,f,k,w,q,o,o,v,n,r,u
k,f,k,w,q,v,o,v,n,r,u
...
s,e,i,p,p,e,t,z,o,n,e
s,v,i,p,p,e,t,z,o,n,e
s,h,i,p,p,e,t,z,o,n,e
s,d,i,p,p,e,t,z,o,n,e
s,s,i,p,p,e,t,z,o,n,e
s,e,i,p,p,e,t,z,o,n,e
s,g,i,p,p,e,t,z,o,n,e
s,q,i,p,p,e,t,z,o,n,e
s,m,i,p,p,e,t,z,o,n,e
s,w,i,p,p,e,t,z,o,n,e
s,g,i,p,p,e,t,z,o,n,e
s,x,i,p,p,e,t,z,o,n,e
s,t,i,p,p,e,t,z,o,n,e
s,o,i,p,p,e,t,z,o,n,e
s,e,i,p,p,e,t,z,o,n,e
s,k,i,p,p,e,t,z,o,n,e
s,p,i,p,p,e,t,z,o,n,e
s,b,i,p,p,e,t,z,o,n,e
s,n,i,p,p,e,t,z,o,n,e
found after 176 iterations
Fast Sine and Cosine Approximation
copy let t = 0 , cos, sin, x, y; const PI = Math .PI ; const TWO_PI = PI * 2 ; const HALF_PI = PI / 2 ; const tA = 4 / PI; const tB = 4 / PI ** 2 ; const canvas = document.body .appendChild ( document.createElement ( 'canvas' ) ) ; const c = canvas.getContext ( '2d' ) ; canvas.width = canvas.height = 300 ; function loop( ) { // low precision sine/cosine // always wrap input angle to -PI..PI t += 0.1 ; if ( t < - PI) { t += TWO_PI; } else if ( t > PI) { t -= TWO_PI; } // compute sine if ( t < 0 ) { sin = tA * t + tB * t * t; } else { sin = tA * t - tB * t * t; } // compute cosine: sin(t + PI/2) = cos(t) t += HALF_PI; if ( t > PI) { t -= TWO_PI; } if ( t < 0 ) { cos = tA * t + tB * t * t; } else { cos = tA * t - tB * t * t; } t -= HALF_PI; // move the shape x = 110 + 100 * cos; y = 110 + 100 * sin; c.fillStyle = 'rgba(100, 100, 20, .5)' ; c.fillRect ( x, y, 10 , 10 ) ; window.requestAnimationFrame ( loop) ; } loop( ) ;
Try it out…
This is an old trick for fast sine/cosine approximation. I learned about it from Michael Baczynski’s blog which now seems to be down. Here is a wayback link for the original article and another one to a more in-depth discussion about it:
original post
original thread
It’s unlikely that the speed gain (if there even is one here) makes sense in javascript. This is really more for fun… a quick search will turn up all kinds of cool info about fast sine/cosine stuff.
isPowerOfTwo
copy function isPowerOfTwo( value) { return ( value & ( value - 1 ) ) === 0 && value !== 0 ; } for ( let i = 0 ; i < 66 ; i++ ) { console.log ( i, isPowerOfTwo( i) ) ; }
Try it out…
I’ve had the pleasure of using THREE.js quite a bit over the years. The MathUtils file has some great utility functions. This version of isPowerOfTwo
comes straight from there. I may post a few more from there in the future…
Obfuscated Pre
copy b = document.body with( b.style ) fontFamily = 'monospace' , fontSize = '2em' , transform = 'skew(10deg) translateX(40px)' N = '<br>' ; ( f= ( _= '*' ) => ( b.innerHTML += ` < b style= 'opacity:${Math.random()+.2}' > ${ _} </ b> `, f) ) ( '<pre>' ) ( ) ( ) ( ) ( ) ( N) ( '(' ) ( '0' ) ( '_' ) ( '_' ) ( N) ( '-' ) ( ) ( ) ( '-' ) ( N) ( '_' ) ( '_' ) ( '0' ) ( ')' ) ( N) ( '-' ) ( ) ( ) ( '-' ) ( N) ( '(' ) ( '0' ) ( '_' ) ( '_' ) ( N) ( '-' ) ( ) ( ) ( '-' ) ( N) ( '_' ) ( '_' ) ( '0' ) ( ')' ) ( N) ( '-' ) ( ) ( ) ( '-' ) ( N) ( '(' ) ( '0' ) ( '_' ) ( '_' ) ( N) ( ) ( ) ( ) ( )
Try it out…
Not really sure what this is… just playing around…