Skip to content

Instantly share code, notes, and snippets.

@samth
Created August 5, 2020 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samth/7cb4645308d8572e2250833ef7b90b7c to your computer and use it in GitHub Desktop.
Save samth/7cb4645308d8572e2250833ef7b90b7c to your computer and use it in GitHub Desktop.
#lang racket
(define (convert.1 n)
(define pling (divides? 3 n))
(define plang (divides? 5 n))
(define plong (divides? 7 n))
(if (or pling plang plong)
(string-append (if pling "Pling" "")
(if plang "Plang" "")
(if plong "Plong" ""))
(number->string n)))
; version 2
(define (convert.2 n)
(define table '((3 . "Pling") (5 . "Plang") (7 . "Plong")))
(define result (for/list ([(k v) (in-dict table)] #:when (divides? k n)) v))
(if (empty? result) (number->string n)
(string-append* result)))
(require math/number-theory)
(collect-garbage)
(collect-garbage)
(time
(for ([_ 10000])
(for ([i (in-list '(1 3 5 7 6 8 9 10 14 15 21 25 47 35 49 52 105 3125))])
(convert.1 i ))))
(collect-garbage)
(collect-garbage)
(time
(for ([_ 10000])
(for ([i (in-list '(1 3 5 7 6 8 9 10 14 15 21 25 47 35 49 52 105 3125))])
(convert.2 i ))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment