Changeset 3557 for trunk/j/src/org/armedbear/lisp/numbers.lisp
 Timestamp:
 09/02/03 17:50:32 (19 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/j/src/org/armedbear/lisp/numbers.lisp
r3552 r3557 2 2 ;;; 3 3 ;;; Copyright (C) 2003 Peter Graves 4 ;;; $Id: numbers.lisp,v 1. 8 20030902 16:15:20piso Exp $4 ;;; $Id: numbers.lisp,v 1.9 20030902 17:49:54 piso Exp $ 5 5 ;;; 6 6 ;;; This program is free software; you can redistribute it and/or … … 163 163 rational 164 164 rationalize))) 165 166 167 168 ;;; From discussion on comp.lang.lisp and Akira Kurihara. 169 (defun isqrt (n) 170 "Returns the root of the nearest integer less than n which is a perfect 171 square." 172 (declare (type unsignedbyte n) (values unsignedbyte)) 173 ;; theoretically (> n 7) ,i.e., nlenquarter > 0 174 (if (and (fixnump n) (<= n 24)) 175 (cond ((> n 15) 4) 176 ((> n 8) 3) 177 ((> n 3) 2) 178 ((> n 0) 1) 179 (t 0)) 180 (let* ((nlenquarter (ash (integerlength n) 2)) 181 (nhalf (ash n ( (ash nlenquarter 1)))) 182 (nhalfisqrt (isqrt nhalf)) 183 (initvalue (ash (1+ nhalfisqrt) nlenquarter))) 184 (loop 185 (let ((iteratedvalue 186 (ash (+ initvalue (truncate n initvalue)) 1))) 187 (unless (< iteratedvalue initvalue) 188 (return initvalue)) 189 (setq initvalue iteratedvalue))))))
Note: See TracChangeset
for help on using the changeset viewer.