7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7...

42
1 Day 7 Number Theory 1 Number Theory In elementary number theory, integers are studied without use of techniques from other mathematical fields. Questions of divisibility, use of the Euclidean algorithm to compute greatest common divisors, factorization of integers into prime numbers, i ti ti f f t b d investigation of perfect numbers and congruences belong here. Several important discoveries of this field are Fermat's little theorem, Euler's theorem, the Chinese remainder theorem and the law of quadratic reciprocity. The properties of multiplicative functions such as the Möbius function, Euler's φ function, integer sequences, factorials and Fibonacci numbers all also fall into this area. 2 Divides Let and be integers, 0 divides an integer with n d d d n q n dq ↔∃ = if divides we write | if does not divide we write | d n nd d n n d 3

Transcript of 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7...

Page 1: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

1

Day 7

Number Theory

1

Number TheoryIn elementary number theory, integers are studied without use of techniques from other mathematical fields. Questions of divisibility, use of the Euclidean algorithm to compute greatest common divisors, factorization of integers into prime numbers, i ti ti f f t b dinvestigation of perfect numbers and congruences belong here. Several important discoveries of this field are Fermat's little theorem, Euler's theorem, the Chinese remainder theorem and the law of quadratic reciprocity. The properties of multiplicative functions such as the Möbius function, Euler's φ function, integer sequences, factorials and Fibonacci numbers all also fall into this area.

2

Divides

Let and be integers, 0 divides an integer with

n d dd n q n dq

≠↔ ∃ =

if divides we write |

if does not divide we write |

d n n d

d n n d

3

Page 2: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

2

Theorem

( ) ( ) ( )m d n d m n d∧ → +| | |

( )

1 1

2 2

1 2 1 2

Proof: with with

then

d m q m q dd n q n q d

m n q d q d q q d

→∃ =→∃ =

+ = + = +

||

4

( ) ( ) ( )m d n d m n d∧ → −| | |

Theorem

( )

1 1

2 2

1 2 1 2

Proof: with with

then

d m q m q dd n q n q d

m n q d q d q q d

→∃ =→ ∃ =

− = − = −

||

5

( ) ( )m d n d mn d∧ →| | |

Theorem

6

( )

1 1

2 2

1 2 1 2

Proof: with with

then

d m q m q dd n q n q d

mn q dq d q q d d

→∃ =→∃ =

= =

||

Page 3: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

3

Review Modular Arithmetic

x mod n means “remainder after dividing x by n”

x ≡ y (mod n) means “x and y have the same remainder mod n”

In C++ x%n means x mod n

7

Prime Number

A prime number (or a prime) is a natural number that has exactly two (distinct) natural number divisors. It follows that these two divisors are 1 and the prime

Euclid

number itself. There exists an infinitude of prime numbers, as demonstrated by Euclid in about 300 B.C.

The fundamental theorem of arithmetic states that every positive integer larger than 1 can be written as a product of one or more primes in a unique way, i.e. unique except for the order. Primes are thus the "basic building blocks" of the natural numbers.

8

Prime Number

An integer p>1 is prime iff it is not the product of two integers greater than 1:

p > 1 ∧ ¬∃ a, b ∈ N: a > 1, b > 1, a b = p

The only positive factors of a prime p are 1 and p itself

Some primes: 2, 3, 5, 7, 11, 13, 17, ...

9

Page 4: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

4

Eratosthenes of CyreneA versatile scholar, Eratosthenes of Cyrene lived approximately 275-195 BC

He was the first to estimate accurately the diameter of the earth

Born in Cyrene (in modern day Libya)

276 BC - 194 BC

Born in Cyrene (in modern-day Libya), but worked and died in Alexandria, capital of Ptolemaic Egypt

Never married, he was reputedly known for his haughty character

10

Sieve of EratosthenesPrime Numbers

11

Euclid’s Proof Infinite Number of Primes

Assume there are a finite number n of primes, the largest being pn

C id th b th t i th d t f th lConsider the number that is the product of these, plus one: N = p1 ... pn + 1

By construction, N is not divisible by any of the pi’s

Hence it is either prime itself, or divisible by another prime greater than pn contradicting the assumption

12

Page 5: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

5

Prime Number Algorithm

The sieve of Atkin is a fast, modern algorithm for finding all prime numbers up to a specified integer

It is an optimized version of the ancient sieve ofIt is an optimized version of the ancient sieve of Eratosthenes: the Atkin sieve does some preliminary work and then marks off multiples of primes squared, rather than multiples of primes

13

Prime? - Test for a Divisor

is_prime(n){

for 2 to nd ⎢ ⎥= ⎣ ⎦( )

( )

if mod 0 return d return}

n d ==

14

TI Test if prime

15

Page 6: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

6

Prime Counting Function π(n)

( ) is the number of primes less than or equal to x xπ

( )lnnnn

π =

Counts the number of prime numbers less than or equal to some real number n

Gauss and Legendre approximated

Th th li t d f l t f

16

There are other more complicated formulas, most of which are beyond our course level, for example:

Where O is our “Big – O”

( ) ( )1

00 1

lim logln ln

k n

kk

dt dtn O n nt t

π−

→+

⎛ ⎞= + +⎜ ⎟

⎝ ⎠∫ ∫

Prime Counting Function

( ) is the number of primes less than or equal to x xπ ( )lnnnn

π =

17

Composite Number

A composite number is a positive integer which has a positive divisor other than one or itself

By definition, every integer greater than one is either a prime number or a composite number

The numbers zero and one are considered to be neither prime nor composite

For example, the integer 15 is a composite number because it can be factored as 3 · 5 and 315 can be written as 32 · 5 · 7

18

Page 7: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

7

TI - Prime Factorization

19

Prime Factorization Algorithm

A prime factorization algorithm is any algorithm by which an integer is "decomposed" into a product of factors that are prime numbers

The fundamental theorem of arithmetic guarantees that this decomposition is unique

20

Recursive Factoring AlgorithmGiven a number n

if n is prime, this is the factorization, so stop here

if n is composite, divide n by the first prime p1

If it divides cleanly, recurse with the value n/p1Add p1 to the list of factors obtained for n/p1 to get a factorization for n

If it does not divide cleanly, divide n by the next prime p2, and so on

We need to test only primes from 2 to n 21

Page 8: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

8

Least Common Multiple

The least common multiple or lowest common multiple (lcm) or smallest common multiple of two integers a and b is the smallest positive integer that is a multiple of both a and b

If there is no such positive integer, e.g., if a = 0 or b = 0, then lcm(a, b) is defined to be zero

22

TI Example

23

Greatest Common Divisor

The greatest common divisor (gcd), sometimes known as the greatest common factor (gcf) or highest common factor (hcf), of two non-zero integers, is the largest positive integer that divides both numbers without remainder

24

Page 9: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

9

GCD pseudocode

gcd(n,m)Func

If n<m thenswap(n,m)

endifhil 0

swap(n,m)Func

n tm n

25

while m≠0n mod m rm nr m

endwhilereturn a

end

m nt m

end

GCD pseudocode

gcd(n,m)Func

Recursive Function

26

FuncIf m=0 then

return nelse

return(gcd(m, n mod m))endif

end

TI Example

27

Page 10: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

10

Theorem

( ) ( ), gcd ,lcm a b a b a b⋅ = ⋅

Proof:Suppose factors of and area b

1 2 1 2

1 2 1 2

1 2

1 2 1 2

where are the common factorsand and are the factors unique to and

n n

n n

n

n n

a p p p r r rb q q q r r r

r r rp p p q q q

a b

= ⋅ ⋅ ⋅ ⋅= ⋅ ⋅ ⋅

⋅⋅ ⋅

28

( )( )

1 2 1 2 1 2

1 2

Then , =

and gcd ,n n n

n

lcm a b p p p q q q r r r

a b r r r

⋅ ⋅ ⋅ ⋅ ⋅

= ⋅

Theorem

( ) ( ), gcd ,lcm a b a b a b⋅ = ⋅

( )

( ) ( )( ) ( )

( ) ( )

1 2

1 2 1 2 1 2 1 2

1 2 1 2 1 2 1 2

d gcd ,Then

, gcd ,

n

n n n n

n n n n

a bp p p r r r q q q r r r

p p p q q q r r r r r r

lcm a b a b

= ⋅ ⋅ ⋅ ⋅ ⋅ ⋅

= ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅

= ⋅

i

29

Relatively Prime

and are relatively prime or coprime if gcd( , ) 1a b

a b =

A set of integers {a1 , a2 ,…} is (pairwise) relatively prime if all pairs (ai , aj ), for i ≠ j, are relatively prime

30

Page 11: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

11

Example

Neither 21 nor 10 is prime21=3·7 and 10=2·5

21 and 10

They have no common factors > 1They have no common factors > 1and their gcd = 1

They are coprime

31

TI Test if coprime

32

programmer humor

There are 10 types of people in the world

Those that understand binary numbersand those that do not.

Binary Watch33

Page 12: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

12

Sexagesimal Number System

The sexagesimal (base-sixty) is a numeral system with sixty as the base. It originated with the ancient Sumerians in the 2000s BC, and was transmitted to the Babylonians

Sexagesimal is seldom used for general computation or logic, but we retain some residual use of their system, in degrees of angle, and in the number of seconds in a minute, and minutes in an hour

34

Babylonian Symbols

35

Babylonian Multiplication Tablet

36table for 9

Page 13: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

13

Decimal System

Possibly originated in China

This tablet was discovered in 1898 in Xiao dun in the An-yang district of Henan province dating from the 14th century BC

37

Arabic Numerals

Various Arabic SystemsToday

38

Arabic NumeralsFibonacci, an Italian mathematician who had lived in North Africa, introduced the Arabic numeral system to Europe and promoted it with his book Liber Abaci, which was published in 1202

In the Muslim World until modern times the Arabic numeral system was used only by mathematicians

Muslim scientists used the Babylonian numeral system, and merchants used a numeral system similar to the Greek numeral system and the Hebrew numeral system

Therefore, it was not until Fibonacci that the Arabic numeral system was used by a large population

39

Page 14: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

14

Computer Number Systems

40

Binary Number System

The binary system was used in Egypt 3,000 - 2,050 BC

The ancient Indian mathematician Pingala िपङ्गलpresented the first known description of a binary numeral system in the 3rd century BC

The modern binary number system was fully documented by Gottfried Leibniz in the 17th century in his article Explication de l'Arithmétique Binaire. Leibniz's system used 0 and 1, like the modern binary numeral system

41

Eye of Horus

ancient Egyptian symbol of protection and royal power from deities, in this case from Horus or Ra

42

The Eye Of Horus defined an Egyptian Old Kingdom rounded off number

1 = 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/64, by discarding 1/64

Page 15: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

15

Binary Number System

The binary numeral system represents numeric values using two symbols, typically 0 and 1 in base (radix) 2

Due to its straightforward implementation in electronic circuitry, the binary system is used internally by virtually all modern computers

43

binary TI - 01 ?

TI-01

89 ?

44

TI Binary Number

45

Page 16: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

16

Binary Addition Examples

1011+ 10110000

1010+ 100

1110

1011+ 110010111

(a) (b) (c)

10011001+ 101100

11000101

101+ 1001

1110

(d) (e)

46

Binary Complement (1s Complement) Operation

1 0

0 1

47

1 1 0 0 1 0 1 1 0Example

0 0 1 1 0 1 0 0 1

Find binary complement

48

0 0 1 1 0 1 0 0 1

Page 17: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

17

Two’s ComplementThe Two’s complement of a binary number is obtained by first complementing the number and then adding 1 to the result

1001110

0110001+ 1

0110010

One’s Complement

Two’s Complement

49

Binary Subtraction

Binary subtraction is implemented by adding the Two’s complement of the number to be subtracted

Example

1101 1101

Two’s complement of 1001

1101 1101-1001 +0111

10100

If there is a carry then it is ignored

Thus, the answer is 0100

50

Example 101102Base 2 Base 10

101102 = 1 · 24 + 0 · 23 + 1 · 22 + 1 · 21 + 0 · 20

= 16 + 0 + 4 + 2 + 0 = 2210

51

Page 18: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

18

Binary Decimal Base 10

011.112 = 0 · 22 + 1 · 21 + 1 · 20 + 1 · 2-1 + 1 · 2-2

= 0 + 2 + 1 + ½ + ¼ = 3.7510

110 101 = 1 · 22 + 1 · 21 + 0 · 20 + 1 · 2-1110.1012 = 1 22 + 1 21 + 0 20 + 1 2 1

+ 0 · 2-2 + 1 · 2-3

= 4 +2 + 0 + ½ + 0 + ⅛ = 6.62510

52

Bits, Bytes, Nibbles

A bit (binary digit) refers to a digit in the binary numeral system, which consists of base 2 digits.

Bits can be represented in many forms.

A bit of storage is like a light switch; it can be either (1) ff (0) A i l bit i ton (1) or off (0). A single bit is a one or a zero, a true

or a false, a "flag" which is "on" or "off“.

A byte is a collection of bits, originally variable in size but now almost always eight bits. Eight-bit bytes, also known as octets, can represent 256 values (28 values, 0–255). A four-bit quantity is known as a nibble, and can represent 16 values (24 values, 0–15).

53

Quaternary BaseThis is a base-4 system using the symbols 0, 1, 2, and 3

North and Central American natives used this base to represent the four cardinal directions

Many or all of the Chumashanused a base 4 counting system

54

used a base 4 counting system

The Chumashan are Native Americans inhabiting central and southern coastal regions of California

Page 19: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

19

Quinary Number SystemQuinary (base-5) is a numeral system with five as the base. This originates from the five fingers on either hand: the most primitive numeral system

In the twentieth century, only the y yEast African Luo tribe of Kenya and the Yoruba of Nigeria were still using a base-five system

Women of Luo tribe drying fish55

Octal Number System

The octal numeral system is the base-8 number system, and uses the digits 0 to 7

The Yuki in California and the Pamean in Mexico have octalPamean in Mexico have octal systems because the speakers count using the spaces between their fingers rather than the fingers themselves

Yuki Tribe 1858 56

Binary to Octal

Octal numerals can be made from binary numerals by grouping consecutive digits into groups of three (starting from the right)

Example:

The binary representation for decimal 74 is 1001010, which groups into 1 001 010

so the octal representation is 112.

57

Page 20: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

20

Octal Decimal

6548 = 6 · 82 + 5 · 81 + 4 · 80

= 384 + 40 + 4 = 42810

2558 = 2 · 82 + 5 · 81 + 5 · 808= 128 + 40 + 5 = 17310

177.48 = 1·82 + 7 · 81 + 7 · 80 + 4 · 8-1

= 64 + 56 + 7 + ½= 127.510

58

Nonary BaseThe Nenets language once used a base-9 system (nonary), but has since shifted to decimal under the influence of Russian

The word yúq originally meant 9, but took the value 10 on account of Russian influence; so in current Nenets the word for 9 is xasu-yúq, lit. 'Nenets yúq', whereas 10 is simply yúq, but in Eastern dialects also lúca-yúq, lit. 'Russian yúq’

59

Duodecimal Number System The word duodecimal comes from the Latin words for two and ten

The Egyptians were fond of counting in base twelve

The base-12 number system composed of the digits 0, 1 2 3 4 5 6 7 8 9 A B1, 2, 3, 4, 5, 6, 7, 8, 9, A, B

Merchants use a duodecimal system when they count by the dozen or gross (12 dozen or 144)

Base-12 systems (duodecimal or dozenal) have been popular because multiplication and division are easier than in base-10, with addition just as easy

This base is responsible for our 12 and 24 hour clock60

Page 21: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

21

Duodecimal Number System Base-12 originated from the number of joints in the four fingers of a hand, using the thumb as a pointer

1

2

3

4

5

6 7

8

9

A

10B

61

A

A number of people in the Nigerian Middle Belt use a duodecimal system

Tridecimal BaseUses 13 different digits for representing numbers

Suitable digits for base 13 could be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, X, E, and T

According to Douglas Adams

62

g gand his Hitchhiker's Guide to the Galaxy, 42 is the ultimate answer to life, the universe and everything

With base 13, 6 x 9 = 42

Vigesimal Number System

The Maya civilization and other civilizations of Pre-Columbian Mesoamerica used base-20 (vigesimal), possibly originating from the number of a person's fingers and toes

Evidence of base 20 countingEvidence of base-20 countingsystems is also found in the languages of central and western Africa

63

Page 22: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

22

Dual Base Number System

Many ancient counting systems use 5 as a primary base, almost surely coming from the number of fingers on a person's hand

Often these system are supplemented with a secondary base, sometimes ten, sometimes twentysecondary base, sometimes ten, sometimes twenty

In some African languages the word for 5 is the same as "hand" or "fist" (Dyola language of Guinea-Bissau, Banda language of Central Africa) 64

Hexadecimal Number SystemBase 16, hexadecimal, or simply hex, is a numeral system with a radix or base of 16, usually written using the symbols 0–9 and A–F or a–f

The current hexadecimal system was first introduced to the computing world in 1963 by IBM. An earlier

i i th di it 0 9 d i t d dversion, using the digits 0–9 and u–z, was introduced in 1956 by the Bendix Corporation

Hexadecimal is primarily used in computing as the most common form of expressing a human-readable string representation of a byte. All 256 possible values of a byte can be represented using 2 digits in hexadecimal notation

65

TI Hexadecimal

66

Page 23: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

23

ASCII

ASCII (American Standard Code for Information Interchange) is a character encoding based on the English alphabet using one byte per symbol

Example:

A = 4116 a = 6116

B = 4216 b = 6216

67

ASCIITable

68

TI - Characters <--> ASCII

69

Page 24: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

24

Hexadecimal Decimal

71016 = 7 162 + 1 161 + 0 160

= 1792 + 16 + 0 = 180810

A21 = 10 162 + 2 161 + 1 160A2116 = 10 162 + 2 161 + 1 160= 2560 + 32 + 1 = 259310

FFF16 = 15 162 + 15 161 + 15 160

= 3840 + 240 + 15= 409510

70

Binary Octal

Just take 3 bits at a time from bottom

010 110 100 1112 6 4 7

= 26478

001 011 111 101 1101 3 7 5 6= 137568

71

Binary Hexadecimal

Take 4 bits at a time from bottom

0101 1010 01115 A 75 A 7

= 5A716

001 0111 1110 11101 7 E E= 17EE16

72

Page 25: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

25

TI Solution

73

Decimal Binary

Note: Order is from right to left0 3 6 131

2 1 2 3 2 6 2 13 2 261 0 1 01R R R R R` 1 0 1 01R R R R R= = = = =

10 226 11010=

74

TI Solution

75

Page 26: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

26

Decimal Octal

0 1928 2 8 19 8 155

R=2 R=3 R=3 ↓ ↓ ↓

10 8155 233=

76

TI-89 Decimal to Octal

To convert to octal using the TI first convert to binary, separate into groups of three, then convert the groups into octal.

10 011 0112 3 3

77

Base b to Decimal

base_b_to_dec ( , , ){ dec_val=0 power=1

f 1 t 0

c n b

iInput: , ,Output: dec val

c n b

i

for 1 to 0 dec_val = dec_val+c power power = power } return dec_val}

i

b

=i

i

Output: dec_val

78

Page 27: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

27

Decimal to Base b

( )

dec_to_base_b ( , , , ) { 1 While 0 {

1

m b c nn

mn n

= −

>

= +Input: ,Output:

m bc n 1

mod }}

n

n nc m bm m b

= +=

= ⎢ ⎥⎣ ⎦

Output: ,c n

79

Remarks

To convert from base a to base b

1)base a base 102)base 10 base b

We use both algorithms together

80

TI-89

Base Converter 1.2

"Base Converter" is a tool to convert any number (between 0 and 32767) from any base

baseconv.zip

81

to any other base (between 2 and 36)

This program and others similar can be found at www.ticalc.org

Page 28: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

28

Modular Exponentiation

Modular exponentiation is a type of exponentiation performed over a modulus. It is particularly useful in computer science, especially in the field of cryptology

Doing a "modular exponentiation" means calculating theDoing a modular exponentiation means calculating the remainder when dividing by a positive integer m (called the modulus) a positive integer b (called the base) raised to the eth power (e is called the exponent)

82

Modular Exponentiation

79 = 7 × 7 × 7 × 7 × 7 × 7 × 7 × 7 × 7 = 40,353,607

79 mod 10 = 40,353,607 mod 10 = 7

79 takes four multiplications, and the intermediate l k tti lvalues keep getting larger:

72 = 7 × 7 = 4974 = 72 × 72 = 49 × 49 = 240178 = 74 × 74 = 2401 × 2401 = 5,764,80179 = 78 × 71 = 5,764,801 × 7 = 40,353,607

83

79 mod 10 takes also four (mod) multiplications, but the values stay small

72 mod 10 = 49 mod 10 = 974 mod 10 = 81 mod 10 = 1

Modular Exponentiation

78 mod 10 = 1 mod 10 = 179 mod 10 = 78·7 mod 10 =1 · 7 mod 10 = 7

Useful property

(xa)b=(xb)a=xab, xa+b=xa · xb mod n

84

Page 29: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

29

Example 1620 mod 7

16 mod 7 = 2

162 mod 7 = 2 · 2 mod 7 = 4

164 mod 7 = 4 · 4 mod 7 = 2164 mod 7 = 4 · 4 mod 7 = 2

168 mod 7 = 2 · 2 mod 7 = 4

1616 mod 7 = 4 · 4 mod 7 = 2

1620 mod 7 = 1616 · 164 mod 7 = 2 · 2 mod 7 = 4

85

TI Solution

86

Exponentiation by SquaringExponentiation by squaring is an algorithm used for the fast computation of large integer powers of a number.

Recursive Formula

if 1x⎧

( ) ( )( )( )

2

2

if 1 Power , , 2 if even

if 2 odd, 1 2

x nx n Power x n n

nx Power x n

⎧ =⎪⎪= ⎨⎪ >−⎪⎩ i

14 7 6 3 23 9 9 9 9 81 9 81 81 4,782,969= = ⋅ = ⋅ = ⋅ ⋅ =87

Page 30: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

30

Extended Euclidean Algorithm

The extended Euclidean algorithm is an extension to the Euclidean algorithm for finding the greatest common divisor (GCD) of a and b: it also finds the integers x and y in Bézout's identity

The extended Euclidean algorithm is particularly useful when a and b are coprime, since x is the multiplicative inverse of a modulo b

( )gcd ,ax by a b+ =

88

Review Euclidean AlgorithmThe Euclidean algorithm is a way to find the gcd of two positive integers, a and b

Example gcd(210,45)

210/45 = 4 with remainder 30 so 210 = 4 · 45 + 30210/45 = 4 with remainder 30, so 210 = 4 · 45 + 30

45/30 = 1 with remainder 15, so 45 = 1 · 30 + 15

30/15 = 2 with remainder 0, so 30 = 2 · 15 + 0

gcd(210,45) = 15

89

TI Solution

90

Page 31: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

31

Review Euclidean Algorithm

Input : a, b Output: gcd

1 If a < b exchange a and b1. If a < b, exchange a and b. 2. Divide a by b and get the remainder, r If r = 0,

report b as the GCD of a and b. 3. Replace a by b and replace b by r. Return to the

previous step

91

Using the Extended Euclidean Algorithm

210/45 = 4 with remainder 30, so 210=4·45+30

Find gcd(210,45)

gcd(210, 45) 15 =

30 1·210 - 4·45=

45 / 30 1 with remainder 15, so 45 1·30 15= = +

15 45 1·30 45 1·(1·210 4·45) 1·210 5·45= − = − − = − +

92

Example ( )gcd 65, 40

65 / 40 1 with 25 25 65 1 40R= = → = − i40 / 25 1 with 15 15 40 1 25R= = → = − i

25 /15 1 with 10 10 25 1 15R= = → = − i15 /10 1 with 5 5 15 1 10R= = → = − i

10 / 5 2 with 0 5 10 2 5R= = → = − i

Therefore gcd(65, 40) 5=

93

Page 32: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

32

Same Example - Back Substitution

( )( )

5 15 10 15 25 15 2 15 25

2 40 25 25 2 40 3 25

= −

= − − = −

= − − = −

i

i i( )( ) 2 40 3 65 40 5 40 3 65= − − = −i i i i

( )gcd(65, 40) 3 65 5 40= − +i i

94

recursive pseudocode

function extended_gcd(a, b) {

if a mod b = 0

return {0, 1}

elseelse

temp := extended_gcd(b, a mod b)

x := first(temp)

y := last(temp)

return {y, x-y*(a div b)

} 95

Example GCD( 69974450 , 6898073 )

69974450 mod 6898073

106898073 69974450

68980730993720

= 993720

6898073 mod 993720 = 935753

993720 mod 935753 = 57967

935753 mod 57967 = 8281

57967 mod 8281 = 0

GCD( 69974450 , 6898073 ) = 828196

Page 33: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

33

Finding Inverses mod n

We want to find a -1 mod n

If gcd( a, n ) = 1, can use extended Euclidean to findx, y such that x · a + y · n = 1y y

Then x · a = 1 – y · n ≡ 1 mod n x ≡ a -1 mod n

Example: 5 · 3 – 2 · 7 = 1 5 ≡ 3-1 mod 7

97

TI Solution

modinv() is a function available at ticalc.org 98

Example

1 23 25 7 821 23 25mod82= ⋅ − ⋅= ⋅

Find the multiplicative inverse of 25 modulo 82

82 3 35 725 3 7 4

= ⋅ += ⋅ +

7 1 4 34 1 3 11 3

= ⋅ += ⋅ +=

23 is the inverse of 25mod82

99

Page 34: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

34

TI Solution

Example Find the multiplicative inverse of 25 modulo 82

100

Pseudo code - Inverse mod n

s = m, t = x; /* set up for gcd(x,m)

a = 0, b = 1; /* 0*x = s and 1*x = t

while(t) {

q = s/t r = s%t; /* quotient and remainderq = s/t, r = s%t; / quotient and remainder

s = t, t = r; /* push back

temp = (a-b*q) % m; a = b, b = temp; /* push back

}

return a

101

CryptologyCryptology is an term for cryptography and cryptanalysis

In cryptography, a cipher (or cypher) is an algorithm for performing encryption and decryption — a series f ll d fi d t th t bof well-defined steps that can be

followed as a procedure

Enigma

102

An alternative term is encipherment

Page 35: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

35

Cryptology

The original information is called plaintext

The encrypted form is called ciphertext

Encryption is the process of obscuring information to make it unreadable without special knowledgespecial knowledge, sometimes referred to as scrambling

103Lorenz cipher machine

EnigmaOutput

+ HRKM 13617 1807 -

AN HEERESGRUPPENKOMMANDO 2= 06 18 24 (FRX)

2109 -1750 - 3 TLE - FRX FRX - 1TL -172=

01 07 09 (In pencil; AGI)

HCALN UQKRQ AXPWT WUQTZ KFXZO MJFOY RHYZW VBXYS IWMMV WBLEB DMWUW BTVHM RFLKS DCCEX IYPAH RMPZI OVBBR VLNHZ UPOSY EIPWJ TUGYO SLAOX RHKVC HQOSV DTRBP DJEUK SBBXH TYGVH GFICA CVGUV

OQFAQ WBKXZ JSQJF ZPEVJ RO –2TL - 166 - 25 02 05 (In pencil; YBE)2TL 166 25 02 05 (In pencil; YBE)ZZWTV SYBDO YDTEC DMVWQ KWJPZ OCZJW XOFWP XWGAR KLRLX TOFCDSZHEV INQWI NRMBS QPTCK LKCQR MTYVG UQODM EIEUT VSQFI MWORPRPLHG XKMCM PASOM YRORP CVICA HUEAF BZNVR VZWXX MTWOE GIEBSZZQIU JAPGN FJXDK I -

3TL - 176 - 12 21 14 (In pencil; LUN)DHHAO FWQQM EIHBF BMHTT YFBHK YYXJK IXKDF RTSHB HLUEJ MFLACZRJDL CJZVK HFBYL GFSEW NRSGS KHLFW JKLLZ TFMWD QDQQV JUTJSVPRDE MUVPM BPBXX USOPG IVHFC ISGPY IYKST VQUIO CAVCW AKEQQEFRVM XSLQC FPFTF SPIIU ENLUW O =1 ABT GEN ST D H NR. 2050/38 G KDOS +

104

PlainText

Auf Befehl des Obersten Befehlshabers sind im Falle,

(z.Zt =) zur Zeit unwahrscheinlichen, Franzoesischen Angriffs

die Westbefestigungen jeder zahlenmaessigen Ueberlegenheit zum trotz zu halten. Fuehrung und Truppe muessen von dieser Ehrenpflicht durchdrungen sein.

Dem gemaess behalte ich mir die Ermaechtigung zurDem gemaess behalte ich mir die Ermaechtigung zur Aufgabe der Befestigungen oder auch von Teilen ausdruecklich persoenlich vor.

Aenderungen der Anweisung OKH/Gen/St/D/H Erste Abt Nr. 3321/38

G/KDos vom Juli 1938 bleibt vorbehalten.

Der Oberbefehlshaber des Heeres. 105

Page 36: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

36

TranslationThe Commander-in-Chief orders as follows:

In the case of French attacks on the western fortifications, although unlikely at this moment, those fortifications must be held at all costs, even against numerically superior forces.

Commanders and troops must be imbued with the honor of this duty.

In accordance with orders I emphasize that I alone haveIn accordance with orders, I emphasize that I alone have the right to authorize the fortifications to be abandoned in whole or part.

I reserve the right to make changes to the order OKH/Gen/St/D/H

1. Abt. Nr. 3321/38 GKDos of July 1938.

The Commander-in-Chief of the Army.106

RSA

In cryptology, RSA is an algorithm for public-key encryption

It was the first algorithm known to be suitable for signing as well as encryption, and one of the first great advances in public key cryptographyadvances in public key cryptography

RSA involves a public and private key

The public key can be known to everyone and is used for encrypting messages

Messages encrypted with the public key can only be decrypted using the private key

107

RSA

The keys for the RSA algorithm are generated using modulo math with two prime numbers

Keys are typically 1024–2048 bits long

Secret key cryptography, also known as symmetric t h i l t k f b thcryptography uses a single secret key for both

encryption and decryption

It was developed by Ron Rivest, Adi Shamir, and Leonard Adleman in 1977

108

Page 37: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

37

RSAAn analogy for public key encryption is that of a locked mailbox with a mail slot

The mail slot is exposed and accessible to the public; its location (the street address) is in essence the public keyy

Anyone knowing the street address can go to the door and drop a written message through the slot

However, only the person who possesses the key can open the mailbox and read the message

109

Rabin

The Rabin cryptosystem is an asymmetric cryptographic technique, whose security, like that of RSA, is related to the difficulty of factorization

Both RSA and Rabin use a trapdoor function, a function that is easy to compute in one direction yet believed tothat is easy to compute in one direction, yet believed to be difficult to compute in the opposite direction (finding its inverse) without special information, called the "trapdoor”

110

RSA

( )( )

RSA uses product of primes

public key , mod

private key , mod

e

d

n pq

e n C m n

d n M c n

=

=

=( )

RSA Chip

111

Page 38: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

38

RSA Key Generation

( ) ( )

We select two different primes, say 5, 7Then we compute the following

351 1 24

p q

n p qp qϕ

= =

= =

= − − =

ii

5 we select so and , are coprime5 = inverse of modulo

e e e n ed e

ϕϕ

= <=

( )( )

From thse we havethe public key ,

the private key ,

e n

d n112

RSA CodingWe write our message “Discrete”

We find the number for each letter (call it m)A B C D E F … 1 2 3 4 5 6 …

W h l k h d d

Letter D i s c r e t e4 9 19 3 18 5 20 5

mod 09 04 24 33 23 10 20 10e

mm n

We then look up the code me mod nFor example for “D” 45 mod 35 = 9

D 4 9

113

RSA DecodingOur message is then 0904243323102010Break up into 09 04 24 33 23 10 20 10Take the first number, call it 09m′ =

5We then calculate mod , so 9 mod35 4dm m n′= =

09 04 24 33 23 10 20 10mod 4 9 19 3 18 5 20 5

Letter D i s c r e t e

d

mm n

′′

Letter number 4 corresponds to letter DWe took 9 4 D→ →

114

Page 39: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

39

TI-89 RSA

rsa(p,q)FuncLocal s,n,e,d(p-1)*(q-1) sp*q»n

d( 1)

Input – two primenumbers

Output – private andbli krand(n-1) e

While gcd(e,s)≠1rand(n-1) e

EndWhilemodinv(e,s) dReturn {public,e,n,private,d,n}

EndFunc

115

public keys

Example Encode ”Silence is golden!”

Choose two prime numbers, say 7, 13p q= =

7 11 91n p q= = =i i

( ) ( )1 1 6 12 72p qϕ = − − = =i i

Choose and gcd( , ) 1, let 23e n e eϕ< = =1 mod 47d e ϕ−= =

( )( )

Public Key 23,91

Private Key 47,91116

TI Solution

117

Page 40: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

40

This time we will use ASCII codes for the characters

Letters S i l e n c e iASCII 83 105 108 101 110 99 101 32 105

Coding

ASCII 83 105 108 101 110 99 101 32 105

Letters s g o l d e n !ASCII 115 32 103 111 108 100 101 110 33

118

( )We apply the Public Key 23,91 mod

Letters S i l e n c e iASCII 83 105 108 101 110 99 101 32 105

34 14 75 82 24 57 82 37 14

em m n

m

′ =

Letters s g o l d e n !ASCII 115 32 103 111 108 100 101 110 33

19 37 38 41 75 24 82 24 80m′

Our Code is 341475822457823714193738417524822480

119

TI-89 Encryption Function

encrypt(s,e,n)FuncLocal wReturn seq(mod(ord(mid(s,w,1))^e,n),w,1,dim(s))EndFunc

This encrypts ASCII values for each character

120

Page 41: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

41

TI Result

121

TI-89 Decryption Function

decrypt(w,d,n)FuncReturn char(mod(w^d,n))EndFunc

This produces the characters, assuming letters are encrypted using ASCII values

It may be modified if we are using a different representation

122

TI Result

123

Page 42: 7 Number Theory.ppt - ottummath.comottummath.com/CS208/Files/Handouts/7 Number Theory.pdfDay 7 Number Theory 1 Number Theory In elementary number theory, integers are studied ... Least

42

Remarks

Even when we select the same pair of prime numbers our key is not unique

This happens since e is selected randomly

124

So the first numbers of each key may differ

But the second number of each key is the same

Example Create Public and Private Keys using prime numbers 13 and 19

Possible Solutions include:

Public Key Private Key179,247 35,247223 247 31 247

125

223,247 31,247155,247 131,247

61,247 85,247127,247 199,24747,247 23,247

RSA Software for TI-89

RSA Encryption and Decryption forTi-89, Ti-92+ and Voyage 200 rsa_project.zip at ticalc.org

126