Skip to content

Instantly share code, notes, and snippets.

View wmjd's full-sized avatar

William Diebolt wmjd

View GitHub Profile
// Heron's method
function sqrt(n){
var close_enuf = 0.001
function iterate(guess){
console.log(guess);
var next = (guess + n/guess)/2;
return (Math.abs(guess-next) < close_enuf) ? next : iterate(next);
}
iterate(Math.random()*n);
}
@wmjd
wmjd / gist:78b4c53e2595a2852aadc9c9ddfe986f
Last active April 23, 2019 09:57
Randomize array with guaranteed unique in possibly linear time
//version that only works for arrays of nums in JS
var array = [0,1,2,3,4,5,6,7,8,9]
var r, i = array.length
console.log(array)
while (i --> 1) {
r = Math.floor(Math.random()*i);
if(array[r] !== array[i]){
array[r] = array[r] ^ array[i];
array[i] = array[r] ^ array[i];
array[r] = array[r] ^ array[i];
//see if sequence of nums is in increasing order, but seq is allowed to decrease once.
//some shared stuff first:
var arr = [0,1,2,3,5,4,5,7];
//currently these two functions are only used in v1 (v2-3 prints value of entire function)
const s = () => console.log("success");
const f = () => console.log("failure");
//v1 imperative style
function exeImperative(){
let dec = 0;
@wmjd
wmjd / .bash_profile
Last active February 21, 2019 18:27
export CLICOLOR=1
#export PS1='\e[32;42m[\@]\$\em['
export PS1='\[\e[30;93m[\@]\e[m\e[30;33m\w\e[m\e[30;93m \$\e[m\]'
export LSCOLORS="GxFxCxDxBxegedabagaced"