<<

MATH36032 Problem Solving by Computer

Exploring numbers I There are infinitely many prime numbers I (Fermat’s Last Theorem:) There is not no non-trivial integer solutions to x n + y n = zn for n ≥ 3 I (Fermat’s Little Theorem:) if p is a prime, then for any number a co-prime to p, then ap−1 ≡ 1 (mod p).

I Famous (proved) theorems in number theory

I Special numbers: 2p − 1, Fermat number 2n Fn = 2 + 1, ··· I Important applications: Random number generation, cryptography (RSA public-key algorithm)

Integers and prime numbers

I Many problems are simple to state, yet difficult to prove or refute I Special numbers: Mersenne prime 2p − 1, Fermat number 2n Fn = 2 + 1, ··· I Important applications: Random number generation, cryptography (RSA public-key algorithm)

Integers and prime numbers

I Many problems are simple to state, yet difficult to prove or refute I Famous (proved) theorems in number theory I There are infinitely many prime numbers I (Fermat’s Last Theorem:) There is not no non-trivial integer solutions to x n + y n = zn for n ≥ 3 I (Fermat’s Little Theorem:) if p is a prime, then for any number a co-prime to p, then ap−1 ≡ 1 (mod p). I Important applications: Random number generation, cryptography (RSA public-key algorithm)

Integers and prime numbers

I Many problems are simple to state, yet difficult to prove or refute I Famous (proved) theorems in number theory I There are infinitely many prime numbers I (Fermat’s Last Theorem:) There is not no non-trivial integer solutions to x n + y n = zn for n ≥ 3 I (Fermat’s Little Theorem:) if p is a prime, then for any number a co-prime to p, then ap−1 ≡ 1 (mod p). I Special numbers: Mersenne prime 2p − 1, Fermat number 2n Fn = 2 + 1, ··· Integers and prime numbers

I Many problems are simple to state, yet difficult to prove or refute I Famous (proved) theorems in number theory I There are infinitely many prime numbers I (Fermat’s Last Theorem:) There is not no non-trivial integer solutions to x n + y n = zn for n ≥ 3 I (Fermat’s Little Theorem:) if p is a prime, then for any number a co-prime to p, then ap−1 ≡ 1 (mod p). I Special numbers: Mersenne prime 2p − 1, Fermat number 2n Fn = 2 + 1, ··· I Important applications: Random number generation, cryptography (RSA public-key algorithm) Unproved in number theory I Goldbach : every even integer greater than two is the sum of two prime numbers (tested by for all integer up to 1020) I Twin Prime Conjecture: are there infinitely many pairs of twin primes like (5, 7), (29, 31), ··· ? Largest twin prime pair known 2996863034895 · 21290000 ± 1 (388, 342 digits) I Collatz Conjectures: For any integer n, the iteration ( 3n + 1, if n is odd, n 7→ n/2, if n is even,

eventually contains 1. I Riemann Hypothesis: All non-trivial zeroes of the zeta function ∞ X ζ(s) = n−s n=1 have real part 1/2 (on the complex plane). Two ”simple” questions in the introduction

Q1: what is the smallest (in absolute value) integer solutions to x3 + y 3 + z3 = 42? x = −80538738812075974, y = 80435758145817515, z = 12602123297335631.

Q2: what is the smallest non-negative integer solutions to x y z + + = 4? y + z z + x x + y

x = 154476802108746166441951315019919837485664325669565431700026634898253202035277999, y = 36875131794129999827197811565225474825492979968971970996283137471637224634055579, z = 4373612677928697257861252602371390152816537558161613618621437993378423467772036. Some related built-in functions

I primes(N): return all primes up to N I isprime: whether a number is prime I factor: factorize the number I gcd: greatest common I lcm: least common multiplier Each type has its own range, for example: I int8: [−27, 27 − 1] = [−128, 127] I int64: [−263, 263 − 1] = [−9223372036854775808, 9223372036854775807] (19 digits) I uint16: [0, 216 − 1] = [0, 65535] I double: about [−253, 253](≈ 9 × 1015)

I Many operations (like sqrt) can not be applied to integers. I Make sure the number stays in the right range (otherwise the number is shrinked to the range) I Use symbolic numbers (though slower), if outside this range, like the of 20!: factor(factorial(sym(20)))

Representing numbers in MATLAB I double: the default type for any decimal number, like the output for rand I int8, int16, int32, int64: signed integers I uint8, uint16, uint32, uint64: unsigned integers I Many operations (like sqrt) can not be applied to integers. I Make sure the number stays in the right range (otherwise the number is shrinked to the range) I Use symbolic numbers (though slower), if outside this range, like the factorization of 20!: factor(factorial(sym(20)))

Representing numbers in MATLAB I double: the default type for any decimal number, like the output for rand I int8, int16, int32, int64: signed integers I uint8, uint16, uint32, uint64: unsigned integers

Each type has its own range, for example: I int8: [−27, 27 − 1] = [−128, 127] I int64: [−263, 263 − 1] = [−9223372036854775808, 9223372036854775807] (19 digits) I uint16: [0, 216 − 1] = [0, 65535] I double: about [−253, 253](≈ 9 × 1015) Representing numbers in MATLAB I double: the default type for any decimal number, like the output for rand I int8, int16, int32, int64: signed integers I uint8, uint16, uint32, uint64: unsigned integers

Each type has its own range, for example: I int8: [−27, 27 − 1] = [−128, 127] I int64: [−263, 263 − 1] = [−9223372036854775808, 9223372036854775807] (19 digits) I uint16: [0, 216 − 1] = [0, 65535] I double: about [−253, 253](≈ 9 × 1015)

I Many operations (like sqrt) can not be applied to integers. I Make sure the number stays in the right range (otherwise the number is shrinked to the range) I Use symbolic numbers (though slower), if outside this range, like the factorization of 20!: factor(factorial(sym(20))) theorem

Problem: verify that the number of prime up N is about N/ log N, when N is large?

How do you use the built-in function primes to accomplish this? Related Problem: find the largest twin primes both less than or equal to N

Twin prime conjecture: are there infinitely many twin primes?

Twin primes

Problem: List all twin primes up to N

Find all pairs of consecutive primes in the output of primes, with gap exactly two. Twin prime conjecture: are there infinitely many twin primes?

Twin primes

Problem: List all twin primes up to N

Find all pairs of consecutive primes in the output of primes, with gap exactly two.

Related Problem: find the largest twin primes both less than or equal to N Twin primes

Problem: List all twin primes up to N

Find all pairs of consecutive primes in the output of primes, with gap exactly two.

Related Problem: find the largest twin primes both less than or equal to N

Twin prime conjecture: are there infinitely many twin primes? Largest gap between consecutive primes 1

Problems: what is the largest gap between two consecutive primes that are less than or equal to a given number N

1More on wiki: https://en.wikipedia.org/wiki/Prime_gap Unfortunate, Fn with n ≤ 4 are the only known prime numbers; the conjecture was refuted first by Euler in 1732 by showing that 32 F5 = 2 + 1 = 4294967297 = 641 × 6700417.

Current Fn is completely factorized up to n = 11, and incompletely factorized for a few more (only one or more factors of Fn are known).

2n Fermat prime number: Fn = 2 + 1 Observations made by : 1 F0 = 2 + 1 =3 is prime 2 F1 = 2 + 1 =5 is prime 22 F2 = 2 + 1=17 is prime 23 F3 = 2 + 1=257 is prime 24 F4 = 2 + 1=65537 is prime He concluded all such numbers are prime. Current Fn is completely factorized up to n = 11, and incompletely factorized for a few more (only one or more factors of Fn are known).

2n Fermat prime number: Fn = 2 + 1 Observations made by Pierre de Fermat: 1 F0 = 2 + 1 =3 is prime 2 F1 = 2 + 1 =5 is prime 22 F2 = 2 + 1=17 is prime 23 F3 = 2 + 1=257 is prime 24 F4 = 2 + 1=65537 is prime He concluded all such numbers are prime.

Unfortunate, Fn with n ≤ 4 are the only known prime numbers; the conjecture was refuted first by Euler in 1732 by showing that 32 F5 = 2 + 1 = 4294967297 = 641 × 6700417. 2n Fermat prime number: Fn = 2 + 1 Observations made by Pierre de Fermat: 1 F0 = 2 + 1 =3 is prime 2 F1 = 2 + 1 =5 is prime 22 F2 = 2 + 1=17 is prime 23 F3 = 2 + 1=257 is prime 24 F4 = 2 + 1=65537 is prime He concluded all such numbers are prime.

Unfortunate, Fn with n ≤ 4 are the only known prime numbers; the conjecture was refuted first by Euler in 1732 by showing that 32 F5 = 2 + 1 = 4294967297 = 641 × 6700417.

Current Fn is completely factorized up to n = 11, and incompletely factorized for a few more (only one or more factors of Fn are known). Make sure the right operation is applied on the string or vector (convert back into numbers), like the built-in function str2num.

Numbers and Digits

num2str: conversion of a number like 2020 into string (sequence of characters). You can convert digital string back to numbers, like

1 str2num(’2020’); % the output is the number 2020

Equivalently, how to convert a number into a vector of number (from the number 2020 to [2, 0, 2, 0])?

1 function dig = num2dig(n) 2 % convert a number into a vector of digits 3 % for example: num2dig(2020) --> [2 0 2 0] Numbers and Digits

num2str: conversion of a number like 2020 into string (sequence of characters). You can convert digital string back to numbers, like

1 str2num(’2020’); % the output is the number 2020

Equivalently, how to convert a number into a vector of number (from the number 2020 to [2, 0, 2, 0])?

1 function dig = num2dig(n) 2 % convert a number into a vector of digits 3 % for example: num2dig(2020) --> [2 0 2 0]

Make sure the right operation is applied on the string or vector (convert back into numbers), like the built-in function str2num. Questions related to implementation: I Which variable to loop? I How to test that the three digits are the same?

Perfect squares with the same last three digits

Problem: Find all non-trivial2 perfect squares between 1000 and 107 that end with the same three digits like 1444.

2Here “non-trivial” means not ending with the three zeros, and hence 100 is excluded because 1002 = 10000 Perfect squares with the same last three digits

Problem: Find all non-trivial2 perfect squares between 1000 and 107 that end with the same three digits like 1444.

Questions related to implementation: I Which variable to loop? I How to test that the three digits are the same?

2Here “non-trivial” means not ending with the three zeros, and hence 100 is excluded because 1002 = 10000 6174: the Kaprekar’s constant Starting with any four-digit number (at least two different digits): (a) Arrange the digit in descending and then ascending order to get two four-digits (adding leading zeros if needed) (b) Subtract the smaller number from the bigger number (c) Go back to step (a)

For example, start with the number 2020: I 2020 − 22 7→ 1998 I 9981 − 1899 7→ 8082 I 8820 − 288 = 8532 I 8532 − 2358 7→ 6174 I 7641 − 1467 7→ 6174

Task: write a function to perform the iterations. Key observation (to avoid large number): work only on the last two digits!

Example: the last two digits of 2019 × 36032 = 72748608 is exactly the same as 19 × 32=608.

Solution: find the last two digits of

2019, 20192, 20193, ··· , 20192019

or equivalently 1919.

Last two decimal digits of 20192019? Solution: find the last two digits of

2019, 20192, 20193, ··· , 20192019

or equivalently 1919.

Last two decimal digits of 20192019?

Key observation (to avoid large number): work only on the last two digits!

Example: the last two digits of 2019 × 36032 = 72748608 is exactly the same as 19 × 32=608. Last two decimal digits of 20192019?

Key observation (to avoid large number): work only on the last two digits!

Example: the last two digits of 2019 × 36032 = 72748608 is exactly the same as 19 × 32=608.

Solution: find the last two digits of

2019, 20192, 20193, ··· , 20192019

or equivalently 1919. Can we still find the last two digits of

2019 2019, 20192, 20193, 20194, ··· , 20192 ,

for all 22019 such numbers?

Try these sequence:

0 1 2 3 2019 20192 , 20192 , 20192 , 20192 , ··· , 20192 ,

with the fact that

k k k+1 (20192 )2 = 20192·2 = 20192

Last two decimal digits of 201922019 ? Try these sequence:

0 1 2 3 2019 20192 , 20192 , 20192 , 20192 , ··· , 20192 ,

with the fact that

k k k+1 (20192 )2 = 20192·2 = 20192

Last two decimal digits of 201922019 ?

Can we still find the last two digits of

2019 2019, 20192, 20193, 20194, ··· , 20192 ,

for all 22019 such numbers? Last two decimal digits of 201922019 ?

Can we still find the last two digits of

2019 2019, 20192, 20193, 20194, ··· , 20192 ,

for all 22019 such numbers?

Try these sequence:

0 1 2 3 2019 20192 , 20192 , 20192 , 20192 , ··· , 20192 ,

with the fact that

k k k+1 (20192 )2 = 20192·2 = 20192 Key built-in function: dec2bin (decimal numbers to binary strings)

Power algorithm for

Problem: find the remainder of np, when divided by m (common in RSA encryption algorithm with large n, p and m)

The power algorithm illustrated for 20192019(mod 100): I Find the binary representation of the power:

10 9 8 7 6 5 1 0 2019 = (11111100011)2 = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2

k I Find 20192 (mod 100) for k = 0, 1, 2, ··· 10 I Find 20192019(mod 100) using 10 9 5 0 20192 · 20192 ··· 20192 · 20192 (mod 100) = 10 9 1 0 20182 (mod 100) · 20192 (mod 100) ··· 20192 (mod 100) · 20192 (mod 100) Power algorithm for modular arithmetic

Problem: find the remainder of np, when divided by m (common in RSA encryption algorithm with large n, p and m)

The power algorithm illustrated for 20192019(mod 100): I Find the binary representation of the power:

10 9 8 7 6 5 1 0 2019 = (11111100011)2 = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2

k I Find 20192 (mod 100) for k = 0, 1, 2, ··· 10 I Find 20192019(mod 100) using 10 9 5 0 20192 · 20192 ··· 20192 · 20192 (mod 100) = 10 9 1 0 20182 (mod 100) · 20192 (mod 100) ··· 20192 (mod 100) · 20192 (mod 100)

Key built-in function: dec2bin (decimal numbers to binary strings) The most efficient way is based on the Fermat’s Little Theorem

ap−1 = a · ap−2 = 1 (mod p),

which gives x = ap−2 (mod p).

Fast modular multiplicative inverse

What is the modular multiplicative inverse of 2019 to the mudulus p = 20988936657440586486151264256610222593863921 (the largest prime number found before the age of computer)? In other words, find an integer x such that

2019 · x = 1 (mod p) Fast modular multiplicative inverse

What is the modular multiplicative inverse of 2019 to the mudulus p = 20988936657440586486151264256610222593863921 (the largest prime number found before the age of computer)? In other words, find an integer x such that

2019 · x = 1 (mod p)

The most efficient way is based on the Fermat’s Little Theorem

ap−1 = a · ap−2 = 1 (mod p),

which gives x = ap−2 (mod p). Observation:

1 n! NO. zeros 2 4! = 24 0 3 5! = 120 1 4 6! = 720 1 5 9! = 362880 1 6 10!= 3628800 2 7 11!= 39916800 2 8 16!= 20922789888000 3 9 27! = 10888869450418352160768000000 6 10 32! = 263130836933693530167218012160000000 7

How about the following number:

2200 · 3300 · 5500 · 7700

The number of zeros is exactly the minimal power of 2 and 5!

Zeros at the end of n! Problem: how many zeros at the end of the factorial n! How about the following number:

2200 · 3300 · 5500 · 7700

The number of zeros is exactly the minimal power of 2 and 5!

Zeros at the end of n! Problem: how many zeros at the end of the factorial n!

Observation:

1 n! NO. zeros 2 4! = 24 0 3 5! = 120 1 4 6! = 720 1 5 9! = 362880 1 6 10!= 3628800 2 7 11!= 39916800 2 8 16!= 20922789888000 3 9 27! = 10888869450418352160768000000 6 10 32! = 263130836933693530167218012160000000 7 The number of zeros is exactly the minimal power of 2 and 5!

Zeros at the end of n! Problem: how many zeros at the end of the factorial n!

Observation:

1 n! NO. zeros 2 4! = 24 0 3 5! = 120 1 4 6! = 720 1 5 9! = 362880 1 6 10!= 3628800 2 7 11!= 39916800 2 8 16!= 20922789888000 3 9 27! = 10888869450418352160768000000 6 10 32! = 263130836933693530167218012160000000 7

How about the following number:

2200 · 3300 · 5500 · 7700 Zeros at the end of n! Problem: how many zeros at the end of the factorial n!

Observation:

1 n! NO. zeros 2 4! = 24 0 3 5! = 120 1 4 6! = 720 1 5 9! = 362880 1 6 10!= 3628800 2 7 11!= 39916800 2 8 16!= 20922789888000 3 9 27! = 10888869450418352160768000000 6 10 32! = 263130836933693530167218012160000000 7

How about the following number:

2200 · 3300 · 5500 · 7700

The number of zeros is exactly the minimal power of 2 and 5! Exact formula: ∞ n   n   n  X  n  α = + + + ··· = p p p2 p3 pk k=1 Heuristic reasoning (for power of 2 in 10!):  10  I 5 = 2 numbers counted once: 2, 4, 6, 8, 10  10  I 2 = 22 numbers counted twice: 4, 8  10  I 1 = 23 number counted three times: 8

The power of a prime factor in n!

Problem: If the factorial of n, i.e. n!, can be factorized as

n! = 2α2 3α3 5α5 ···

what is αp for the prime number p? The power of a prime factor in n!

Problem: If the factorial of n, i.e. n!, can be factorized as

n! = 2α2 3α3 5α5 ···

what is αp for the prime number p?

Exact formula: ∞ n   n   n  X  n  α = + + + ··· = p p p2 p3 pk k=1 Heuristic reasoning (for power of 2 in 10!):  10  I 5 = 2 numbers counted once: 2, 4, 6, 8, 10  10  I 2 = 22 numbers counted twice: 4, 8  10  I 1 = 23 number counted three times: 8 Other challenging problems to explore:

1. Four-square theorem: every integer can be expressed as the sum of at most four squares (7 = 22 + 12 + 12 + 12). Can you verify it up to some large number N ?

2. Taxcab number: What is the smallest number that is greater than 36032 and can be written as the sum two (positive) cubes in two different way3?

3. Warning problems: every integer can be expressed as the sum of how many cubes (or higher order powers)?

4. Last integer√ digit of irrational numbers: What is the last digit of b(2 + 5)2019c ?

3The smallest taxcab number is 1729 = 13 + 123 = 93 + 103 attributed to India mathematician Ramanujan.