25
Fermats (Little) Theorem CSCI 2824, Fall 2014

Fermat s (Little) Theorem - University of Colorado Boulderl3d.cs.colorado.edu/~ctg/classes/struct14/lecslides/DiscStruc2014L... · Wilson’s Theorem! Suppose we’re dealing with

  • Upload
    trandat

  • View
    215

  • Download
    1

Embed Size (px)

Citation preview

Fermat’s (Little) Theorem!

CSCI 2824, Fall 2014!!!!

Modular Multiplication: Mod 6

0 1 2 3 4 5

0 0 0 0 0 0 0 1 0 1 2 3 4 5 2 0 2 4 0 2 4 3 0 3 0 3 0 3 4 0 4 2 0 4 2 5 0 5 4 3 2 1

Mod 7 Multiplication

0 1 2 3 4 5 6 0 0 0 0 0 0 0 0 1 0 1 2 3 4 5 6 2 0 2 4 6 1 3 5 3 0 3 6 2 5 1 4 4 0 4 1 5 2 6 3

5 0 5 3 1 6 4 2 6 0 6 5 4 3 2 1

Patterns…

•  There are 0’s in the interior of the 6 table, but not the 7 table. (Why?)

•  Each row of the 7’s table includes each of the values 0-6. (Why?)

•  In the 7’s table, the 1’s row counts up (naturally); the 6’s row “counts down” (why?)

For a PRIME modulus p

•  Every non-0 number has a multiplicative inverse: that is, for every a we can find a b such that a*b = 1.

•  Two numbers: 1 and -1 (or p-1) are their own inverses.

•  You can do division on this table!

Wilson’s Theorem Suppose we’re dealing with a prime modulus p.

Now consider one of those rows of the multiplicative table. It has pairs of inverses, except for the two numbers (1 and -1) that are their own inverses. So if we multiply all the values across the row: 1 * 2 * 3 * …. (p-1)

we’ll get a factor of 1 from each inverse pair, a factor of 1 from 1 and a factor of -1 from (p-1).

Wilson’s Theorem

•  So: (1 * 2 * 3 * … (p – 1)) mod p = (p-1)! mod p = (-1) mod p

Fermat’s Theorem

•  Something even cooler! Note that if we multiply every element in a row by, say, 2, we simply rearrange the numbers: 1, 2, 3, … p-1 In other words, the list: (2*1), (2*2), (2*3), … (2 * (p-1))

simply consists of (1, 2, 3, … p-1) in some new order!

Fermat’s Theorem So, think what that means:

(2*1) * (2*2) * (2*3) *… (2* (p-1)) = 1 * 2 * 3 * … (p-1)

In other words: 2p-1 (p-1)! = (p-1)!

Or, 2p-1 = 1 (mod p)

Fermat’s Theorem

In fact, for any value m between 1 and p-1, the very same argument shows that: mp-1 = 1 mod p

Try it!!

212 mod 13 = 4096 mod 13 = 1 312 mod 13 = 531441 mod 13 = 1 66 mod 7 = 46656 mod 7 = 1

So… This is a quick test for compositeness

•  It’s quick (unlike Wilson’s Theorem) because you can rapidly raise a number to a given power:

What’s 2 to the 1024 power? What’s 2 to the 82 power?

How to raise a number to a power quickly:

Write the power value in binary, and then use that binary value as a template for a program.

Suppose, for instance we want to raise 7 to the 82 power.

We write 82 as (1 0 1 0 0 1 0)

Now we do the following:

start with 1 1 mult 7, square 0 square 1 mult 7, square 0 square 0 square 1 mult 7, square 0 done!

The rule for the program mn

•  Write n as a binary number. •  Start at the high digit of n, and with a running

value of 1, and then repeat until done: •  If you see a 1, multiply m into your running

value. •  If you’re not at the final digit, square your

running value. If you are at the final digit, you’re done.

•  Go on to the next digit.

Fermat’s Theorem shows conclusively if a number is

composite, but not if it’s prime… •  If we do a Fermat test (say, raise 2 to the

n-1 power) and don’t get 1, we know that the number is composite.

•  On the other hand, if we do get 1, we know that… um… the number might well be prime.

Let’s take a break: Casting out 9’s

• You’ve probably seen this trick: to determine if a number is divisible by 9, add up its digits… Is 3401001 divisible by 9? Ia 2022101 divisible by 9?

A more general statement of this trick

Call “SumDigits(n)” the result you get when

you add all the digits of n. Then SumDigits(n) mod 9 = n mod 9

How come?

34526 3 * (9999 + 1) + 4 * (999+1) + 5 * (99+1) + 2 * (9+1) + 6

Put on your “mod 9 glasses”: 3*1 + 4*1 + 5*1 + 2*1 + 6

A less well-known trick: casting out 11’s

Call AlternateSumDigits(n) the result of adding the low digit of n, then subtracting the next (10s) digit, then adding the next (100s) digit, then subtracting the next (1000s) digit, and so forth.

ASD(n) mod 11 = n mod 11

Why does this work?

5412 mod 11 = 2 – 1 + 4 – 5 = 0 mod 11 Rewrite: 5*(1001 – 1) + 4*(99+1) + 1*(11-1) + 2*(1)

Put on your “mod 11 glasses”: 5*(-1) + 4*1 + 1*(-1) + 2*1

How to Shuffle Cards without Shuffling Cards

Suppose we have our original deck numbered 0, … 51:

0, 1, 2, 3, …. 51 Now split the deck into two perfect halves: 0, 1, 2… 25 26, 27, … 51

How to Shuffle Cards without Shuffling Cards

Now split the deck into two perfect halves: 0, 1, 2… 25 26, 27, … 51 Alternate the cards: 0, 26, 1, 27, 2, 28, … 25, 51 This is called an outshuffle: 0 and 51 don’t

move.

How to Shuffle Cards without Shuffling Cards

Alternate the cards: 0, 26, 1, 27, 2, 28, … 25, 51 This is called an outshuffle: 0 and 51 don’t

move. But note that we can express this shuffle as a

function for the first 51 cards: position’ = (2*position) mod 51

How to Shuffle Cards without Shuffling Cards

position’ = (2*position) mod 51 After 8 shuffles (recall that 255 = 5 * 51): Finalposition = (256 * position) mod 51 = (1 * position) mod 51