mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-19 05:39:18 +02:00
fixing bug in prime-above
This commit is contained in:
parent
3fc9c22245
commit
cfbd64f085
2 changed files with 34 additions and 8 deletions
|
@ -32,7 +32,19 @@
|
||||||
(test 1009 (nth-prime 168))
|
(test 1009 (nth-prime 168))
|
||||||
(test 1013 (nth-prime 169))
|
(test 1013 (nth-prime 169))
|
||||||
|
|
||||||
|
(test 2 (prime-above 1))
|
||||||
|
(test 3 (prime-above 2))
|
||||||
|
(test 5 (prime-above 3))
|
||||||
|
(test 5 (prime-above 4))
|
||||||
|
(test 7 (prime-above 5))
|
||||||
(test 907 (prime-above 888))
|
(test 907 (prime-above 888))
|
||||||
|
(test 911 (prime-above 907))
|
||||||
|
(test-not (prime-below 2))
|
||||||
|
(test 2 (prime-below 3))
|
||||||
|
(test 3 (prime-below 4))
|
||||||
|
(test 3 (prime-below 5))
|
||||||
|
(test 5 (prime-below 6))
|
||||||
|
(test 5 (prime-below 7))
|
||||||
(test 797 (prime-below 808))
|
(test 797 (prime-below 808))
|
||||||
|
|
||||||
(test 1 (totient 2))
|
(test 1 (totient 2))
|
||||||
|
|
|
@ -146,20 +146,34 @@
|
||||||
;;> Returns the first prime less than or equal to \var{n}, or #f if
|
;;> Returns the first prime less than or equal to \var{n}, or #f if
|
||||||
;;> there are no such primes.
|
;;> there are no such primes.
|
||||||
(define (prime-below n)
|
(define (prime-below n)
|
||||||
(and (>= n 3)
|
(cond
|
||||||
(let lp ((n (if (even? n) (- n 1) n)))
|
((> n 3)
|
||||||
(if (prime? n) n (lp (- n 2))))))
|
(let lp ((n (if (even? n) (- n 1) (- n 2))))
|
||||||
|
(if (prime? n) n (lp (- n 2)))))
|
||||||
|
((= n 3)
|
||||||
|
2)
|
||||||
|
(else
|
||||||
|
#f)))
|
||||||
|
|
||||||
;;> Returns the first prime greater than or equal to \var{n}. If the
|
;;> Returns the first prime greater than or equal to \var{n}. If the
|
||||||
;;> optional \var{limit} is given and not false, returns \scheme{#f}
|
;;> optional \var{limit} is given and not false, returns \scheme{#f}
|
||||||
;;> if no such primes exist below \var{limit}.
|
;;> if no such primes exist below \var{limit}.
|
||||||
(define (prime-above n . o)
|
(define (prime-above n . o)
|
||||||
(let ((limit (and (pair? o) (car o))))
|
(let ((limit (and (pair? o) (car o))))
|
||||||
(let lp ((n (if (even? n) (+ n 1) n)))
|
(cond
|
||||||
(cond
|
((< n 2)
|
||||||
((and limit (>= n limit)) #f)
|
2)
|
||||||
((prime? n) n)
|
(limit
|
||||||
(else (lp (+ n 2)))))))
|
(let lp ((n (if (even? n) (+ n 1) (+ n 2))))
|
||||||
|
(cond
|
||||||
|
((>= n limit) #f)
|
||||||
|
((prime? n) n)
|
||||||
|
(else (lp (+ n 2))))))
|
||||||
|
(else
|
||||||
|
(let lp ((n (if (even? n) (+ n 1) (+ n 2))))
|
||||||
|
(cond
|
||||||
|
((prime? n) n)
|
||||||
|
(else (lp (+ n 2)))))))))
|
||||||
|
|
||||||
;;> Returns the factorization of \var{n} as a monotonically
|
;;> Returns the factorization of \var{n} as a monotonically
|
||||||
;;> increasing list of primes.
|
;;> increasing list of primes.
|
||||||
|
|
Loading…
Add table
Reference in a new issue