Skip to content

Instantly share code, notes, and snippets.

@rctay
Created September 23, 2011 09:50
Show Gist options
  • Save rctay/1237054 to your computer and use it in GitHub Desktop.
Save rctay/1237054 to your computer and use it in GitHub Desktop.
xdiff's bogosqrt
/* from libxdiff's xutils.c */
long xdl_bogosqrt(long n) {
long i;
/*
* Classical integer square root approximation using shifts.
*/
for (i = 1; n > 0; n >>= 2)
i <<= 1;
return i;
}
; uncomment if you're on repl.it
#(define (quotient a b)
(/ (- a (mod a b)) b))
#(define (arithmetic-shift n count)
(if (< count 0)
(quotient n (expt 2 (- count)))
(* n (expt 2 count))))
(define (<< a b) (arithmetic-shift a b))
(define (>> a b) (arithmetic-shift a (- b)))
(define (xdl_bogosqrt n)
(let loop ([i 1] [n n])
(if (<= n 0)
i
(loop (<< i 1) (>> n 2)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment