Created
December 7, 2014 21:26
-
-
Save RenaissanceBug/f57ceb6ec5eb7696b3d4 to your computer and use it in GitHub Desktop.
Generating multiplication tables for modular arithmetic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket | |
#| Generator for times-table images for modular arithmetic. | |
jmj (jjohnson@kirby.org), 12 Nov 2013 | |
Based on a discussion at the Monterey Bay Area Math Teachers' Circle. | |
|# | |
(require 2htdp/image) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Int[>1] -> Listof[Listof[Nat]] | |
;; Generate a list-of-lists representation of the times table for | |
;; multiplication modulo the given base. | |
(define (times-table base) | |
(for/list ([i (in-range 1 base)]) | |
(for/list ([j (in-range 1 base)]) | |
(remainder (* i j) base)))) | |
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | |
;; Visualization | |
(define SQ-SIZE 25) ; Size of squares in the times table | |
(define FONT-SIZE 14) ; Size of font for displaying numbers | |
;; Int[>1] -> Image | |
;; Same as times-table above, but rendered as an image, with all identities | |
;; highlighted in red. | |
(define (tt/img base) | |
(define legend | |
(build-list (sub1 base) | |
(compose (curryr product->text-square true) add1))) | |
(above/align 'right | |
(apply beside legend) | |
(beside/align 'bottom | |
(apply above legend) | |
(apply above | |
(for/list ([row (times-table base)]) | |
(apply beside | |
(map product->text-square | |
row))))))) | |
;; Nat [Bool] -> Image | |
;; produce an image of the given number, BG red if n = 1, otherwise white | |
;; OR...white on black if inverse? is true. | |
(define (product->text-square n [inverse? false]) | |
(define fg (if inverse? 'white 'black)) | |
(define bg | |
(cond [inverse? 'black] | |
[(= n 1) 'red] | |
[else 'white])) | |
(overlay (text (number->string n) FONT-SIZE fg) | |
(frame (square SQ-SIZE 'solid bg)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment