; code -------------------------------------------------------------------------------
#lang racket
(require racket/serialize)
(define pl (list))
(define start-time 0)
(define (set-start-time)
(set! start-time (current-milliseconds)))
(define (elapsed-time)
(/ (- (current-milliseconds) start-time) 3600000.0))
(define (set-initial-primes-file)
(define out (open-output-file "primes-list.txt" #:exists 'replace))
(define l (list 2 3 5 7))
(write (serialize l) out)
(close-output-port out))
(define (append-primes-file total-time)
(read-primes-file)
(set-start-time)
(displayln (length pl))
(define last-prime (last pl))
(define (test-next-number n)
(cond
((> (last pl) 10000000) void)
((even? n) (test-next-number (+ n 1)))
((> (elapsed-time) total-time) void)
(else
(define n-root (floor (sqrt n)))
(define result (check-list n n-root pl))
(when result (set! pl (append pl (list n))))
(test-next-number (+ n 1)))))
(define (check-list test test-root l)
(define check (car l))
(cond
((null? l) #t)
((> check test-root) #t)
((= (modulo test check) 0) #f)
(else (check-list test test-root (cdr l)))))
(test-next-number (+ last-prime 1))
(write-primes-file))
(define (write-primes-file)
(define out (open-output-file "primes-list.txt" #:exists 'replace))
(write (serialize pl) out)
(close-output-port out))
(define (read-primes-file)
(define in (open-input-file "primes-list.txt"))
(set! pl (deserialize (read in)))
(close-input-port in))
(define (list-factor n)
(define factors (list))
(define (test-factors n1 l)
(cond
((and (null? l) (null? factors))
(displayln "The procedure cannot factor this number.")
(displayln "Either it is a prime greater than 100,000,380,000,361,")
(displayln "or it is a composite containing factors, all of which are greater than 10,000,019."))
((and (null? l) (not (null? factors)))
(set! factors (append factors (list n1)))
(displayln "The procedure cannot fully factor this number.")
(displayln "The last factor is either a prime greater than 100,000,380,000,361,")
(displayln "or it is a composite containing sub-factors, all of which are greater than 10,000,019.")
factors)
(else
(define test (car l))
(define sr (floor (sqrt n1)))
(cond
((= (modulo n1 test) 0)
(set! factors (append factors (list test)))
(check-duplicates n1 test l))
((> test sr)
(set! factors (append factors (list n1)))
factors)
(else (test-factors n1 (cdr l)))))))
(define (check-duplicates n2 t l1)
(define n3 (/ n2 t))
(cond
((= n3 1) factors)
((= (modulo n3 t) 0)
(set! factors (append factors (list t)))
(check-duplicates n3 t l1))
(else (test-factors n3 (cdr l1)))))
(cond
((= n 0)
(list 0))
((= n 1)
(list 1))
(else (test-factors n pl))))
(define (time-read-primes-file)
(let ((t1 (current-milliseconds)))
(read-primes-file)
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
tt))))
(define (time-list-factor n)
(let ((t1 (current-milliseconds)))
(let ((factor-list (list-factor n)))
(let ((t2 (current-milliseconds)))
(let ((tt (/ (- t2 t1) 1000.0)))
(values factor-list tt))))))
; REPL interactions ------------------------------------------------------------------
Welcome to DrRacket, version 5.1 [3m].
Language: racket.
> (time-read-primes-file)
4.009
> (list-factor 0)
'(0)
> (list-factor 1)
'(1)
> (list-factor 2)
'(2)
> (list-factor 3)
'(3)
> (list-factor 4)
'(2 2)
> (list-factor 5)
'(5)
> (list-factor 6)
'(2 3)
> (list-factor 1024)
'(2 2 2 2 2 2 2 2 2 2)
> (time-list-factor 111111111)
'(3 3 37 333667)
0.001
> (time-list-factor 1111111111)
'(11 41 271 9091)
0.001
> (time-list-factor 11111111111)
'(21649 513239)
0.319
> (time-list-factor 111111111111)
'(3 7 11 13 37 101 9901)
0.001
> (time-list-factor 1111111111111)
'(53 79 265371653)
0.002
> (time-list-factor 11111111111111)
'(11 239 4649 909091)
0.001
> (time-list-factor 111111111111111)
'(3 31 37 41 271 2906161)
0.001
> (time-list-factor 1111111111111111)
'(11 17 73 101 137 5882353)
0.001
> (time-list-factor 11111111111111111)
'(2071723 5363222357)
24.243
> (time-list-factor 111111111111111111)
'(3 3 7 11 13 19 37 52579 333667)
0.536
> (time-list-factor 1111111111111111111)
The procedure cannot factor this number.
Either it is a prime greater than 100,000,380,000,361,
or it is a composite containing factors, all of which are greater than 10,000,019.
97.527
> (time-list-factor 11111111111111111111)
'(11 41 101 271 3541 9091 27961)
1.628
> (time-list-factor 111111111111111111111)
'(3 37 43 239 1933 4649 10838689)
1.636
> (time-list-factor 1111111111111111111111)
'(11 11 23 4093 8779 21649 513239)
0.21
> (time-list-factor 11111111111111111111111)
The procedure cannot factor this number.
Either it is a prime greater than 100,000,380,000,361,
or it is a composite containing factors, all of which are greater than 10,000,019.
107.971
> (time-list-factor 111111111111111111111111)
'(3 7 11 13 37 73 101 137 9901 99990001)
0.125
> (time-list-factor 1111111111111111111111111)
'(41 271 21401 25601 182521213001)
6.208
> (time-list-factor 11111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(11 53 79 859 280846283204599997)
91.478
> (time-list-factor 111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(3 3 3 37 757 333667 440334654777631)
87.828
> (time-list-factor 1111111111111111111111111111)
'(11 29 101 239 281 4649 909091 121499449)
10.889
> (time-list-factor 11111111111111111111111111111)
'(3191 16763 43037 62003 77843839397)
2.168
> (time-list-factor 111111111111111111111111111111)
'(3 7 11 13 31 37 41 211 241 271 2161 9091 2906161)
0.14
> (time-list-factor 1111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(2791 6943319 57336415063790604359)
94.458
> (time-list-factor 11111111111111111111111111111111)
'(11 17 73 101 137 353 449 641 1409 69857 5882353)
0.593
> (time-list-factor 111111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(3 37 67 21649 513239 1344628210313298373)
91.104
> (time-list-factor 1111111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(11 103 4013 2071723 117957818840753430733)
92.212
> (time-list-factor 11111111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(41 71 239 271 4649 123551 102598800232111471)
94.068
> (time-list-factor 111111111111111111111111111111111111)
'(3 3 7 11 13 19 37 101 9901 52579 333667 999999000001)
12.855
> (time-list-factor 1111111111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(2028119 547853016076034547830334961169)
88.655
> (time-list-factor 11111111111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(11 1010101010101010101010101010101010101)
93.319
> (time-list-factor 111111111111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(3 37 53 79 239073561261285168617387389778123)
94.863
> (time-list-factor 1111111111111111111111111111111111111111)
'(11 41 73 101 137 271 3541 9091 27961 1676321 5964848081)
15.662
> (time-list-factor 11111111111111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(83 1231 538987 201763709900322803748657942361)
94.13
> (time-list-factor 1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(11 41 101 251 271 3541 5051 9091 21401 25601 27961 60101 7019801 341233306557836423189042926585457900151074303303755301)
90.121
> (time-list-factor 11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111)
The procedure cannot fully factor this number.
The last factor is either a prime greater than 100,000,380,000,361,
or it is a composite containing sub-factors, all of which are greater than 10,000,019.
'(11
41
73
101
137
251
271
401
1201
1601
3541
5051
9091
21401
25601
27961
60101
1676321
7019801
263980647407951828155827620420447036038246324232161580581998828957153987896442603868348285938940685117797680817907519399405310559008181)
97.999
>
Bookmarks