ModularArithmeticandtheCaesarCipher
YanHuang
Objectives
• Divisibility• PrimeandCompositeNumbers• FundamentalTheoremofArithmetic• ceiling, floor, /, mod• Caesarcipher
Divisibility
• Thesetofingtegers ℤ = … , −2, −1, 0, 1, 2, … .
• ) divides* if)+ = * forsome+ ∈ ℤ.
• Wewrite)|* todenote) divides*.Wesay) isadivisorof* and* isamultiple of).
Divisibility
Forall),*,. ∈ ℤ
) ), 1 ), )| 0.
Divisibility
Forall),*,. ∈ ℤ
0|) ifandonlyif) = 0.
Divisibility
Forall),*,. ∈ ℤ
) * ⇔ −) * ⇔ )| − *.
Divisibility
Forall),*,. ∈ ℤ
) * and ) . ⇒ )|(* + .).
Divisibility
Forall),*,. ∈ ℤ
) * and * . ⇒ )|..
Divisibility
Forall),* ∈ ℤ
) * and * ) ⇔ ) = ±*.
Primality
: isaprimeif: > 1 andhasnootherpositivedivisorbesides1 and:.
: isacompositeif: > 1 andisnotaprime.
TheListofPrimes
Fundamentaltheoremofarithmetic
Everynon-zerointeger: canbewrittenas
: = ±<=>? … <@
>A.
where<= < <C < ⋯ < <@ aredistinctprimesandE=, … , E@ arenon-negativeintegers.Moreover,theexpressionisunique.
DivisionwithRemainder
Let), * ∈ ℤ with * > 0.ThenthereexistuniqueF, G ∈ ℤ suchthat
) = F* + G and0 ≤ G < *.
Floors
Thefloor function,denotedby⌊⋅⌋,isafunctionfromrealnumbersℝ toℤ.Forevery M ∈ ℝ,⌊M⌋ isthegreatestintegerN ≤ M.
⌊M⌋ isuniquelydefinedforeveryM.
Ceilings
Theceilingfunction,denotedby⌈⋅⌉,isafunctionfromrealnumbersℝ toℤ.Forevery M ∈ ℝ,⌈M⌉ isthesmallestintegerN ≥ M.
⌈M⌉ isuniquelydefinedforeveryM.
Themod operator
Let), * ∈ ℤ with * > 0,) = F* + G and0 ≤ G < *.Wedefine
) mod * ≔ G
Themod operator(GeneralizedDefinition)
Let), * ∈ ℤ,wedefine
) mod * ≔ ) − *⌊)/*⌋
DayinaWeek
September1,2016isThursday.WhatdayisOct1,2016?
MessagesEncoding&Decoding
• Percharacter:
encodeC :: Char -> IntencodeC =
decodeC :: Int -> ChardecodeC =
MessagesEncoding&Decoding
• Percharacter:
encodeC :: Char -> IntencodeC ‘A’ = 0encodeC ‘B’ = 1...encodeC ‘Z’ = 25
decodeC :: Int -> ChardecodeC 0 = ‘A’decodeC 1 = ‘B’...decodeC 25 = ‘Z’
Verytediousandunscalable.Doyouhavebetterideas?
MessagesEncoding&Decoding
• Dealingwithmulti-charactermessages
encode :: [Char] -> [Int]encode m = map encodeC m
encode :: [Char] -> [Int]encode = map encodeC
Point-freeform
CaesarCipher(ShiftCipher)
Encryption:V = W + X mod 26
Decryption:W = V − X mod 26
TheencryptionkeyInputcharacter
ImplementingCaesarCipher
caesarC :: Int -> Int -> IntcaesarC k c = (c + k) mod 26
caesar :: Int -> [Int] -> [Int]caesar = map . caesarC
ImplementingCaesarCipher
caesarDC :: Int -> Int -> IntcaesarDC k c = (c - k) mod 26
caesar :: Int -> [Int] -> [Int]caesar = map . caesarDC