1.22

secje posted @ 2010年5月11日 11:10 in sicp , 794 阅读
(define (square x) (* x x))
(define (smallest-divisor n)
  (find-divisor n 2))
(define (find-divisor n test-divisor)
  (cond ((> (square test-divisor) n) n)
        ((divides? test-divisor n) test-divisor)
        (else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b)
  (= (remainder b a) 0))
(define (prime? n)
  (= n (smallest-divisor n)))
(define (find-prime n k)
  (cond ((even? n) (find-prime (+ n 1) k))
        ((= k 0) #t)
        ((prime? n) (find-prime (+ n 2) (- k 1)))
        ((not (prime? n)) (find-prime (+ n 2) k))))
(define (start-prime-test n start-time k)
  (if (find-prime n k)
      (- (runtime) start-time)))
(define (time-prime-test n k)
  (start-prime-test n (runtime) k))

(time-prime-test 100000000000 3)
;Value: 5.178

(time-prime-test 1000000000000 3)
;Value: 14.943000000000001

(time-prime-test 10000000000000 3)
;Value: 70.16499999999999

(time-prime-test 10000000000 3)
;Value: 1.9200000000000017

总体来说还是比较贴近\sqrt{10}

为了能在PLT下面运行

语言包改成了Swindle,调用了primitive called current-milliseconds

 

(define (runtime) (current-milliseconds))
(define (square x) (* x x))
(define (smallest-divisor n)
  (find-divisor n 2))
(define (find-divisor n test-divisor)
  (cond ((> (square test-divisor) n) n)
        ((divides? test-divisor n) test-divisor)
        (else (find-divisor n (+ test-divisor 1)))))
(define (divides? a b)
  (= (remainder b a) 0))
(define (prime? n)
  (= n (smallest-divisor n)))
(define (find-prime n k)
  (cond ((even? n) (find-prime (+ n 1) k))
        ((= k 0) #t)
        ((prime? n) (find-prime (+ n 2) (- k 1)))
        ((not (prime? n)) (find-prime (+ n 2) k))))
(define (start-prime-test n start-time k)
  (if (find-prime n k)
      (- (runtime) start-time)))
(define (time-prime-test n k)
  (start-prime-test n (runtime) k))

> (time-prime-test 1000 3)
1
> (time-prime-test 10000 3)
1
> (time-prime-test 100000 3)
3
> (time-prime-test 10000000000 3)
1411
> (time-prime-test 100000000000 3)
3727
> (time-prime-test 1000000000000 3)
11193

CBSE sa 1 Question P 说:
2022年9月21日 06:14

The Summative Assessment -1 Exams are known as Half Yearly (6 Months) Exams and it’s held also as Term-1 exams for all grade students. Every student can download the CBSE Class 5 SA-1 Sample Paper 2023 Pdf or Term-1 Question Bank that covers all topics chapter by chapter for all languages & subjects of the course. CBSE sa 1 Question Paper Class 5 The teaching staff of the school along with subject experts of the board have introduced the CBSE 5th Class SA-1 Sample Paper 2023 with suggested answer solutions for all format exams.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter