-
Notifications
You must be signed in to change notification settings - Fork 1
/
primetime.scm
165 lines (136 loc) · 5.96 KB
/
primetime.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
; vim: set expandtab:
(declare (uses recognizer
colors-256
banner))
(import
recognizer
colors-256
banner
(chicken foreign)
(chicken process signal)
(chicken process-context)
(chicken time)
ansi-escape-sequences
srfi-13
srfi-18
srfi-4
srfi-1)
;; DEBUGGING
;(set! current-seconds (lambda () 99131.0)) ;; Quadruple
;(set! current-seconds (lambda () 1412316039.0)) ;; TRIPLET
;(set! current-seconds (lambda () 1412360565.0)) ;; TWIN PRIMES
;(set! current-seconds (lambda () 1412360693.0)) ;; immediate prime, then 60 gap
;(set! current-seconds (lambda () 1415577600.0)) ;; Error: (subu32vector) out of range
;; Action to take upon process exit - show the cursor and reset the terminal's color
(define (cleanup signal)
(print (show-cursor) (set-text '(fg-white) ""))
(exit))
(set-signal-handler! signal/term cleanup)
(set-signal-handler! signal/int cleanup)
(set-signal-handler! signal/pipe cleanup)
(set-signal-handler! signal/quit cleanup)
;; make ready for the factor_time C function
(foreign-declare "void factor_time(unsigned, unsigned* facts, int len);")
(define factor-time (foreign-lambda void "factor_time"
unsigned-integer32
u32vector
integer))
(define *MAX-FACTORS* 32)
;; an array of unsigned ints to write prime factors into as a side-effect
(define u32factors (make-u32vector *MAX-FACTORS* 0))
;; either use the current epoch time or the command-line argument
(define (what-time?)
(if (and (not (null? (command-line-arguments)))
(> (string-length (car (command-line-arguments))) 9)
(string->number (car (command-line-arguments))))
(string->number (car (command-line-arguments)))
(current-seconds)))
;;; main code
; we don't need no stinkin' input
(close-input-port (current-input-port))
; Set up fancy colors and print the title banner
(print* (hide-cursor) (set-title "IT'S PRIME TIME!!!") (erase-display) (cursor-position))
(print* (banner (drop special-colors 5)))
(let ((start (time->seconds (current-time)))
(now (what-time?))
(prime-counter (make-prime-counter)))
(let loop ((x 1)
(now now)
(prev-prime 1000)
(c (drop special-colors (car (last keyframes)))))
(let-syntax ((doloop
(syntax-rules ()
((_ cc tt)
(begin
;(print "\n\tthe prime list was " (take prime-counter 4))
(circle-incr prime-counter)
;(print "\n\tthe prime list is now " (take prime-counter 4))
(print* "\n" (set-text256 (car cc) tt ))
(thread-sleep! (seconds->time (+ x start)))
(loop (+ 1 x) (+ 1 now) 0 cc)))
((_ cc tt pp)
(begin
;(print "\n\tthe prime list was " (take prime-counter 4))
(circle-incr prime-counter)
;(print "\n\tthe prime list is now " (take prime-counter 4))
(print* "\n" (set-text256 (car cc) tt ))
(thread-sleep! (seconds->time (+ x start)))
(loop (+ 1 x) (+ 1 now) pp cc))))))
; call the C function and put the list of factors into u32factors
(factor-time now u32factors *MAX-FACTORS*)
; the 1st element of u32factors is the count of prime factors
(let* ((n (u32vector-ref u32factors 0))
(prime? (= 1 n))
(now-str (substring (number->string now) 0 10)))
(if prime?
(begin
(advance-prime-count prime-counter)
;(print "\nrecognizing on " (take prime-counter 4)
;(recognizer prime-counter))
(case (recognizer prime-counter)
((quadruple)
(doloop
special-colors
(string-append now-str
": ** *** PRIME ******* QUADRUPLET! ************* *****************\r")))
((triplet)
(doloop
special-colors
(string-append now-str
": ** *** PRIME TRIPLET *********** ************* *****************\r")))
((octomus)
(doloop
special-colors
(string-append now-str
": ** *** ***** OCTOMUS ***PRIME***\r")))
((sexy)
(doloop
special-colors
(string-append now-str
": ** *** *SEXY PRIME**\r")))
((cousin)
(doloop
special-colors
(string-append now-str
": ** *** PRIME COUSIN* ***********\r")))
((twin)
(doloop
special-colors
(string-append now-str
": ** *** *TWIN PRIME** *********** *************\r")))
((combo-breaker)
(doloop
prime-colors
(string-append now-str
": ** CCC COMBO BREAKER *PRIME*GAP="
(number->string (- (list-ref prime-counter 2) 1)) "\r")))
(else
(doloop
prime-colors
(string-append now-str
": ** *** PRIME TIME***\r")))))
(let ((factors (subu32vector u32factors 1 (+ 1 n))))
(doloop
(cdr c)
(string-append now-str ": " (string-join (map number->string (u32vector->list factors))) "\r")
(+ 1 prev-prime))))))))