Created
September 23, 2011 09:50
-
-
Save rctay/1237054 to your computer and use it in GitHub Desktop.
xdiff's bogosqrt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 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