|
#lang racket/base |
|
|
|
(require racket/format |
|
racket/string |
|
pict |
|
(only-in metapict save-pict) |
|
"liars.rkt" |
|
"viridis.rkt") |
|
|
|
;; Number of witnesses to test. |
|
(define MAX-WITNESS 100) |
|
|
|
;; Amount of precision to determine liar percent. |
|
(define ATTEMPTS 1000) |
|
|
|
;; How many columns in the square. |
|
(define COLS 10) |
|
|
|
;; Visualization parameters. |
|
(define MAIN-TEXT-SIZE 25) |
|
(define SUB-TEXT-SIZE 15) |
|
(define SIZE 100) |
|
(define COLOR inferno) |
|
(define MAX-SCALE 0.8) |
|
(define TEXT-STYLE (cons (inferno 1) "Coo Hew")) |
|
(define PADDING 5) |
|
|
|
(define TEXT? (make-parameter #f)) |
|
(define liars (rabin-miller-liars MAX-WITNESS ATTEMPTS)) |
|
(define liar-counts |
|
(for/hash ([(k v) (in-hash liars)]) |
|
(values k (length v)))) |
|
(define max-liar-count (apply max (hash-values liar-counts))) |
|
|
|
(define (square #:percent [pct 0] #:color [color #f]) |
|
(filled-rectangle SIZE SIZE |
|
#:draw-border? #f |
|
#:color (or color (COLOR pct)))) |
|
|
|
(define (square-for-liar n) |
|
(define liar-count (hash-ref liar-counts n 0)) |
|
(define %-lying (* (/ liar-count ATTEMPTS) 100)) |
|
(cc-superimpose |
|
(square #:percent (* MAX-SCALE (/ liar-count max-liar-count))) |
|
(rb-superimpose |
|
(cc-superimpose |
|
(blank (- SIZE (* 2 PADDING)) (- SIZE (* 2 PADDING))) |
|
(if (not (equal? (TEXT?) 'none)) |
|
(text (~a n) TEXT-STYLE MAIN-TEXT-SIZE) |
|
(blank))) |
|
(if (equal? (TEXT?) 'both) |
|
(text (~r %-lying #:precision 2) |
|
TEXT-STYLE |
|
SUB-TEXT-SIZE) |
|
(blank))))) |
|
|
|
(displayln |
|
(format "How often witnesses from 2 to ~a lie when testing the numbers from ~a to ~a for primality." |
|
MAX-WITNESS |
|
(+ MAX-WITNESS 1) |
|
(+ MAX-WITNESS 1 ATTEMPTS))) |
|
|
|
(define (make-pict) |
|
(for/fold ([acc (blank)]) |
|
([row-start (in-range 1 MAX-WITNESS COLS)]) |
|
(define row-pict |
|
(for/fold ([acc (blank)]) |
|
([col (in-range row-start (+ row-start COLS))]) |
|
(define sq |
|
(if (= col 1) |
|
(square #:color "black") |
|
(square-for-liar col))) |
|
(hc-append acc sq))) |
|
(vc-append acc row-pict))) |
|
|
|
(void |
|
(with-output-to-file (format "~a-~a.txt" MAX-WITNESS ATTEMPTS) |
|
#:exists 'replace |
|
(λ () |
|
(for ([k (in-range 2 (+ MAX-WITNESS 1))]) |
|
(define nums (sort (hash-ref liars k null) <)) |
|
(define nums-str (string-join (map number->string nums) ",")) |
|
(displayln (format "~a: ~a" k nums-str)))))) |
|
|
|
(void |
|
(for ([text?-val '(both main none)]) |
|
(parameterize ([TEXT? text?-val]) |
|
(define final-pict (make-pict)) |
|
(define name (format "~a-~a-~a" MAX-WITNESS ATTEMPTS (TEXT?))) |
|
(for ([fmt '(png svg pdf)]) |
|
(save-pict (format "~a.~a" name fmt) final-pict fmt))))) |