Math Recipes
Math Recipes
Dennis Yurichev.
1 Unsorted parts 1
1.1 Fencepost error / off-by-one error . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 GCD and LCM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.1 GCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2.2 Oculus VR Flicks and GCD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2.3 LCM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.3 Signed numbers: two’s complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.3.1 Couple of additions about two’s complement form . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3.2 -1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4 Mandelbrot set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4.1 A word about complex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.4.2 How to draw the Mandelbrot set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2 Boolean algebra 14
2.1 XOR (exclusive OR) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.1 Logical difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.2 Everyday speech . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.3 Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.4 RAID1 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.1.5 XOR swap algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.6 XOR linked list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.1.7 Switching value trick . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.1.8 Zobrist hashing / tabulation hashing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
2.1.9 By the way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2 AND operation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2.1 Checking if a value is on 2n boundary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.2.2 KOI-8R Cyrillic encoding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
2.3 AND and OR as subtraction and addition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.3.1 ZX Spectrum ROM text strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2.4 Hamming weight / population count . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
3 Set theory 22
3.1 Set theory explanation via boolean operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.1.1 Collecting TV series . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.1.2 Rock band . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.1.3 Languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
3.1.4 De Morgan’s laws . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.1.5 Powerset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
3.1.6 Inclusion-exclusion principle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4 IEEE 754 27
4.1 IEEE 754 round-off errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.1.1 The example from Wikipedia . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4.1.2 Conversion from double-precision to single-precision and back . . . . . . . . . . . . . . . . . . . . . 31
4.1.3 The moral of the story . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
4.1.4 All the files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
1 Redundant Array of Independent Disks
1
2
5 Calculus 37
5.1 What is derivative? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.1.1 Square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.1.2 Cube . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
5.1.3 Line . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.1.4 Circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
5.1.5 Cylinder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.1.6 Sphere . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
5.1.7 Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
6 Prime numbers 41
6.1 Integer factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
6.1.1 Using composite number as a container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
6.1.2 Using composite number as a container (another example) . . . . . . . . . . . . . . . . . . . . . . . . 43
6.2 Coprime numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
6.3 Semiprime numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6.4 How RSA2 works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6.4.1 Fermat little theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
6.4.2 Euler’s totient function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
6.4.3 Euler’s theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
6.4.4 RSA example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
6.4.5 So how it works? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
6.4.6 Breaking RSA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48
6.4.7 The difference between my simplified example and a real RSA algorithm . . . . . . . . . . . . . . . . 48
6.4.8 The RSA signature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
6.4.9 Hybrid cryptosystem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
7 Modulo arithmetics 50
7.1 Quick introduction into modular arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
7.1.1 Modular arithmetic on CPUs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
7.1.2 Getting random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
7.1.3 Reverse Engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.2 Remainder of division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.2.1 Remainder of division by modulo 2n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
7.3 Modulo inverse, part I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
7.3.1 No remainder? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.4 Modulo inverse, part II . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
7.5 Reversible linear congruential generator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
7.6 Getting magic number using extended Euclidean algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
8 Probability 60
8.1 Text strings right in the middle of compressed data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
8.2 Autocomplete using Markov chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
8.2.1 Dissociated press . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
8.2.2 Autocomplete . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
8.2.3 Further work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.2.4 The files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.2.5 Read more . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.2.6 Another thought-provoking example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
8.3 random.choices() in Python 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76
9 Combinatorics 78
9.1 Soldering a headphones cable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
9.2 Vehicle license plate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.3 Forgotten password . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
9.4 Executable file watermarking/steganography using Lehmer code and factorial number system . . . . . . . . . 85
9.5 De Bruijn sequences; leading/trailing zero bits counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
9.5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
9.5.2 Trailing zero bits counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90
9.5.3 Leading zero bits counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
2 Rivest–Shamir–Adleman cryptosystem
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
3
9.5.4 Performance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.5.5 Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.5.6 Generation of De Bruijn sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
9.5.7 Other articles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
11 Logarithms 102
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
11.1.1 Children’s approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
11.1.2 Scientists’ and engineers’ approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
11.2 Logarithmic scale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
11.2.1 In human perception . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
11.2.2 In electronics engineering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103
11.2.3 In IT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105
11.2.4 Web 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
11.2.5 Errors of TeX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106
11.2.6 Moore’s Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
11.2.7 Money . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
11.2.8 Sizes, values, weights... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
11.3 Multiplication and division using addition and subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
11.3.1 Logarithmic slide rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
11.3.2 Logarithmic tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
11.3.3 Working with very small and very large numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
11.3.4 IEEE 754: adding and subtracting exponents . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
11.4 Exponentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
11.5 Square root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
11.6 Base conversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
11.7 Binary logarithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
11.7.1 Weights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
11.7.2 A burned LED garland . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
11.7.3 Denoting a number of bits for some value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
11.7.4 Calculating binary logarithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
11.7.5 O(log n) time complexity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
11.8 Common (base 10) logarithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
11.9 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
11.9.1 Hexdump and binary logarithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
11.9.2 Naive approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
11.10 Natural logarithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
11.10.1 Savings account in your bank . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
11.10.2 Exponential decay . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
4
13.1.6 Social graph: LiveJournal spammers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
13.1.7 Links graph: link farms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
13.1.8 Further reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
What is this?
This a collection of notes gathered here with an aim to offload my other books (“Reverse Engineering for Beginners”4 and
“SAT/SMT by Example”5 ) from mathematical theory, which still can be of interest for a general audience of programmers.
It can be said, this is a companion book.
Also, random posts from my blog.
So, nothing special.
Thanks
Henry Legge.
4 https://beginners.re/
5 https://sat-smt.codes/
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 1
Unsorted parts
When I was a younger man–two wives ago, 250,000 cigarettes ago, 3,000 quarts of booze ago.
Go figure, how many ways there are exist to determine that point in time (”two wives ago”). Considering that Kurt Vonnegut
at the moment of writing was 1) married; 2) not married.
Also, often, a phrase in advertisement like ”... [something] is valid from January 10th to 20th”. Without the addition of the
word ”inclusive”, it is not clear if January 20th is included in this interval or not? Is this a half-closed interval or a closed
interval?
See also: http://www.catb.org/jargon/html/O/off-by-one-error.html, http://www.catb.org/jargon/html/F/
fencepost-error.html.
Now an excerpt from the amusing article by Denys Wilkinson:
Communications direct to the projectionist are always good and should be made in such a manner that
it is not immediately clear whether the Slidesman is addressing the projectionist or the audience. Absurd
complication in the instructions must be avoided. The Slidesman uses something like: ‘After the next slide
but two I shall want to look again at the last one but four.’ After the next slide: ‘I meant of course that slide
which was then going to be the last one but four after I had indeed had that which was going to be the next
but two, not that which was then the last but four.’ Follow this by skipping one slide.
1
2
1.2 GCD and LCM
1.2.1 GCD
What is Greatest common divisor (GCD2 )?
Let’s suppose, you want to cut a rectangle by squares. What is maximal square could be?
For a 14*8 rectangle, this is 2*2 square:
**************
**************
**************
**************
**************
**************
**************
**************
->
** ** ** ** ** ** **
** ** ** ** ** ** **
** ** ** ** ** ** **
** ** ** ** ** ** **
** ** ** ** ** ** **
** ** ** ** ** ** **
** ** ** ** ** ** **
** ** ** ** ** ** **
->
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
******* *******
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
3
**************
**************
**************
GCD of coprimes is 1.
GCD is also a common set of factors of several numbers. This we can see in Mathematica:
In []:= FactorInteger [300]
Out []= {{2, 2}, {3, 1}, {5, 2}}
In []:= 11*13*17
Out []= 2431
Listing 1.1: Another example from the Structure and Interpretation of Computer Programs
The greatest common divisor (GCD) of two integers a and b is defined to be the
largest integer that divides both a and b with no remainder .
This unit of time is the smallest time unit which is LARGER than a nanosecond ,
and can in integer quantities exactly represent a single frame duration for
24hz , 25hz , 30hz , 48hz , 50hz , 60hz , 90hz , 100hz , 120hz , and also 1/1000 divisions of
each.
This makes it suitable for use via std :: chrono :: duration and std :: ratio
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
4
for doing timing work against the system high resolution clock , which is in
nanoseconds ,
but doesn 't get slightly out of sync when doing common frame rates .
In order to accomodate media playback , we also support some common audio sample rates
as well.
This list is not exhaustive , but covers the majority of digital audio formats .
They are 8kHz , 16kHz , 22.05 kHz , 24kHz , 32kHz , 44.1 kHz , 48kHz , 88.2 kHz , 96kHz , and 192
kHz.
While humans can 't hear higher than 48kHz , the higher sample rates are used
for working audio files which might later be resampled or retimed .
The NTSC variations (∼29.97 , etc) are actually defined as 24 * 1000/1001 and
30 * 1000/1001 , which are impossible to represent exactly in a way where 1 second is
exact ,
so we don 't bother - they 'll be inexact in any circumstance .
Details
( https://github.com/OculusVR/Flicks )
Where the number came from? Let’s enumerate all possible time intervals they want to use and find GCD using Mathematica:
In []:= GCD [1/24 , 1/24000 , 1/25 , 1/25000 , 1/30 , 1/30000 , 1/48 , 1/50 ,
1/50000 , 1/60 , 1/60000 , 1/90 , 1/90000 , 1/100 , 1/100000 , 1/120 ,
1/120000 , 1/8000 , 1/16000 , 1/22050 , 1/24000 , 1/32000 , 1/44100 ,
1/48000 , 1/88200 , 1/96000 , 1/192000]
1
Rationale: you may want to play a video with 50 fps and, simultaneously, play audio with 96kHz sampling rate. Given that,
you can change video frame after each 14112000 flicks and change one audio sample after each 7350 flicks. Use any other
video fps and any audio sampling rate and you will have all time periods as integer numbers. No ratios any more.
On contrary, one nanosecond wouldn’t fit: try to represent 1/30 second in nanoseconds, this is (still) ratio: 33333.33333...
nanoseconds.
1.2.3 LCM
Many people use LCM3 in school. Sum up 14 and 16 . To find an answer mentally, you ought to find Lowest Common Denomi-
6 4
nator, which can be 4*6=24. Now you can sum up 24 + 24 = 10
24 .
3 Least Common Multiple
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
5
3 2 5
But the lowest denominator is also a LCM. LCM of 4 and 6 is 12: 12 + 12 = 12 .
This happens — you stuck in traffic jams, there are two vehicles in front of you, and you see how yellow turn signals blinking,
out of sync. You wonder, how many blinks you have to wait so that they will blink at once?
If the first signal blinking with period of 0.6s (or 600ms) and the second is 0.5s (or 500ms), this problem can be solved graph-
ically:
*****|*****|*****|*****|*****| 600 ms
****|****|****|****|****|****| 500 ms
In GNU coreutils, we can find that LCM is used to find optimal buffer size, if buffer sizes in input and ouput files are differ. For
example, input file has buffer of 4096 bytes, and output is 6144. Well, these sizes are somewhat suspicious. I made up this
example. Nevertheless, LCM of 4096 and 6144 is 12288. This is a buffer size you can allocate, so that you will minimize number
of read/write operations during copying.
https://github.com/coreutils/coreutils/blob/4cb3f4faa435820dc99c36b30ce93c7d01501f65/src/copy.c#L1246.
https://github.com/coreutils/coreutils/blob/master/gl/lib/buffer-lcm.c.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
6
binary hexadecimal unsigned signed
01111111 0x7f 127 127
01111110 0x7e 126 126
...
00000110 0x6 6 6
00000101 0x5 5 5
00000100 0x4 4 4
00000011 0x3 3 3
00000010 0x2 2 2
00000001 0x1 1 1
00000000 0x0 0 0
11111111 0xff 255 -1
11111110 0xfe 254 -2
11111101 0xfd 253 -3
11111100 0xfc 252 -4
11111011 0xfb 251 -5
11111010 0xfa 250 -6
...
10000010 0x82 130 -126
10000001 0x81 129 -127
10000000 0x80 128 -128
The difference between signed and unsigned numbers is that if we represent 0xFFFFFFFE and 0x00000002 as unsigned, then
the first number (4294967294) is bigger than the second one (2). If we represent them both as signed, the first one becomes
−2, and it is smaller than the second (2).
That is the reason why conditional jumps in x86 are present both for signed (e.g. JG, JL) and unsigned (JA, JB) operations.
For the sake of simplicity, this is what one needs to know:
• Numbers can be signed or unsigned.
• C/C++ signed types:
– int64_t (-9,223,372,036,854,775,808 .. 9,223,372,036,854,775,807) (- 9.2.. 9.2 quintillions) or
0x8000000000000000..0x7FFFFFFFFFFFFFFF),
– int (-2,147,483,648..2,147,483,647 (- 2.15.. 2.15Gb) or
0x80000000..0x7FFFFFFF),
– char (-128..127 or 0x80..0x7F),
– ssize_t.
Unsigned:
– uint64_t (0..18,446,744,073,709,551,615 ( 18 quintillions)
or 0..0xFFFFFFFFFFFFFFFF),
– unsigned int (0..4,294,967,295 ( 4.3Gb) or 0..0xFFFFFFFF),
– unsigned char (0..255 or 0..0xFF),
– size_t.
• Signed types have the sign in the MSB4 : 1 means “minus”, 0 means “plus”.
• Promoting to a larger data types is simple: get the leftmost bit and fill all other newly created bits with it.
• Negation is simple: just invert all bits and add 1.
We can keep in mind that a number of inverse sign is located on the opposite side at the same proximity from zero. The
addition of one is needed because zero is present in the middle.
• The addition and subtraction operations work well for both signed and unsigned values. But for multiplication and
division operations, x86 has different instructions: IDIV/IMUL for signed and DIV/MUL for unsigned.
• Here are some more x86 instructions that work with signed numbers:
CBW/CWD/CWDE/CDQ/CDQE, MOVSX, SAR.
4 Most Significant Bit
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
7
A table of some negative and positive values (1.3) looks like thermometer with Celsius scale. This is why addition and subtrac-
tion works equally well for both signed and unsigned numbers: if the first addend is represented as mark on thermometer,
and one need to add a second addend, and it’s positive, we just shift mark up on thermometer by the value of second addend.
If the second addend is negative, then we shift mark down to absolute value of the second addend.
Addition of two negative numbers works as follows. For example, we need to add -2 and -3 using 16-bit registers. -2 and -3
is 0xfffe and 0xfffd respectively. If we add these numbers as unsigned, we will get 0xfffe+0xfffd=0x1fffb. But we work on 16-
bit registers, so the result is cut off, the first 1 is dropped, 0xfffb is left, and this is -5. This works because -2 (or 0xfffe) can be
represented using plain English like this: “2 lacks in this value up to maximal value in 16-bit register + 1”. -3 can be represented
as “…3 lacks in this value up to …”. Maximal value of 16-bit register + 1 is 0x10000. During addition of two numbers and cutting
off by 216 modulo, 2 + 3 = 5 will be lacking.
int main ()
{
unsigned int val=∼0; // change to "unsigned char" to get maximal value for the
unsigned 8-bit byte
// 0-1 will also work, or just -1
printf ("%u\n", val); // %u for unsigned
};
int main ()
{
signed int val =1; // change to "signed char" to find values for signed byte
while (val !=0)
{
printf ("%d %d\n", val , ∼val);
val=val <<1;
};
};
Output is:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
8
...
536870912 -536870913
1073741824 -1073741825
-2147483648 2147483647
Two last numbers are minimum and maximum signed 32-bit int respectively.
1.3.2 -1
Now you see that −1 is when all bits are set. Often, you can find the −1 constant in all sorts of code, where a constant with
all bits set are needed, for example, a mask.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
9
• Enumerate all points on screen.
• Check if the specific point is in the Mandelbrot set.
• Here is how to check it:
– Represent the point as a complex number.
– Calculate the square of it.
– Add the starting value of the point to it.
– Does it go off limits? If yes, break.
– Move the point to the new place at the coordinates we just calculated.
– Repeat all this for some reasonable number of iterations.
• The point is still in limits? Then draw the point.
• The point has eventually gone off limits?
– (For a black-white image) do not draw anything.
– (For a colored image) transform the number of iterations to some color. So the color shows the speed with which
point has gone off limits.
Here is Pythonesque algorithm for both complex and integer number representations:
while True:
if (P> bounds ):
break
P=P^2+ P_start
if iterations > max_iterations :
break
iterations ++
return iterations
# black -white
for each point on screen P:
if check_if_is_in_set (P) < max_iterations :
draw point
# colored
for each point on screen P:
iterations = if check_if_is_in_set (P)
map iterations to color
draw color point
The integer version is where the operations on complex numbers are replaced with integer operations according to the rules
which were explained above.
while True:
if (X^2 + Y^2 > bounds ):
break
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
10
new_X =X^2 - Y^2 + X_start
new_Y =2*X*Y + Y_start
if iterations > max_iterations :
break
iterations ++
return iterations
# black -white
for X = min_X to max_X :
for Y = min_Y to max_Y :
if check_if_is_in_set (X,Y) < max_iterations :
draw point at X, Y
# colored
for X = min_X to max_X :
for Y = min_Y to max_Y :
iterations = if check_if_is_in_set (X,Y)
map iterations to color
draw color point at X,Y
Here is also a C# source which is present in the Wikipedia article5 , but we’ll modify it so it will print the iteration numbers
instead of some symbol 6 :
using System ;
using System . Collections . Generic ;
using System .Linq;
using System .Text;
namespace Mnoj
{
class Program
{
static void Main( string [] args)
{
double realCoord , imagCoord ;
double realTemp , imagTemp , realTemp2 , arg;
int iterations ;
for ( imagCoord = 1.2; imagCoord >= -1.2; imagCoord -= 0.05)
{
for ( realCoord = -0.6; realCoord <= 1.77; realCoord += 0.03)
{
iterations = 0;
realTemp = realCoord ;
imagTemp = imagCoord ;
arg = ( realCoord * realCoord ) + ( imagCoord * imagCoord );
while (( arg < 2*2) && ( iterations < 40))
{
realTemp2 = ( realTemp * realTemp ) - ( imagTemp * imagTemp ) -
realCoord ;
imagTemp = (2 * realTemp * imagTemp ) - imagCoord ;
realTemp = realTemp2 ;
arg = ( realTemp * realTemp ) + ( imagTemp * imagTemp );
iterations += 1;
}
Console .Write ("{0 ,2:D} ", iterations );
}
Console . Write ("\n");
}
5 wikipedia
6 Here is also the executable file: https://math.recipes/current_tree/unsorted/mandel/dump_iterations.exe
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
11
Console . ReadKey ();
}
}
}
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
12
There is a cool demo available at http://demonstrations.wolfram.com/MandelbrotSetDoodle/, which shows visually
how the point moves on the plane at each iteration for some specific point. Here are two screenshots.
First, we’ve clicked inside the yellow area and saw that the trajectory (green line) eventually swirls at some point inside:
This implies that the point we’ve clicked belongs to the Mandelbrot set.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
13
Then we’ve clicked outside the yellow area and saw a much more chaotic point movement, which quickly went off bounds:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 2
Boolean algebra
2.1.3 Encryption
XOR is heavily used in both amateur2 and real encryption (at least in Feistel network).
XOR is very useful here because: cipher_text = plain_text ⊕ key and then: (plain_text ⊕ key) ⊕ key = plain_text.
2.1.4 RAID4
RAID4 offers a very simple method to protect hard disks. For example, there are several disks (D1 , D2 , D3 , etc.) and one
parity disk (P ). Each bit/byte written to parity disk is calculated and written on-fly:
P = D1 ⊕ D2 ⊕ D3 (2.1)
If any of disks is failed, for example, D2 , it’s restored using the very same way:
1 http://www.bitsavers.org/pdf/cray/CRAY-1/HR-0004-CRAY_1_Hardware_Reference_Manual-PRELIMINARY-1975.OCR.pdf
2 “SAT/SMT by Example”3 has some examples.
14
15
D2 = D1 ⊕ P ⊕ D3 (2.2)
If parity disk failed, it is restored using 2.1 way. If two of any disks are failed, then it wouldn’t be possible to restore both.
RAID5 is more advanced, but this XOR property is still exploited there.
That’s why RAID controllers has hardware “XOR accelerators” helping to XOR large chunks of written data on-fly. When com-
puters get faster and faster, it now can be done at software level, using SIMD4 .
Let’s find out, how it works. First, we will rewrite it to step aside from x86 assembly language:
X = X XOR Y
Y = Y XOR X
X = X XOR Y
What X and Y has at each step? Just keep in mind the simple rule: (X ⊕ Y ) ⊕ Y = X for any values of X and Y.
Let’s see, X after 1st step has X ⊕ Y ; Y after 2nd step has Y ⊕ (X ⊕ Y ) = X; X after 3rd step has (X ⊕ Y ) ⊕ X = Y .
Hard to say if anyone should use this trick, but it servers as a good demonstration example of XOR properties.
Wikipedia article5 has also yet another explanation: addition and subtraction operations can be used instead of XOR:
X = X + Y
Y = X - Y
X = X - Y
Let’s see: X after 1st step has X + Y ; Y after 2nd step has X + Y − Y = X; X after 3rd step has X + Y − X = Y .
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
16
address link field contents
A0 A1
A1 A0 ⊕ A2
A2 A1 ⊕ A3
A3 A2 ⊕ A4
A4 A3
And again, hard to say if anyone should use this tricky hacks, but this is also a good demonstration of XOR properties. As
with XOR swap algorithm, Wikipedia article about it also offers way to use addition or subtraction instead of XOR: https:
//en.wikipedia.org/wiki/XOR_linked_list.
int main ()
{
int a =123;
# define C 123^456
a=a^C;
printf ("%d\n", a);
a=a^C;
printf ("%d\n", a);
a=a^C;
printf ("%d\n", a);
};
It works because 123 ⊕ 123 ⊕ 456 = 0 ⊕ 456 = 456 and 456 ⊕ 123 ⊕ 456 = 456 ⊕ 456 ⊕ 123 = 0 ⊕ 123 = 123.
One can argue, worth it using or not, especially keeping in mind code readability. But this is yet another demonstration of
XOR properties.
int position [8][8]; // for each square on board. 0 - no piece. 1..12 - piece
uint64_t hash;
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
17
for (int col =0; col <8; col ++)
{
int piece = position [row ][ col ];
if (piece !=0)
hash=hash^table [ piece ][ row ][ col ];
};
return hash;
Now the most interesting part: if the next (modified) chess position differs only by one (moved) piece, you don’t need to
recalculate hash for the whole position, all you need is:
hash =...; // (already calculated)
In other words, this code checks if there are any bit set among lower 12 bits. As a side effect, lower 12 bits is always a remain-
der from division a value by 4096 (because division by 2n is merely a right shift, and shifted (and dropped) bits are bits of
remainder).
Same story if you want to check if the number is odd or even:
if ( value &1)
// odd
else
// even
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
18
It was also a time, when non-Latin writing systems used second half of 8-bit ASCII table to accommodate non-Latin charac-
ters. There were several popular Cyrillic encodings, but KOI-8R (devised by Andrey “ache” Chernov) is somewhat unique in
comparison with others.
Someone may notice that Cyrillic characters are allocated almost in the same sequence as Latin ones. This leads to one
important property: if all 8th bits in Cyrillic text encoded in KOI-8R are to be reset, a text transforms into transliterated text
with Latin characters in place of Cyrillic. For example, Russian sentence:
Мой дядя самых честных правил, Когда не в шутку занемог, Он уважать себя заставил, И лучше
выдумать не мог.
…if encoded in KOI-8R and then 8th bit stripped, transforms into:
mOJ DQDQ SAMYH ^ESTNYH PRAWIL, kOGDA NE W [UTKU ZANEMOG, oN UWAVATX SEBQ ZASTAWIL, i
LU^[E WYDUMATX NE MOG.
…perhaps this is not very appealing æsthetically, but this text is still readable to Russian language natives.
Hence, Cyrillic text encoded in KOI-8R, passed through an old 7-bit service will survive into transliterated, but still readable
text.
Stripping 8th bit is automatically transposes any character from the second half of the (any) 8-bit ASCII table to the first one,
into the same place (take a look at red arrow right of table). If the character has already been placed in the first half (i.e., it
has been in standard 7-bit ASCII table), it’s not transposed.
Perhaps, transliterated text is still recoverable, if you’ll add 8th bit to the characters which were seems transliterated.
Drawback is obvious: Cyrillic characters allocated in KOI-8R table are not in the same sequence as in Russian/Bulgarian/Ukraini-
an/etc. alphabet, and this isn’t suitable for sorting, for example.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
19
( http://www.matthew-wilson.net/spectrum/rom/128_ROM0.html )
Last character has most significant bit set, which marks string end. Presumably, it was done to save some space? Old 8-bit
computers have very tight environment.
Characters of all messages are always in standard 7-bit ASCII table, so it’s guaranteed 7th bit is never used for characters.
To print such string, we must check MSB of each byte, and if it’s set, we must clear it, then print character, and then stop. Here
is a C example:
unsigned char hw []=
{
'H',
'e',
'l',
'l',
'o'|0 x80
};
void print_string ()
{
for (int i=0; ;i++)
{
if (hw[i]&0 x80) // check MSB
{
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
20
// clear MSB
// (in other words, clear all, but leave 7 lower bits intact)
printf ("%c", hw[i] & 0x7F);
// stop
break ;
};
printf ("%c", hw[i]);
};
};
Now what is interesting, since 7th bit is the most significant bit (in byte), we can check it, set it and remove it using arithmetical
operations instead of logical.
I can rewrite my C example:
unsigned char hw []=
{
'H',
'e',
'l',
'l',
'o'+0 x80
};
void print ()
{
for (int i=0; ;i++)
{
// hw[] must have 'unsigned char' type
if (hw[i] >= 0x80) // check for MSB
{
printf ("%c", hw[i]-0 x80); // clear MSB
// stop
break ;
};
printf ("%c", hw[i]);
};
};
By default, char is signed type in C/C++, so to compare it with variable like 0x80 (which is negative (−128) if treated as signed),
we must treat each character in text message as unsigned.
Now if 7th bit is set, the number is always larger or equal to 0x80. If 7th bit is clear, the number is always smaller than 0x80.
Even more than that: if 7th bit is set, it can be cleared by subtracting 0x80, nothing else. If it’s not set beforehand, however,
subtracting will destruct other bits.
Likewise, if 7th bit is clear, it’s possible to set it by adding 0x80. But if it’s set beforehand, addition operation will destruct
some other bits.
In fact, this is valid for any bit. If the 4th bit is clear, you can set it just by adding 0x10: 0x100+0x10 = 0x110. If the 4th bit is set,
you can clear it by subtracting 0x10: 0x1234-0x10 = 0x1224.
It works, because carry isn’t happened during addition/subtraction. It will, however, happen, if the bit is already set there
before addition, or absent before subtraction.
Likewise, addition/subtraction can be replaced using OR/AND operation if two conditions are met: 1) you want to add/subtract
by a number in form of 2n ; 2) this bit in source value is clear/set.
For example, addition of 0x20 is the same as ORing value with 0x20 under condition that this bit is clear before: 0x1204|0x20
= 0x1204+0x20 = 0x1224.
Subtraction of 0x20 is the same as ANDing value with 0x20 (0x....FFDF), but if this bit is set before: 0x1234&(~0x20) = 0x1234&0xFFDF
= 0x1234-0x20 = 0x1214.
Again, it works because carry not happened when you add 2n number and this bit isn’t set before.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
21
This property of boolean algebra is important, worth understanding and keeping it in mind.
This branch of cryptography is fast-paced and very politically charged. Most designs are secret; a majority
of military encryptions systems in use today are based on LFSRs. In fact, most Cray computers (Cray 1, Cray
X-MP, Cray Y-MP) have a rather curious instruction generally known as “population count.” It counts the 1
bits in a register and can be used both to efficiently calculate the Hamming distance between two binary
words and to implement a vectorized version of a LFSR. I’ve heard this called the canonical NSA instruction,
demanded by almost all computer contracts.
10 Also Known As
11 https://yurichev.com/news/20210422_base64scanner/
12 National Security Agency
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 3
Set theory
Important to note that the add_to_set() function can be called several times with the same element.
22
23
// we single out one instrument from each rocker using AND
// then we join them all together using OR, this is 'set union'
// note: we 'take' two instruments from JAKE
// internally, this also must be 0xf (4 lowest bits)
# define STAR_ROCK_BAND ( JAKE_CAN_PLAY & GUITAR ) | ( JAKE_CAN_PLAY & VOCAL ) | (
JOHN_CAN_PLAY & BASS) | ( PETER_CAN_PLAY & DRUMS )
int main ()
{
// this equation must hold:
assert ( STAR_ROCK_BAND == ROCK_BAND_INSTRUMENTS );
};
The obvious limitation: only 32 elements in set are allowed in 32-bit integer type. But you can switch to 64-bit integer or to
bitstring data type.
3.1.3 Languages
# include <assert .h>
# include <stdio .h>
int main ()
{
// which languages can JOHN and PETER use to talk with each other?
// this is 'set intersection'
assert (( JOHN_CAN_SPEAK & PETER_CAN_SPEAK ) == ENGLISH );
// which languages can Peter speak so that John would understand him?
// 'set intersection' again
assert (( PETER_CAN_SPEAK & JOHN_CAN_UNDERSTAND ) == ( ENGLISH | GERMAN ));
// which languages can John speak so that Peter would understand him?
assert (( JOHN_CAN_SPEAK & PETER_CAN_UNDERSTAND ) == ENGLISH );
// which languages can John use so that both Peter and Natasha will understand him?
assert (( JOHN_CAN_SPEAK & ( PETER_CAN_UNDERSTAND & NATASHA_CAN_UNDERSTAND )) ==
ENGLISH );
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
24
assert (((∼JOHN_AND_PETER_CAN_UNDERSTAND ) & UNIVERSE ) == CHINESE );
By the way, an interesting story about applying set/logical operations on paper cards:
To allow a visual check that all cards in a deck were oriented the same way, one corner of each card
was beveled, much like Hollerith punched cards. Edge-notched cards, however, were not intended to be
read by machines such as IBM card sorters. Instead, they were manipulated by passing one or more slim
needles through selected holes in a group of cards. As the needles were lifted, the cards that were notched
in the hole positions where the needles were inserted would be left behind as rest of the deck was lifted by
the needles. Using two or more needles produced a logical and function. Combining the cards from two
different selections produced a logical or. Quite complex manipulations, including sorting were possible
using these techniques.
( https://en.wikipedia.org/wiki/Edge-notched_card )
3.1.5 Powerset
(According to Wiktionary: https://en.wiktionary.org/wiki/power_set.) The set whose elements comprise all the sub-
sets of S (including the empty set and S itself). An alternative notation is 2S ...
For example, we have 4 elements in set. Each element for each bit. How can we enumerate all possible combinations? Just
by listing all numbers between 0b0000 and 0b1111. That comprises 16 combinations or subsets, including empty set (starting
0b0000).
This is why 2S notation is also used: there are 2numberof elements subsets. In our case, 24 = 16.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
25
The problem is supposed to be solved using finite sets counting and inclusion–exclusion principle... But I’m slothful student
and would try simple bruteforce.
Each 1 bit in a variable would reflect 10% of population. Then I enumerate all possible pairs. I check only those pairs, where
there are 7 bits in p1 variable and 8 bits in p2 variable.
#!/usr/bin/env python3
_min =10
_max =0
The result:
min: 5
max: 7
You see, the minimum possible value is 5 (50%), the maximum is 7 (70%). Let’s add some debug info:
...
if x==5 or x==7:
print (" -")
print (" park1 {0:010 b}". format (p1), "(%d)" % popcnt (p1))
print (" park2 {0:010 b}". format (p2), "(%d)" % popcnt (p2))
print (" both {0:010 b}". format (p1 & p2), "(%d)" % popcnt (p1 & p2))
...
If maximum:
park1 0001111111 (7)
park2 0011111111 (8)
both 0001111111 (7)
If minimum:
park1 0001111111 (7)
park2 1111111100 (8)
both 0001111100 (5)
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
26
I’ve found such a cases, where ”ones” are allocated in such a way, so that the AND of ”ones” in ”both” would be minimal/-
maximal.
Observing this, we can deduce the general formula: maximal ”both” = min(park1, park2)
What about minimal ”both”? We can see that ”ones” from park1 must ”shift out” or ”hide in” to a corresponding empty space
of park2. So, minimal both = park2 - (100% - park1)
Variations of the problem from the same book:
Suppose that 100 senators voted on three separate senate bills as follows :
70 percent of the senators voted for the first bill , 65 percent voted for the second
bill ,
and 60 percent voted for the third bill. At least what percentage of the senators
voted for all three bills ?
Suppose that 25 people attended a conference with three sessions , where 15 people
attended the first session ,
18 the second session , and 12 the third session . At least how many people attended
all three sessions ?
Also, The Abstract Algebra book1 by I.N. Herstein has exercises like:
19. In his book A Tangled Tale , Lewis Carroll proposed the following riddle
about a group of disabled veterans : "Say that 70% have lost an eye , 75%
an ear , 80% an arm , 85% a leg. What percentage , at least , must have lost
all four ?" Solve Lewis Carroll 's problem .
1 https://archive.org/details/Herstein3thEditon/
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 4
IEEE 754
a = 0.2;
a += 0.1;
a -= 0.3;
return 0;
}
It appears that the program should not terminate . Yet the output is :
i=54 , a =1.000000
DoubleMantissaLen = 52;
27
28
convertDoubleQWORD [dw_] :=
convertDouble [ BitShiftRight [dw , DoubleBits - 1],
BitAnd [ BitShiftRight [dw , DoubleMantissaLen ], DoubleExpMask ],
BitAnd [dw , DoubleMantissaMask ]]
What are the previous and the next numbers? I decrease/increase mantissa by one bit here:
You see, the first is not exact, but the best approximation possible, in the double-precision IEEE 754 format.
You can’t store 0.1 as exact value by the same reason you can’t represent 13 value in any binary format. Well, maybe unbalanced
ternary computer could, but it will have problems in representing 12 value.
Now I souped up the code from Wikipedia:
# include <stdio .h>
# include <stdint .h>
# include <string .h>
# include <math.h>
# include <errno .h>
# include <fenv.h>
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
29
// sign
printf ("% ld ", (t>>( DoubleBits -1))&1); // sign
// exponent
for (int i=( DoubleBits -2); i!=( DoubleBits -2) -DoubleExpLen ; i--)
printf ("% ld", (t>>i)&1);
printf (" ");
// mantissa
for (int i=( DoubleBits -2) -DoubleExpLen ; i >=0; i--)
printf ("% ld", (t>>i)&1);
printf (" 0x%016 lx", t);
};
if (a ==0.0)
printf ("a==0\n");
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
30
else
printf ("a!=0\n");
return 0;
}
After summation of 0.2 and 0.1, FPU reports FE_INEXACT exception in FPU status word. It warns: the result can’t ”fit” into dou-
ble IEEE 754 exactly/precisely. The sum in form of 64-bit value is 0x3fd3333333333334. But at the beginning of our program,
we dumped 0.3, and it is 0x3fd3333333333333. The difference is one lowest bit of mantissa.
Let’s see what Mathematica can say about the exact sum:
In []:= a + b
Out []= 0.3000000000000000166533453693773481063544750213623046875
That is the result FPU tries to shove into double-precision IEEE 754 register, but can’t.
... while the best approximation of 0.3 is:
And the result produced by our code (lowest bit of mantissa incremented):
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
31
Since the rounding mode set by default is FE_TONEAREST, the FPU rounded it to the nearest value.
Now what is that difference by one lowest bit in mantissa?
This is the ’noise’ left in register after subtraction, that isn’t equal to zero:
Still FE_INEXACT value, but that works, because rounding gets other direction. The sum is ....33333 (not ....33334). (FE_DOWNWARD
would work as well.)
But in a real-world code it’s problematic to set rounding mode for each operation...
int main ()
{
float a;
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
32
// double a; // this will fix it
int i;
a = 0.0;
a += 0.1;
a -= 0.1;
if (a ==0.0)
printf ("a==0\n");
else
printf ("a!=0\n");
return 0;
}
// sign
printf ("%d ", (t>>( FloatBits -1))&1);
// exponent
for (int i=( FloatBits -2); i!=( FloatBits -2) -FloatExpLen ; i--)
printf ("%d", (t>>i)&1);
printf (" ");
// mantissa
for (int i=( FloatBits -2) -FloatExpLen ; i >=0; i--)
printf ("%d", (t>>i)&1);
printf (" 0x%08x", t);
};
int main ()
{
float a;
// double a; // this will fix it
int i;
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
33
a = 0.0;
printf ("a = 0.0;\ n");
printf ("%f %e ", a, a); print_float_in_binary (a); printf ("\n");
feclearexcept ( FE_ALL_EXCEPT );
a += 0.1;
if( fetestexcept ( FE_INEXACT ))
printf (" FE_INEXACT \n");
printf ("a += 0.1;\ n");
printf ("%f %e ", a, a); print_float_in_binary (a); printf ("\n");
feclearexcept ( FE_ALL_EXCEPT );
a -= 0.1;
if (a ==0.0)
printf ("a==0\n");
else
printf ("a!=0\n");
return 0;
}
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
34
How is that possible the ’noise’ gets appeared if we just add a constant value and subtract it?
Luckily, I have some assembly knowledge, and I can get into the x86-64 code:
...
cvtss2sd xmm1 , DWORD PTR -4[ rbp]
cvtss2sd xmm0 , DWORD PTR -4[ rbp]
lea rdi , .LC5[rip]
mov eax , 2
call printf@PLT
mov eax , DWORD PTR -4[ rbp]
movd xmm0 , eax
call print_float_in_binary
mov edi , 10
call putchar@PLT
mov edi , 61
call feclearexcept@PLT
cvtss2sd xmm1 , DWORD PTR -4[ rbp]
movsd xmm0 , QWORD PTR .LC6[rip]
addsd xmm0 , xmm1
cvtsd2ss xmm0 , xmm0
movss DWORD PTR -4[ rbp], xmm0
mov edi , 32
call fetestexcept@PLT
test eax , eax
je .L8
lea rdi , .LC7[rip]
call puts@PLT
.L8:
lea rdi , .LC8[rip]
call puts@PLT
cvtss2sd xmm1 , DWORD PTR -4[ rbp]
cvtss2sd xmm0 , DWORD PTR -4[ rbp]
lea rdi , .LC5[rip]
mov eax , 2
call printf@PLT
mov eax , DWORD PTR -4[ rbp]
movd xmm0 , eax
call print_float_in_binary
mov edi , 10
call putchar@PLT
mov edi , 61
call feclearexcept@PLT
cvtss2sd xmm0 , DWORD PTR -4[ rbp]
movsd xmm1 , QWORD PTR .LC6[rip]
subsd xmm0 , xmm1
cvtsd2ss xmm0 , xmm0
movss DWORD PTR -4[ rbp], xmm0
...
.LC6:
.long 2576980378
.long 1069128089
...
But even without that knowledge, we can say something about that code. It uses ’cvtss2sd’ and ’cvtsd2ss’ instructions, which
converts single-precision value to double-precision and back.
These are: ”Convert Scalar Single-Precision Floating-Point Value to Scalar Double-Precision Floating-Point Value” and ”Con-
vert Scalar Double-Precision Floating-Point Value to Scalar Single-Precision Floating-Point Value”.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
35
The ’addsd’ and ’subsd’ instructions are: ”Add Scalar Double-Precision Floating-Point Values” and ”Subtract Scalar Double-
Precision Floating-Point Value”.
(The whole operation is happens in SIMD registers.) Also, the 0.1 constant is encoded as a 64-bit double-precision value (.LC6).
In other words, everything calculates here in double-precision, but stored as single-precision values.
The first FE_INEXACT exception raises when double-precision zero is summed with double-precision 0.1 value and shoved
into single-precision register.
In []:= N[ convertFloatDWORD [16^^3 dcccccd ], 100]
Out []= 0.100000001490116119384765625
This is the best possible approximation of 0.1 in single-precision. Now you convert it to double-precision. You’ll get something
like 0.10000000149011611... But this is not the best possible approximation of 0.1 in double-precision! The best possible
approximation is:
In []:= N[ convertDoubleQWORD [16^^3 fb999999999999a ], 100]
Out []= 0.1000000000000000055511151231257827021181583404541015625
You subtract ≈ 0.1000000000000000055511151... (best possible approximation) from ≈ 0.10000000149011611... and get
≈ 0.00000000149011611... Of course, it gets normalized, the mantissa is shifted and exponent aligned, leaving ≈ 1.490116 ·
10−9 . After converting this to a single-precision value, it left as the ’noise’:
...
0.000000 1.490116e -09 0 01100001 10011001100110011001101 0 x30cccccd
a!=0
...
...
...
.LC8:
.long 2576980378
.long 1069128089
You see, one operand for faddp/fsubp instruction (add/sub) loaded as a 32-bit value (single-precision), but another (which is
constant) as a 64-bit value (double-precision).
The solution: switching ’float’ to ’double’ in that code would eliminate problem.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
36
A well-known introduction is What Every ComputerScientist Should Know About Floating-Point Arithmetic by David Goldberg.
The Numerical Recipes book is also recommended, as well as D.Knuth’s TAOCP.
The field is numerical analysis.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 5
Calculus
In []:= ListPlot [{ square11 , new}, PlotStyle -> Evaluate [{ PointSize [0.04] , #} & /@ {
Blue , Red }]]
Red dots are the dots been added during growth of the square from 10 to 11. There are 2x of them (or 22 if x=11), and this is
exactly the first derivative of x2 :
Now to get amount of new dots that will be added if the square growed from 100 to 101, you just calculate 2 · 101 − 1.
5.1.2 Cube
Now let’s see what’s with cube.
When it grows, 3 planes are added at 3 sides after each iteration. Each plane is essentially a square with side of x. Hence,
≈ 3x2 ”dots” are ”added” at each iteration.
37
38
3
And this is indeed the derivative of x :
5.1.3 Line
What if your figurine is just a one-dimensional line? It grows by 1 at each iteration:
5.1.4 Circle
Here I’m generating dots inside of two circles, using Pythagorean theorem. There are circles we have with r=15 and r=16:
In []:= incircle15 [ coord_ ] := Abs[ coord [[1]]]^2 + Abs[ coord [[2]]]^2 <= 15^2
In []:= incircle16 [ coord_ ] := Abs[ coord [[1]]]^2 + Abs[ coord [[2]]]^2 <= 16^2
How many dots are added when a circle grows from r=15 to r=16?
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
39
You see, these new dots are like circle circumference in fact.
Indeed, the first derivative of the circle area function is actually its circumference:
Let’s check:
5.1.5 Cylinder
Cylinder is easy. You can say that at each iteration, a new circle is just added at top (or bottom) of it. Indeed, the derivative of
cylinder’s volume function is just an area of circle with the same radius:
5.1.6 Sphere
When a 3D-sphere grows, you can say that 4 disks with the new radius are added into it:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
40
I can show this graphically. Imagine you have a ball and you want to make it bigger. Naive approach is to cut it using knife in
3 planes and insert 3 disks (with the new radius) in between, like:
5.1.7 Conclusion
In other words, derivative is a function that can determine what is added to a function’s result, when its input gets incremented
by 1.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 6
Prime numbers
Prime numbers are the numbers which has no divisors except itself and 1. This can be represented graphically.
Let’s take 9 balls or some other objects. 9 balls can be arranged into rectangle:
ooo
ooo
ooo
So are 12 balls:
oooo
oooo
oooo
Or:
ooo
ooo
ooo
ooo
Or:
o
o
o
o
o
o
o
It’s not possible to form a rectangle using 7 balls, or 11 balls or any other prime number.
The fact that balls can be arranged into rectangle shows that the number can be divided by the number which is represented
by height and width of rectangle. Balls of prime number can be arranged vertically or horizontally, meaning, there are only
two divisors: 1 and the prime number itself.
41
42
By the fundamental theorem of arithmetic, any (composite) number can be represented as a product of prime numbers, in
only one single way.
So the composite number phrase means that the number is composed of prime numbers.
Let’s factor 100 in Wolfram Mathematica:
Listing 6.1: Wolfram Mathematica
In []:= FactorInteger [100]
Out []= {{2, 2}, {5, 2}}
This mean that 100 can be constructed using 2 and 5 prime numbers (22 · 52 ):
Listing 6.2: Wolfram Mathematica
In []:= 2^2*5^2
Out []= 100
...........
oooo ... oo
oooo ... oo
You know how many balls are here, but the problem is to get width and/or height.
Let’s find the first 5 prime numbers, each number for each character:
Listing 6.4: Wolfram Mathematica
In []:= Map[Prime [#] &, Range [5]]
Out []= {2, 3, 5, 7, 11}
Build a huge number using prime numbers as bases and ASCII codes as exponents, then get a product of all them (272 · 3101 ·
5108 · 7108 · 11111 ):
Listing 6.5: Wolfram Mathematica
In []:= tmp = 2^72*3^101*5^108*7^108*11^111
Out []= \
1649465578065933994718255257642275679479006861206428830641826551739434\
9344066214616222018844835866267141943107823334187149334898562231349428\
5708281252457614466981636618940129599457183300076472809826225406689893\
5837524252859632074600687844523389231265776082000229507684707641601562\
5000000000000000000000000000000000000000000000000000000000000000000000\
000
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
43
A first number in each pair is prime number and the second is exponent. Get the text string back:
That allows to have some fun. Let’s add exclamation point to the end of string by manipulating only the big number. ASCII
code of exlamation point is 33. The next prime number after 11 is 13. So add it (by multiplying by 1333 ):
Wow, that works. Will it be possible to remove one ’l’ character from the string at the third position? The ’l’ has the ASCII code
of 108 and it is exponent for two prime numbers in our expression: 5 (first ’l’) and 7 (second ’l’).
To knock out the character, we divide the big number by the corresponding prime number with the exponent of 108 (divide
by 5108 ):
Then let’s set the number at fourth position to 123. The fourth prime number is 7 (the percent sign in Mathematica denotes
the last result):
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
44
Listing 6.12: Wolfram Mathematica
In []:= tmp = tmp *7^123
Out []= 26557071110804040505330743411815438275701018334410643480070773\
5780279761186999642944265644421128096489029
Then let’s set the number at fifth position to 456. The fifth prime number is 11:
Then let’s decrement the number at fourth position, the fourth prime number is 7:
Let’s factor the composite number and get all the numbers we set inside container (1, 122, 456):
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
45
If you choose 5 as number of cogs on one of cogwhell and you have 10 or 15 or 20 chain elements, each cog on cogwheel will
meet some set of the same chain elements. For example, if there are 5 cogs on cogwheel and 20 chain elements, each cog
will meet only 4 chain elements and vice versa: each chain element has its own cog on cogwheel. This is bad because both
cogwheel and chain will run out slightly faster than if each cog would interlock every chain elements at some point. To reach
this, number of cogs and chain elements could be coprime numbers, like 5 and 11, or 5 and 23. That will make each chain
element interlock each cog evenly, which is better.
( https://en.wikipedia.org/wiki/Semiprime )
We’ve got 3, which is not 1, indicating the 10 is not prime. On the other hand, 11 is prime:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
46
This method is not perfect, some composite p numbers can lead to 1, for example p=1105, but can be used as a method to
sieve vast amount of prime numbers candidates.
In []:= n = p*q
Out []= 3827
3827 is published as public key, named public key modulus or modulo. It is semiprime. There is also public key exponent e,
which is not secret, is often 65537, but we will use 17 to keep all results tiny.
Now The Sender wants to send a message (123 number) to The Receiver and he/she uses one-way function:
3060 is encrypted message, which can be decrypted only using p and q values separately. This is one-way function, because
only a part of exponentiation result is left. One and important consequence is that even The Sender can’t decrypt it. This is
why you can encrypt a piece of text in PGP/GnuPG to someone using his/her public key, but can’t decrypt it. Perhaps, that’s
how CryptoLockers works, making impossible to decrypt the files.
To recover message (123), p and q values must be known.
First, we get the result of Euler’s totient function (p − 1)(q − 1) (this is the point where p and q values are needed):
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
47
Now we calculating decrypting exponent using multiplicative modulo inverse (multiplicative inverse was also described in
here (7) (e−1 (mod totient = (p − q)(q − 1))):
So the d exponent forms another one-way function, restoring the work of what was done during encryption.
This allows...
Or in Mathematica:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
48
6.4.6 Breaking RSA
We can try to factor n semiprime (or RSA modulus) in Mathematica:
And we getting correct p and q, but this is possible only for small values. When you use some big ones, factorizing is extremely
slow, making RSA unbreakable, if implemented correctly.
The bigger p, q and n numbers, the harder to factorize n, so the bigger keys in bits are, the harder it to break.
6.4.7 The difference between my simplified example and a real RSA algorithm
In my example, public key is n = pq (product) and secret key are p and q values stored separately. This is not very efficient,
to calculate totient and decrypting exponent each time. So in practice, a public key is n and e, and a secret key is at least n
and d, and d is stored in secret key precomputed.
For example, here is my PGP public key1 :
dennis@ ...:∼$ gpg --export - options export -reset -subkey - passwd --export -secret - subkeys
0 x3B262349 \! | pgpdump
Old: Secret Key Packet (tag 5) (533 bytes )
Ver 4 - new
Public key creation time - Tue Jun 30 02:08:38 EEST 2015
Pub alg - RSA Encrypt or Sign(pub 1)
RSA n(4096 bits) - ...
RSA e(17 bits) - ...
...
... so there are available openly big (4096 bits) n and e (17 bits).
And here is my PGP secret key:
dennis@ ...:∼$ gpg --export - options export -reset -subkey - passwd --export -secret - subkeys
0 x55B5C64F \! | pgpdump
gpg: about to export an unprotected subkey
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
49
...
Now he publishes n = pq (3827), e (17 in our example), the message (456) and the signature (2282). Some other Consumers
can verify its signature using e exponent and n:
... this is another illustration that e and d exponents are complement each other, by modulo totient = (p − 1)(q − 1).
The signature can only be generated with the access to p and q values, but it can be verified using product (n = pq) value.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 7
Modulo arithmetics
One of the earliest practical applications of number theory occurs in gears. If two cogs are placed together
so that their teeth mesh, and one cog has m teeth, the other n teeth, then the movement of the cogs is related
to these numbers. For instance, suppose one cog has 30 teeth and the other has 7. If I turn the big cog exactly
once, what does the smaller one do? It returns to the initial position after 7, 14, 21 and 28 steps. So, the final
2 steps, to make 30, advance it by just 2 steps. This number turns up because it is the remainder on dividing
30 by 7. So the motion of cogs is a mechanical representation of division with remainder, and this is the basis
of modular arithmetic.
( Ian Stewart – Taming the Infinite: The Story of Mathematics from the First Numbers to Chaos Theory )
Modular arithmetic is an environment where all values are limited by some number (modulo). Many textbooks has clock as
example. Let’s imagine old mechanical analog clock. There hour hand points to one of number in bounds of 0..11 (zero is
usually shown as 12). What hour will be if to sum up 10 hours (no matter, AM or PM) and 4 hours? 10+4 is 14 or 2 by modulo 12.
Naively you can just sum up numbers and subtract modulo base (12) as long as it’s possible.
Modern digital watch shows time in 24 hours format, so hour there is a variable in modulo base 24. But minutes and seconds
are in modulo 60 (let’s forget about leap seconds for now).
Another example is US imperial system of measurement: human height is measured in feets and inches. There are 12 inches
in feet, so when you sum up some lengths, you increase feet variable each time you’ve got more than 12 inches.
Another example I would recall is password cracking utilities. Often, characters set is defined in such utilities. And when you
set all Latin characters plus numbers, you’ve got 26+10=36 characters in total. If you brute-forcing a 6-characters password,
you’ve got 6 variables, each is limited by 36. And when you increase last variable, it happens in modular arithmetic rules: if
you got 36, set last variable to 0 and increase penultimate one. If it’s also 36, do the same. If the very first variable is 36, then
stop. Modular arithmetic may be very helpful when you write multi-threading (or distributed) password cracking utility and
you need to slice all passwords space by even parts.
This is yet another application of modulo arithmetic, which many of us encountered in childhood.
Given a counting rhyme, like:
Eeny , meeny , miny , moe ,
Catch a tiger by the toe.
If he hollers , let him go ,
Eeny , meeny , miny , moe.
( https://en.wikipedia.org/wiki/Eeny,_meeny,_miny,_moe )
... predict, who will be choosen.
50
51
If I’m correct, that rhyme has 16 items, and if a group of kids constist of, say, 5 kids, who will be choosen? 16 mod 5 = 1, meaning,
the next kid after the one at whom counting had begun.
Or 7 kids, 16 mod 7 = 2. Count two kids after the first one.
If you can calculate this quickly, you can probably get an advantage by choosing a better place within a circle...
Now let’s recall old mechanical counters which were widespread in pre-digital era:
Figure 7.1: The picture was stolen from http://www.featurepics.com/ — sorry for that!
This counter has 6 wheels, so it can count from 0 to 106 − 1 or 999999. When you have 999999 and you increase the counter,
it will resetting to 000000— this situation is usually understood by engineers and computer programmers as overflow. And
if you have 000000 and you decrease it, the counter will show you 999999. This situation is often called “wrap around”. See
also: http://en.wikipedia.org/wiki/Integer_overflow.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
52
Usually, almost all sane computer programmers works with variables as they never wrapping around and value here is al-
ways in some limits which are defined preliminarily. However, this implicit division operation or ”wrapping around” can be
exploited usefully.
No matter what compiler do you use, you can think about it as 10 is subtraced from rand() result, as long as there is still a
number bigger than 10. Hence, result is remainder of division of rand() result by 10.
One nasty consequence is that neither 0x8000 nor 0x80000000 cannot be divided by 10 evenly, so you’ll get some numbers
slightly more often than others.
I tried to calculate in Mathematica. Here is what you get if you write <i>rand()
In []:= Counts [Map[Mod [#, 3] &, Range [0, 16^^8000 - 1]]]
Out []= <|0 -> 10923 , 1 -> 10923 , 2 -> 10922| >
…now larger part of numbers happens slightly seldom, these are 68...99.
This is sometimes called modulo bias. It’s perhaps acceptable for videogames, but may be critical for scientific simulations,
including Monte Carlo method.
Constructing a PRNG1 with uniform distribution may be tricky, there are couple of methods:
http://www.reddit.com/r/algorithms/comments/39tire/using_a_01_generator_generate_a_random_number/,
http://www.prismmodelchecker.org/casestudies/dice.php.
1 Pseudorandom number generator
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
53
7.1.3 Reverse Engineering
Not uncommon in 32-bit x86 code, you may find an instruction like:
It adds 0xFFFFFFCD to a value in a register (or variable). But why? What for?
Let’s find out in Wolfram Mathematica:
In other words, this instruction subtracts 51 from a variable! Somehow, a compiler (MSVC in this case) decided that addition
operation would be better (or faster?) than subtraction operation. I’m not sure why a compiler can this, but MSVC do this a
lot. Anyway, this is subtraction, and these operations are equivalent to each other.
It’s like adding 0xffffffff which is equivalent to subtracting 1. However, subtracting 0xffffffff is equivalent to adding 1.
Same story for a simple Caesar cipher: you may even don’t know what operation was used during encryption: addition or
subtraction. By cryptanalysis you can find two keys: for addition and subtraction.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
54
*buf ++ = hex[val & 0xf];
}
*buf = '\0';
return buffer ;
}
( https://github.com/git/git/blob/aa1c6fdf478c023180e5ca5f1658b00a72592dc6/hex.c )
This function returns a pointer to the string containing hexadecimal representation of SHA1 digest
(like ”4e1243bd22c66e76c2ba9eddc1f91394e57f9f83”). But this is plain C and you can calculate SHA1 for some block, get
pointer to the string, then calculate SHA1 for another block, get pointer to the string, and both pointers are still points to the
same string buffer containing the result of the second calculation. As a solution, it’s possible to allocate/deallocate string
buffer each time, but more hackish way is to have several buffers (4 are here) and fill the next each time. The bufno variable
here is a buffer counter in 0..3 range. Its value increments each time, and its value is also always kept in limits by AND operation
(3 & ++bufno).
The author of this piece of code (seemingly Linus Torvalds himself) went even further and forgot (?) to initialize bufno counter
variable, which will have random garbage at the function start. Indeed: no matter, which buffer we are starting each time!
This can be mistake which isn’t affect correctness of the code, or maybe this is left so intentionally – I don’t know.
for (...)
{
do_something ();
cnt=cnt +1;
if (( cnt % 1000) ==0);
print (" heartbeat : %d processed ", cnt);
This is how you can use remainder from division for occasional logging: printing will happen once in 1000 steps.
However, another way is:
cnt =0
for (...)
{
do_something ();
cnt=cnt +1;
if (( cnt &0 xfff)==0);
print (" heartbeat : %d processed ", cnt);
Now printing will occur once in 0x1000 or 4096 steps. This is also getting remainder from division.
int main ()
{
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
55
printf ("%d\n", div17 (1700) ); // result=100
printf ("%d\n", div17 (34)); // result=2
printf ("%d\n", div17 (2091) ); // result=123
};
How it works?
Let’s imagine, we work on 4-bit CPU, it has 4-bit registers, each can hold a value in 0..15 range.
Now we want to divide by 3 using multiplication. Let’s find modulo inverse of 3 using Wolfram Mathematica:
In []:= PowerMod [3, -1, 16]
Out []= 11
The ”magic number” for division by 3 is 11. Multiply by 11 instead of dividing by 3 and you’ll get a result (quotient).
This works, let’s divide 6 by 3. We can now do this by multiplying 6 by 11, this is 66=0x42, but on 4-bit register, only 0x2 will
be left in register (0x42 ≡ 2 mod 24 ). Yes, 2 is correct answer, 6/3=2.
Let’s divide 3, 6 and 9 by 3, by multiplying by 11 (m).
| 1 2 3 4 5 6 7 8 9 a b c de f 0 | 1 2 . . . f0 |123456789 abcdef0 | 1 2 . . . f0 |123456789 abcdef0 | 1 2 . . . f0 |123456789 abcdef0 |
m= 1 1 | *********** | ... | | ... | | ... | |
3 / 3 3m=33 | **************** | ** . . . **|* | ... | | ... | |
6 / 3 6m=66 | **************** | ** . . . ** | **************** | ** . . . **|** | ... | |
9 / 3 9m=99 | **************** | ** . . . ** | **************** | ** . . . ** | **************** | ** . . . **|*** |
A “protruding” asterisk(s) (“*”) in the last non-empty chunk is what will be left in 4-bit register. This is 1 in case of 33, 2 if 66, 3
if 99.
In fact, this ”protrusion” is defined by 1 in the equation we’ve solved. Let’s replace 1 with 2:
In []:= FindInstance [3 m == 16 k + 2, {m, k}, Integers ]
Out []= {{m -> 6, k -> 1}}
Now the new ”magic number” is 6. Let’s divide 3 by 3. 3*6=18=0x12, 2 will be left in 4-bit register. This is incorrect, we have 2
instead of 1. 2 asterisks are ”protruding”. Let’s divide 6 by 3. 6*6=36=0x24, 4 will be left in the register. This is also incorrect,
we now have 4 ”protruding” asterisks instead of correct 2.
Replace 1 in the equation by 0, and nothing will ”protrude”.
7.3.1 No remainder?
Now the problem: this only works for dividends in 3x form, i.e., which can be divided by 3 with no remainder. Try to divide 4
by 3, 4*11=44=0x2c, 12 will be left in register, this is incorrect. The correct quotient is 1.
We can also notice that the 4-bit register is ”overflown” during multiplication twice as much as in ”incorrect” result in low 4
bits.
Here is what we can do: use only high 4 bits and drop low 4 bits. 4*11=0x2c and 2 is high 4 bits. Divide 2 by 2, this is 1.
Let’s ”divide” 8 by 3. 8*11=88=0x58. 5/2=2, this is correct answer again.
Now this is the formula we can use on our 4-bit CPU to divide numbers by 3: ”x*3 » 4 / 2” or ”x*3 » 5”. This is the same as
almost all modern compilers do instead of integer division, but they do this for 32-bit and 64-bit registers.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
56
int f(int a)
{
return a/9;
};
push ebp
mov ebp , esp
mov ecx , [ebp+ arg_0 ]
mov edx , 954437177 ; 38E38E39h
mov eax , ecx
imul edx
sar edx , 1
mov eax , ecx
sar eax , 1Fh
mov ecx , edx
sub ecx , eax
mov eax , ecx
pop ebp
retn
f endp
Given the fact that division by 2n is very fast (using shifts), we now should find that M agicN umber, for which the following
equation will be true: 2n = 9 · M agicN umber.
Division by 232 is somewhat hidden: lower 32-bit of product in EAX is not used (dropped), only higher 32-bit of product (in
EDX) is used and then shifted by additional 1 bit.
954437177 232+1
In other words, the assembly code we have just seen multiplicates by 232+1 , or divides by 954437177 . To find a divisor
we just have to divide numerator by denominator. Using Wolfram Alpha, we can get 8.99999999.... as result (which is close
to 9).
Many people miss “hidden” division by 232 or 264 , when lower 32-bit part (or 64-bit part) of product is not used. This is why
division by multiplication is difficult to understand at the beginning.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
57
7.5 Reversible linear congruential generator
LCG2 PRNG is very simple: just multiply seed by some value, add another one and here is a new random number. Here is how
it is implemented in MSVC (the source code is not original one and is reconstructed by me):
uint32_t state ;
uint32_t rand ()
{
state =state *214013+2531011;
return (state > >16) &0 x7FFF ;
};
The last bit shift is attempt to compensate LCG weakness and we may ignore it so far. Will it be possible to make an inverse
function to rand(), which can reverse state back? First, let’s try to think, what would make this possible? Well, if state internal
variable would be some kind of BigInt or BigNum container which can store infinitely big numbers, then, although state is
increasing rapidly, it would be possible to reverse the process. But state isn’t BigInt/BigNum, it’s 32-bit variable, and summing
operation is easily reversible on it (just subtract 2531011 at each step). As we may know now, multiplication is also reversible:
just multiply the state by modular multiplicative inverse of 214013!
# include <stdio .h>
# include <stdint .h>
uint32_t state ;
void next_state ()
{
state =state *214013+2531011;
};
void prev_state ()
{
state =state -2531011; // reverse summing operation
state =state *3115528533; // reverse multiply operation. 3115528533 is modular inverse
of 214013 in 232 .
};
prev_state ();
printf ("state =%d\n", state );
prev_state ();
printf ("state =%d\n", state );
prev_state ();
printf ("state =%d\n", state );
};
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
58
state =1255958651
state = -456978094
state =1255958651
state = -1650445800
state =12345
It’s hard to find a real-world application of reversible LCG, but this could be the one: a media player with forward/backward
buttons. Once shuffle is clicked, random number is generated (number of item to be played). User clicks forward: get a new
random number by calculating the next state. User clicks backward: get it by calculating the previous state. Thus, a user could
navigate through some ”virtual” non-existent (but consistent) playlist, which is even not present in media player’s memory!
Or maybe you could navigate a huge labyrinth, so huge that you can’t record your path in full. But you can pick some seed for
LCG and take left/right turns according to PRNG results. You could go back by reversing LCG’s state.
while (r!=0)
{
int quotient = old_r /r;
tmp=r; r= old_r - quotient *r; old_r =tmp;
tmp=s; s= old_s - quotient *s; old_s =tmp;
tmp=t; t= old_t - quotient *t; old_t =tmp;
};
printf ("GCD: %d\n", old_r );
if ( old_r !=1)
{
printf ("%d and %d are not coprimes !\n", a, b);
return ;
};
printf (" Bézout coefficients : %d %d\n", old_s , old_t );
printf (" quotients by the GCD (s,t): %d %d\n", s, t);
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
59
void main ()
{
EGCD (3, 0 x10000 );
};
GCD: 1
Bézout coefficients : -21845 1
quotients by the GCD (s,t): 65536 -3
corrected coefficients : 43691(0 xaaab ) 2(0 x2)
This is in fact the magic number for division by 5. And there is 3 instead of 2 in the SHR instruction, so the result is divided by
2.
Extended Euclidean algorithm is probably an efficient way of finding magic number, but needless to say, this equation can be
solved using other ways. In “SAT/SMT by Example”3 you can find a method of finding ”magic number” using SMT-solver.
3 https://sat-smt.codes/
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 8
Probability
% wget https :// cdn. kernel .org/pub/ linux / kernel /v2 .3/ linux -2.3.3. tar.bz2
One of Linux kernel patches in compressed form has the “Linux” word itself (not a bad self-referential joke):
% wget https :// cdn. kernel .org/pub/ linux / kernel /v4.x/ testing /patch -4.6 - rc4.gz
Other English words I’ve found in other compressed Linux kernel trees:
linux -4.6.2. tar.gz: [ maybe ] at 0 x68e78ec
linux -4.10.14. tar.xz: [ OCEAN ] at 0 x6bf0a8
linux -4.7.8. tar.gz: [ FUNNY ] at 0 x29e6e20
linux -4.6.4. tar.gz: [ DRINK ] at 0 x68dc314
linux -2.6.11.8. tar.bz2: [ LUCKY ] at 0 x1ab5be7
linux -3.0.68. tar.gz: [ BOOST ] at 0 x11238c7
linux -3.0.16. tar.bz2: [ APPLE ] at 0 x34c091
linux -3.0.26. tar.xz: [ magic ] at 0 x296f7d9
linux -3.11.8. tar.bz2: [ TRUTH ] at 0 xf635ba
linux -3.10.11. tar.bz2: [ logic ] at 0 x4a7f794
60
61
There is a nice illustration of apophenia and pareidolia (human’s mind ability to see faces in clouds, etc) in Lurkmore, Russian
counterpart of Encyclopedia Dramatica. As they wrote in the article about electronic voice phenomenon1 , you can open any
long enough compressed file in hex editor and find well-known 3-letter Russian obscene word, and you’ll find it a lot: but that
means nothing, just a mere coincidence.
And I was interested in calculation, how big compressed file must be to contain all possible 3-letter, 4-letter, etc, words? In my
naive calculations, I’ve got this: probability of the first specific byte in the middle of compressed data stream with maximal
1 1 1 1
entropy is 256 , probability of the 2nd is also 256 , and probability of specific byte pair is 256·256 = 256 2 . Probabilty of specific
1
triple is 2563 . If the file has maximal entropy (which is almost unachievable, but …) and we live in an ideal world, you’ve got to
have a file of size just 2563 = 16777216, which is 16-17MB. You can check: get any compressed file, and use rafind2 to search
for any 3-letter word (not just that Russian obscene one).
It took ≈ 8-9 GB of my downloaded movies/TV series files to find the word “beer” in them (case sensitive). Perhaps, these
movies wasn’t compressed good enough? This is also true for a well-known 4-letter English obscene word.
My approach is naive, so I googled for mathematically grounded one, and have find this question: “Time until a consecutive
sequence of ones in a random bit sequence” 2 . The answer is: (p−n −1)/(1−p), where p is probability of each event and n is
1
number of consecutive events. Plug 256 and 3 and you’ll get almost the same as my naive calculations.
So any 3-letter word can be found in the compressed file (with ideal entropy) of length 2563 =≈ 17M B, any 4-letter word —
2564 = 4.7GB (size of DVD). Any 5-letter word — 2565 =≈ 1T B.
For the piece of text you are reading now, I mirrored the whole kernel.org website (hopefully, sysadmins can forgive me), and
it has ≈ 430GB of compressed Linux Kernel source trees. It has enough compressed data to contain these words, however, I
cheated a bit: I searched for both lowercase and uppercase strings, thus compressed data set I need is almost halved.
This is quite interesting thing to think about: 1TB of compressed data with maximal entropy has all possible 5-byte chains,
but the data is encoded not in chains itself, but in the order of chains (no matter of compression algorithm, etc).
Now the information for gamblers: one should throw a dice ≈ 42 times to get a pair of six, but no one will tell you, when
exactly this will happen. I don’t remember, how many times coin was tossed in the “Rosencrantz & Guildenstern Are Dead”
movie, but one should toss it ≈ 2048 times and at some point, you’ll get 10 heads in a row, and at some other point, 10 tails
in a row. Again, no one will tell you, when exactly this will happen.
Compressed data can also be treated as a stream of random data, so we can use the same mathematics to determine proba-
bilities, etc.
If you can live with strings of mixed case, like “bEeR”, probabilities and compressed data sets are much lower: 1283 = 2M B
for all 3-letter words of mixed case, 1284 = 268M B for all 4-letter words, 1285 = 34GB for all 5-letter words, etc.
Moral of the story: whenever you search for some patterns, you can find it in the middle of compressed blob, but that means
nothing else then coincidence. In philosophical sense, this is a case of selection/confirmation bias: you find what you search
for in “The Library of Babel” 3 .
Also, a citation from a Marten Gardner’s book about randomness of π:
So far pi has passed all statistical tests for randomness. This is disconcerting to those who feel that a
curve so simple and beautiful as the circle should have a less-disheveled ratio between the way around and
the way across, but most mathematicians believe that no pattern or order of any sort will ever be found in pi’s
decimal expansion. Of course the digits are not random in the sense that they represent pi, but then in this
sense neither are the million random digits that have been published by the Rand Corporation of California.
They too represent a single number, and an integer at that. If it is true that the digits in pi are random, perhaps
we are justified in stating a paradox somewhat similar to the assertion that if a group of monkeys pound long
enough on typewriters, they will eventually type all the plays of Shakespeare. Stephen Barr has pointed out
that if you set no limit to the accuracy with which two bars can be constructed and measured, then those
two bars, without any markings on them, can communicate the entire Encyclopaedia Britannica. One bar is
taken as unity. The other differs from unity by a fraction that is expressed as a very long decimal. This decimal
codes the Britannica by the simple process of assigning a different number (excluding zero as a digit in the
number) to every word and mark of punctuation in the language. Zero is used to separate the code numbers.
Obviously the entire Britannica can now be coded as a single, but almost inconceivably long, number. Put
a decimal point in front of this number, add 1, and you have the length of the second of Barr’s bars. Where
1 http://archive.is/gYnFL
2 http://math.stackexchange.com/questions/27989/time-until-a-consecutive-sequence-of-ones-in-a-random-bit-sequence/
27991#27991
3 A short story by Jorge Luis Borges
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
62
does pi come in? Well, if the digits in pi are really random, then somewhere in this infinite pie there should
be a slice that contains the Britannica; or, for that matter, any book that has been written, will be written, or
could be written.
A sequence of six 9’s occurs in the decimal representation of the number pi (π), starting at the 762nd
decimal place. It has become famous because of the mathematical coincidence and because of the idea
that one could memorize the digits of π up to that point, recite them and end with ”nine nine nine nine nine
nine and so on”, which seems to suggest that π is rational. The earliest known mention of this idea occurs in
Douglas Hofstadter’s 1985 book Metamagical Themas, where Hofstadter states
I myself once learned 380 digits of π, when I was a crazy high-school kid. My never-attained ambition
was to reach the spot, 762 digits out in the decimal expansion, where it goes ”999999”, so that I could recite
it out loud, come to those six 9’s, and then impishly say, ”and so on!”
— Douglas Hofstadter, Metamagical Themas
( https://en.wikipedia.org/wiki/Six_nines_in_pi )
What are most chess moves played after 1.e4 e5 2.Nf3 Nf6? A big database of chess games can be queried, showing statistics:
( from https://chess-db.com/ )
Statistics shown is just number of games, where a corresponding 3rd move was played.
The same database can be made for natural language words.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
63
I wrote a Python script, took Sherlock Holmes stories from Gutenberg library...
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
sentences =data. lower (). replace ('\r',' '). replace ('\n',' '). replace ('?','.'). replace ('
!','.'). replace ('“','.'). replace ('”','.'). replace ("\"","."). replace ('‘',' ').
replace ('-',' '). replace ('’',' '). replace ('\'',' '). split (".")
for s in sentences :
words =s. replace (',',' '). split (" ")
words = remove_empty_words ( words )
if len(words )==0:
continue
for i in range (len(words )):
if i >=1:
update_occ (first , words [i -1] , words [i])
if i >=2:
update_occ (second , words [i -2]+" "+ words [i -1] , words [i])
"""
print (" first table :")
for k in first :
print (k)
# https :// stackoverflow .com/ questions /613183/ how -do -i-sort -a-dictionary -by -value
s= sorted ( first [k]. items () , key= operator . itemgetter (1) , reverse =True)
print (s [:20])
print ("")
"""
"""
print (" second table :")
for k in second :
print (k)
# https :// stackoverflow .com/ questions /613183/ how -do -i-sort -a-dictionary -by -value
s= sorted ( second [k]. items () , key= operator . itemgetter (1) , reverse =True)
print (s [:20])
print ("")
"""
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
64
# https://docs.python.org/3/library/random.html#random.choice
def gen_random_from_tbl (t):
return random . choices (list(t.keys ()), weights =list(t. values ()))[0]
text_len =len(text)
last_idx =text_len -1
And here are some 1st-order Markov chains. First part is a first word. Second is a list of words + probabilities of appearance of
each one, in the Sherlock Holmes stories. However, probabilities are in form of words’ numbers.
In other word, how often each word appears after a word?
return
[('to ', 28) , ('or ', 12) , ('of ', 8) , ('the ', 8) , ('for ', 5) , ('with ', 4) , ('by ', 4) ,
('journey ', 4) , ('and ', 3) , ('when ', 2) , ('ticket ', 2) , ('from ', 2) , ('at ', 2) , ('
you ', 2), ('i', 2) , ('since ', 1) , ('on ', 1) , ('caused ', 1) , ('but ', 1) , ('it ', 1)]
of
[('the ', 3206) , ('a', 680) , ('his ', 584) , ('this ', 338) , ('it ', 304) , ('my ', 295) , ('
course ', 225) , ('them ', 167) , ('that ', 138) , ('your ', 137) , ('our ', 135) , ('us ',
122) , ('her ', 116) , ('him ', 111) , ('an ', 105) , ('any ', 102) , ('these ', 92) , ('
which ', 89) , ('all ', 82) , ('those ', 75)]
by
[('the ', 532) , ('a', 164) , ('his ', 67) , ('my ', 39) , ('no ', 34) , ('this ', 31) , ('an ',
30) , ('which ', 29) , ('your ', 21) , ('that ', 19) , ('one ', 17) , ('all ', 17) , ('jove ',
16) , ('some ', 16) , ('sir ', 13) , ('its ', 13) , ('him ', 13) , ('their ', 13) , ('it ',
11) , ('her ', 10)]
this
[('is ', 177) , ('agreement ', 96) , ('morning ', 81) , ('was ', 61) , ('work ', 60) , ('man ',
56) , ('case ', 43) , ('time ', 39) , ('matter ', 37) , ('ebook ', 36) , ('way ', 36) , ('
fellow ', 28) , ('room ', 27) , ('letter ', 24) , ('one ', 24) , ('i', 23) , ('young ', 21) ,
('very ', 20) , ('project ', 18) , (' electronic ', 18)]
no
[('doubt ', 179) , ('one ', 134) , ('sir ', 61) , ('more ', 56) , ('i', 55) , ('no ', 42) , ('
other ', 36) , ('means ', 36) , ('sign ', 34) , ('harm ', 23) , ('reason ', 22) , ('
difficulty ', 22) , ('use ', 21) , ('idea ', 20) , ('longer ', 20) , ('signs ', 18) , ('good
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
65
', 17) , ('great ', 16) , ('trace ', 15) , ('man ', 15)]
return of
[(' sherlock ', 6) , ('lady ', 1) , ('the ', 1)]
for the
[('use ', 22) , ('first ', 12) , ('purpose ', 9) , ('sake ', 8) , ('rest ', 7) , ('best ', 7) ,
('crime ', 6), ('evening ', 6) , ('ebooks ', 6) , ('limited ', 6) , ('moment ', 6) , ('day
', 5), ('future ', 5) , ('last ', 5) , ('world ', 5) , ('time ', 5) , ('loss ', 4) , ('
second ', 4) , ('night ', 4) , ('remainder ', 4)]
use of
[('the ', 15) , ('anyone ', 12) , ('it ', 6) , ('project ', 6) , ('and ', 6) , ('having ', 2) ,
('this ', 1) , ('my ', 1) , ('a', 1) , ('horses ', 1) , ('some ', 1) , ('arguing ', 1) , ('
troubling ', 1) , (' artificial ', 1) , ('invalids ', 1) , ('cocaine ', 1) , ('disguises ',
1), ('an ', 1) , ('our ', 1)]
you may
[('have ', 17) , ('copy ', 13) , ('be ', 13) , ('do ', 9) , ('choose ', 7) , ('use ', 6) , ('
obtain ', 6) , ('convert ', 6) , ('charge ', 6) , ('demand ', 6) , ('remember ', 5) , ('find
', 5), ('take ', 4) , ('think ', 3) , ('possibly ', 3) , ('well ', 3) , ('know ', 3) , ('not
', 3), ('say ', 3) , ('imagine ', 3)]
the three
[('of ', 8) , ('men ', 4) , ('students ', 2) , ('glasses ', 2) , ('which ', 1) , ('strips ', 1) ,
('is ', 1) , ('he ', 1) , ('gentlemen ', 1) , (' enterprising ', 1) , ('massive ', 1) , ('
quarter ', 1), ('randalls ', 1) , ('fugitives ', 1) , ('animals ', 1) , ('shrieked ', 1) ,
('other ', 1), ('murderers ', 1) , ('fir ', 1) , ('at ', 1)]
it was
[('a', 179) , ('the ', 78) , ('not ', 68) , ('only ', 40) , ('in ', 30) , ('evident ', 28) , ('
that ', 28) , ('all ', 25) , ('to ', 21) , ('an ', 18) , ('my ', 17) , ('at ', 17) , ('
impossible ', 17) , ('indeed ', 15) , ('no ', 15) , ('quite ', 15) , ('he ', 14) , ('of ',
14) , ('one ', 12) , ('on ', 12)]
Now two words is a key in dictionary. And we see here an answer for the question ”how often each words appears after a
sequence of two words?”
Now let’s generate some rubbish:
it is just to the face of granite still cutting the lower part of the shame which i
would draw up so as i can t have any visitors if he is passionately fond of a dull
wrack was drifting slowly in our skin before long vanished in front of him of
this arch rebel lines the path which i had never set foot in the same with the
heads of these have been supplemented or more waiting to tell me all that my
assistant hardly knowing whether i had 4 pounds a month however is foreign to the
other hand were most admirable but because i know why he broke into a curve by the
crew had thought it might have been on a different type they were about to set
like a plucked chicken s making the case which may help to play it at the door and
called for the convenience of a boat sir maybe i could not be made to draw some
just inference that a woman exactly corresponding to the question was how a miners
camp had been dashed savagely against the rock in front of the will was drawn
across the golden rays and it
it is the letter was composed of a tall stout official had come the other way that
she had been made in this i expect that we had news that the office of the mud
settling or the most minute exactness and astuteness represented as i come to see
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
66
the advantages which london then and would i could not speak before the public
domain ebooks in compliance with any particular paper edition as to those letters
come so horribly to his feet upon the wet clayey soil but since your wife and of
such damage or cannot be long before the rounds come again whenever she might be
useful to him so now my fine fellow will trouble us again and again and making a
little wizened man darted out of the baskervilles *** produced by roger squires
updated editions will be the matter and let out forty three diamonds of the attack
we carried him into the back and white feather were but a terrible fellow he is
not for your share in an instant holmes clapped his hands and play with me anyhow
i d ha known you under that name in a considerable treasure was hid for no other
it is the unofficial force the shutter open but so far as to what i say to me like
that which is rather too far from at ease for i knew that we were driving ; but
soon it came just as he opened it myself i was not a usual signal between you and
you thought it might be removed to a magistrate than upon the luminous screen of
the one word would he have wanted there are business relations between him and we
listened to his eyes to the spot as soon as their master s affairs of life since
she was but one day we hoped would screen me from under his arm chair of shining
red leather chair his flask in his chair and gave me his name is sherlock holmes
took each face of a barmaid in bristol and marry her but learned from dr
it is selden the criminal or lunatic who had left a match might mar my career had
reached the tower of the crime was committed before twelve foolish tradesmen in a
bad lot though the authorities are excellent at amassing facts though what its
object might be a better nerve for the creature flapped and struggled and writhed
his hands in her bosom lady hilda was down on the table and the curious will so
suddenly upon the floor were now nearly five thousand pounds will be impossible
but i struck him down and roared into the shadow of the thing seemed simplicity
itself said i you seem most fortunate in having every characteristic of the place
is deserted up to keep some clandestine appointment and found as i have no desire
to settle this little matter of fact of absolute ignorance and he with a similar
pearl without any light upon what he had found ourselves at the time and my heart
sank for barrymore at the close of november and holmes fears came to think over
the afghan campaign yet shown by this time at which he now and i have seen his
death this morning he walked straight into
By first look, these pieces of text are visually OK, but it is senseless. Some people (including me) find it amusing.
Spammers also use this technique to make email message visually similar to a meaningful text, albeit it is not meaningful at
all, rather absurdic and funny.
8.2.2 Autocomplete
It’s surprising how easy this can be turned into something rather practically useful.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import operator
from collections import defaultdict
sentences =data. lower (). replace ('\r',' '). replace ('\n',' '). replace ('?','.'). replace ('
!','.'). replace ('“','.'). replace ('”','.'). replace ("\"","."). replace ('‘',' ').
replace ('-',' '). replace ('’',' '). replace ('\'',' '). split (".")
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
67
# (I would use list of strings here as key, but list in not hashable)
# val=dict, k: next word; v: occurrences
first ={}
second ={}
third ={}
for s in sentences :
words =s. replace (',',' '). split (" ")
words = remove_empty_words ( words )
if len(words )==0:
continue
for i in range (len(words )):
# only two words available:
if i >=1:
update_occ (first , words [i -1] , words [i])
# three words available:
if i >=2:
update_occ (second , words [i -2]+" "+ words [i -1] , words [i])
# four words available:
if i >=3:
update_occ (third , words [i -3]+" "+words [i -2]+" "+ words [i -1] , words [i])
"""
print (" third table :")
for k in third :
print (k)
# https :// stackoverflow .com/ questions /613183/ how -do -i-sort -a-dictionary -by -value
s= sorted ( third [k]. items () , key= operator . itemgetter (1) , reverse =True)
print (s [:20])
print ("")
"""
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
68
# https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value
s= sorted (t. items () , key= operator . itemgetter (1) , reverse =True)
# take 5 from each sorted table
for pair in s[:5]:
print ("%s %d%%" % (pair [0] , ( float (pair [1])/ total )*100) )
if test_len >=3:
tmp= test_words [last_idx -2]+" "+ test_words [last_idx -1]+" "+ test_words [ last_idx ]
if tmp in third :
print ("* third order . for sequence :",tmp)
print_stat ( third [tmp ])
if test_len >=2:
tmp= test_words [last_idx -1]+" "+ test_words [ last_idx ]
if tmp in second :
print ("* second order . for sequence :", tmp)
print_stat ( second [tmp ])
if test_len >=1:
tmp= test_words [ last_idx ]
if tmp in first :
print ("* first order . for word:", tmp)
print_stat ( first [tmp ])
print ("")
First, let’s also make 3rd-order Markov chains tables. There are some snippets from it:
the return of
[(' sherlock ', 6) , ('lady ', 1) , ('the ', 1)]
the use of
[('the ', 13) , ('anyone ', 12) , ('project ', 6) , ('having ', 2) , ('a', 1) , ('horses ', 1) ,
('some ', 1) , ('troubling ', 1) , (' artificial ', 1) , ('invalids ', 1) , ('disguises ',
1), ('it ', 1)]
of the second
[('stain ', 5) , ('floor ', 3) , ('page ', 1) , ('there ', 1) , ('person ', 1) , ('party ', 1) ,
('day ', 1)]
it was in
[('the ', 9), ('vain ', 5) , ('a', 4) , ('his ', 2) , ('83', 1) , ('one ', 1) , ('motion ', 1) ,
('truth ', 1), ('my ', 1) , ('this ', 1) , ('march ', 1) , ('january ', 1) , ('june ', 1) ,
('me ', 1)]
was in the
[('room ', 3), ('habit ', 3) , ('house ', 2) , ('last ', 2) , ('loft ', 2) , ('spring ', 1) , ('
train ', 1), ('bar ', 1) , ('power ', 1) , ('immediate ', 1) , ('year ', 1) , ('midst ', 1) ,
('forenoon ', 1) , ('centre ', 1) , ('papers ', 1) , ('best ', 1) , ('darkest ', 1) , ('
prime ', 1), ('hospital ', 1) , ('nursery ', 1)]
murder of the
[(' honourable ', 1) , (' discoverer ', 1)]
particulars of the
[('crime ', 1) , ('inquest ', 1) , ('habits ', 1) , ('voyage ', 1)]
the death of
[('the ', 8), ('sir ', 8) , ('captain ', 3) , ('their ', 2) , ('this ', 2) , ('his ', 2) , ('her
', 2), ('dr ', 2) , ('sherlock ', 1) , ('mrs ', 1) , ('a', 1) , ('van ', 1) , ('that ', 1) ,
('drebber ', 1) , ('mr ', 1) , (' stangerson ', 1) , ('two ', 1) , ('selden ', 1) , ('one ',
1), (' wallenstein ', 1)]
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
69
one of the
[('most ', 23) , ('best ', 3) , ('main ', 3) , ('oldest ', 2) , ('greatest ', 2) , ('numerous ',
2), ('corners ', 2) , ('largest ', 2) , ('papers ', 2) , ('richest ', 2) , ('servants ',
2), ('moor ', 2) , ('finest ', 2) , ('upper ', 2) , ('very ', 2) , ('broad ', 2) , ('side ',
2), ('highest ', 2) , (' australian ', 1) , ('great ', 1)]
so far as
[('i', 17) , ('to ', 8) , ('that ', 2) , ('the ', 2) , ('it ', 2) , ('was ', 1) , ('we ', 1) , ('
they ', 1), ('he ', 1) , ('you ', 1) , ('a', 1) , ('your ', 1) , ('this ', 1) , ('his ', 1) ,
('she ', 1)]
You see, they looks as more precise, but tables are just smaller. You can’t use them to generate rubbish. 1st-order tables big,
but ”less precise”.
And here I test some 3-words queries, like as if they inputted by user:
"i can tell"
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
70
young 3%
tall 3%
middle 2%
* second order . for sequence : was a
very 4%
man 3%
small 3%
long 2%
little 2%
* first order. for word: a
very 2%
man 2%
little 2%
few 2%
small 1%
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
71
personally 12%
* second order . for sequence : not been
for 9%
in 6%
there 6%
a 4%
slept 4%
* first order. for word: been
a 5%
in 4%
the 2%
so 2%
taken 1%
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
72
not 20%
with 6%
so 4%
it 4%
"it is just"
"you shall"
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
73
Perhaps, results from all 3 tables can be combined, with the data from 3rd order table used in highest priority (or weight).
And this is it — this can be shown to user. Aside of Conan Doyle works, your software can collect user’s input to adapt itself
for user’s lexicon, slang, memes, etc. Of course, user’s ”tables” should be used with highest priority.
I have no idea, what Apple/Android devices using for hints generation, when user input text, but this is what I would use as a
first idea.
As a bonus, this can be used for language learners, to get the idea, how a word is used in a sentence.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
74
g), DirectedEdges -> True , VertexLabeling -> True ];
TextPlot [" When I find myself in times of trouble , Mother Mary comes \
to me
Speaking words of wisdom , let it be
And in my hour of darkness she is standing right in front of me
Speaking words of wisdom , let it be
Let it be , let it be , let it be , let it be
Whisper words of wisdom , let it be
And when the broken hearted people living in the world agree
There will be an answer , let it be "]
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
75
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
76
”1” is generated in 25% of cases, ”2” in 60% and ”3” in 15%. Well, almost.
Here is another use of it.
You know, when you send an email, the final destination is a server somewhere. But it may be irresponsible. So network
engineers add additional servers, ”relays”, which can hold your email for some time.
For example, what is about gmail.com?
% dig gmail .com MX
...
6 https://docs.python.org/3/library/random.html#random.choices
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
77
;; ANSWER SECTION :
gmail.com. 3600 IN MX 5 gmail -smtp -in.l. google .com.
gmail.com. 3600 IN MX 10 alt1.gmail -smtp -in.l. google .com.
gmail.com. 3600 IN MX 20 alt2.gmail -smtp -in.l. google .com.
gmail.com. 3600 IN MX 30 alt3.gmail -smtp -in.l. google .com.
gmail.com. 3600 IN MX 40 alt4.gmail -smtp -in.l. google .com.
...
The first server is primary (marked with 5). Other 4 (alt...) are relays. They can hold emails for [email protected] if the main
server is down. Of course, relays also can be down. So an MTA (Message transfer agent) tries to send an email via the first
server in list, then via the second, etc. If all are down, MTA is waiting for some time (not infinitely).
See also: https://en.wikipedia.org/wiki/MX_record.
A number (5/10/20/30/40) is priority:
MX records contain a preference indication that MUST be used in
sorting if more than one such record appears (see below ). Lower
numbers are more preferred than higher ones. If there are multiple
destinations with the same preference and there is no clear reason to
favor one (e.g., by recognition of an easily reached address ), then
the sender -SMTP MUST randomize them to spread the load across
multiple mail exchangers for a specific organization .
( https://tools.ietf.org/html/rfc5321 )
Now if you want your MTA be polite, you can make it poke relays with some probability, unloading the main mail server. In
any case, the internal network withing Google is way better than a link between you and any of these mail servers. And it
would be OK to drop an email to any of these mail servers listed in MX records.
This is how a destination server can be chosen:
random . choices ( range (4) , weights =[1/5 , 1/10 , 1/20 , 1/40])
I’m using reciprocal weights (1/x) because the lower priority, the higher probability it is to be chosen.
What if I want to send 100 emails to [email protected]?
>>> [ random . choices ( range (4) , weights =[1/5 , 1/10 , 1/20 , 1/40]) [0] for x in range (100)
]
[1, 1, 2, 1, 0, 2, 1, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
1, 1, 1, 0, 1, 0, 2, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 3, 0, 0, 2, 1, 0,
0, 0, 0, 1, 2, 2, 1, 0, 0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 1, 0, 2, 1, 0,
0, 2, 0, 0, 0, 3, 2, 0, 1, 2, 0, 1, 1, 3, 1, 1, 1, 1]
535 emails will be sent via the first (primary) mail server, 268/129/68 – via corresponding relays.
This is probably not how MTAs usually operates, but this is how it could be done.
7 https://docs.python.org/3/library/collections.html#collections.Counter
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 9
Combinatorics
(Just 6 ways.)
What if there are 4 wires?
import itertools
78
79
('blue ', 'red ', 'green ', 'yellow ')
('blue ', 'red ', 'yellow ', 'green ')
('blue ', 'green ', 'red ', 'yellow ')
('blue ', 'green ', 'yellow ', 'red ')
('blue ', 'yellow ', 'red ', 'green ')
('blue ', 'yellow ', 'green ', 'red ')
('yellow ', 'red ', 'green ', 'blue ')
('yellow ', 'red ', 'blue ', 'green ')
('yellow ', 'green ', 'red ', 'blue ')
('yellow ', 'green ', 'blue ', 'red ')
('yellow ', 'blue ', 'red ', 'green ')
('yellow ', 'blue ', 'green ', 'red ')
(24 ways.)
This is what is called permutation in combinatorics.
Combinatorics textbooks are abound with exercises like this: can you enumerate all possible 4-digit numbers constrained in
this way?
import itertools
part1_list =["13"]
part2_list =["6" , "8" , "9"]
part3_list =["0" , "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9"]
1360
1306
6130
6013
...
9139
9913
9139
9913
( 180 numbers )
This is something you can query registered vehicle database with...
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
80
import itertools
part1_list =["jake", " melissa ", " oliver ", "emily "]
part2_list =["1987", "1954", "1963"]
part3_list =["!","@","#","$","%","&","*","-","=","_","+",".",","]
jake1987 !
jake !1987
1987 jake!
1987! jake
! jake1987
...
1963 emily ,
1963 , emily
,emily1963
,1963 emily
part1_list =["jake", " melissa ", " oliver ", "emily "]
part2_list =["1987", "1954", "1963"]
part3_list =["!","@","#","$","%","&","*","-","=","_","+",".",","]
And this is a way to memorize it: the length of the final result equals to lengths of all input lists multiplied with each other
(like ”product”).
import itertools
part1_list =["jake", " melissa ", " oliver ", "emily "] # 4 elements
part2_list =["1987", "1954", "1963"] # 3 elements
part3_list =["!","@","#","$","%","&","*","-","=","_","+",".",","] # 13 elements
...
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
81
part1_list =["jake", " melissa ", " oliver ", "emily "]
part2_list =["1987", "1954", "1963"]
part3_list =["!","@","#","$","%","&","*","-","=","_","+",".",","]
JAke1987 !
JAkE1987 !
JAKe1987 !
JAKE1987 !
jake !1987
jakE !1987
jaKe !1987
jaKE !1987
...
,1963 eMIly
,1963 eMIlY
,1963 eMILy
,1963 eMILY
,1963 Emily
,1963 EmilY
,1963 EmiLy
( 56160 passwords )
Now leetspeak1 This is somewhat popular only among youngsters, but still, this is what people of all age groups do: replacing
”o” with ”0” in their passwords, ”e” with ”3”, etc. Let’s add this as well:
import itertools , string
part1_list =["jake", " melissa ", " oliver ", "emily "]
part2_list =["1987", "1954", "1963"]
part3_list =["!","@","#","$","%","&","*","-","=","_","+",".",","]
1 urbandictionary.com
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
82
jake1987 !
jakE1987 !
jak31987 !
jaKe1987 !
jaKE1987 !
jaK31987 !
...
,1963 EM1lY
,1963 EM1Ly
,1963 EM1LY
,19633 mily
,19633 milY
,19633 miLy
( 140400 passwords )
Obviously, you can’t try all 140400 passwords on Facebook, Twitter or any other well-protected internet service. But this is a
peace of cake to brute-force them all on password protected RAR-archive or feed them to John the Ripper, HashCat, Aircrack-
ng, etc.
All the files: https://math.recipes/current_tree/comb/password.
parts =[" den", " xenia ", "1979" , "1985" , " secret ", "!" , "$", "^"]
Here we enumerate all combinations of given strings, 1-string combinations, then 2-, up to 5-string combinations. No string
can appear twice.
...
denxenia1979
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
83
denxenia1985
denxeniasecret
denxenia !
denxenia$
denxenia ^
den19791985
den1979secret
den1979 !
...
xenia1985secret$ ^
xenia1985 !$^
xeniasecret !$^
19791985 secret !$
19791985 secret !^
...
(218 passwords)
Now let’s permute all string in all possible ways:
import itertools
parts =[" den", " xenia ", "1979" , "1985" , " secret ", "!" , "$", "^"]
...
^den
xenia1979
1979 xenia
xenia1985
1985 xenia
xeniasecret
secretxenia
xenia!
!xenia
...
^! $1985secret
^! $secret1985
^ $1985secret !
^$1985! secret
^ $secret1985 !
^ $secret !1985
^$ !1985 secret
^$! secret1985
(8800 passwords)
And finally, let’s alter all Latin characters in lower/uppercase ways and add leetspeek, as I did before:
import itertools , string
parts =[" den", " xenia ", "1979" , "1985" , "!" , "$", "^"]
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
84
for char in s:
if char. isalpha ():
to_be_appended =[ string . lower (char), string . upper (char)]
if char. lower ()=='e ':
to_be_appended . append ('3')
elif char. lower ()=='i ':
to_be_appended . append ('1')
elif char. lower ()=='o ':
to_be_appended . append ('0')
t. append ( to_be_appended )
else:
t. append ([ char ])
for q in itertools . product (*t):
print "". join(q)
...
dEnxenia
dEnxeniA
dEnxenIa
...
D3nx3N1a
D3nx3N1A
D3nXenia
D3nXeniA
D3nXenIa
...
^$1979 !1985
^ $19851979 !
^$1985 !1979
^$ !19791985
^$ !19851979
( 1,348,657 passwords )
Again, you can’t try to crack remote server with so many attempts, but this is really possible for password-protected archive,
known hash, etc...
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
85
9.4 Executable file watermarking/steganography using Lehmer code and facto-
rial number system
The prison authorities’ contribution
consisted of forbidding prisoners
under investigation to receive any
clothing or food packages. Sages of
jurisprudence maintained that two
French rolls, five apples and a pair of
old pants were enough to transmit
any text into the prison –– even a
fragment of Anna Karenina. Such
‘messages from the free world’ –– an
invention of the inflamed minds of
diligent bureaucrats –– were
effectively prevented. A regulation
was issued that only money could be
sent, and it had to be in round
figures of ten, twenty, thirty, forty, or
fifty rubles; thus, numbers could not
be used to work out a new ‘alphabet’
of messages.
Can we hide some information there? Not in string themselves, but in order of strings? Given the fact, that compiler doesn’t
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
86
guarantee at all, in which order the strings will be stored in object/executable file.
Let’s say, we’ve got 26 text strings, and we can swap them how we want, because their order isn’t important at all. All possible
permutations of 26 objects is 26! = 403291461126605635584000000.
How much information can be stored here? log2 26! =≈ 88, i.e., 88 bits or 11 bytes!
11 bytes of your data can be converted to a (big) number and back, OK.
What is next? Naive way is: enumerate all permutations of 26 objects, number each, find permutation of the number we’ve got
and permute 26 text strings, store to file and that’s it. But we can’t iterate over 403291461126605635584000000 permutations.
This is where factorial number system 2 and Lehmer code 3 can be used. In short, for all my non-mathematically inclined
readers, this is a way to find a permutation of specific number without use of any significant resources. And back: gived
specific permutation, we can find its number.
This piece of code I’ve copypasted from https://gist.github.com/lukmdo/7049748 and reworked it slightly:
# python 3.x
import math
return num
return code
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
87
: rtype : list
"""
perm = base.copy ()
for i in range (len(base) - 1):
j = code[i]
if i != i+j:
print (" swapping %d, %d" % (i, i+j))
perm[i], perm[i+j] = perm[i+j], perm[i]
return perm
p = base.copy ()
n = len(base)
pos_map = {v: i for i, v in enumerate (base)}
w = []
for i in range (n):
d = pos_map [perm[i]] - i
w. append (d)
if not d:
continue
t = pos_map [perm[i]]
pos_map [p[i]], pos_map [p[t]] = pos_map [p[t]], pos_map [p[i]]
p[i], p[t] = p[t], p[i]
return w
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
88
num2= int_from_perm (list( range (26)), [14 , 17, 9, 19, 11, 16, 23, 0, 2, 13, 20, 18, 21,
24, 10, 1, 22, 4, 7, 6, 15, 12, 5, 3, 8, 25])
print (" recovered num=", num2)
s2= number_to_bin_string (num2)
print (" recovered s=", s2)
I’m encoding a ”HelloWorld” binary string (in fact, any 11 bytes can be used) into a number. Number is then converted into
Lehmer code. Then the perm_from_code() function permute initial order according to the input Lehmer code:
s= HelloWorld
num= 341881320659934023674980
Lehmer code= [14 , 16, 7, 16, 7, 11, 17, 7, 1, 4, 10, 7, 9, 11, 6, 2, 6, 1, 2, 4, 0,
0, 0, 0, 0, 0]
swapping 0, 14
swapping 1, 17
swapping 2, 9
swapping 3, 19
swapping 4, 11
swapping 5, 16
swapping 6, 23
swapping 7, 14
swapping 8, 9
swapping 9, 13
swapping 10, 20
swapping 11, 18
swapping 12, 21
swapping 13, 24
swapping 14, 20
swapping 15, 17
swapping 16, 22
swapping 17, 18
swapping 18, 20
swapping 19, 23
permutation /order = [14 , 17, 9, 19, 11, 16, 23, 0, 2, 13, 20, 18, 21, 24, 10, 1, 22,
4, 7, 6, 15, 12, 5, 3, 8, 25]
This is it: [14, 17, 9, 19, 11, 16, 23, 0, 2, 13, 20, 18, 21, 24, 10, 1, 22, 4, 7, 6, 15, 12, 5, 3, 8, 25]. First put 14th string, then 17s string,
then 9th one, etc.
Now you’ve got a binary file from someone and want to read watermark from it. Get an order of strings from it and convert it
back to binary string:
recovered num= 341881320659934023674980
recovered s= HelloWorld
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
89
If you have more text strings (not unusual for most executable files), you can encode more.
100 strings: log2 (100!) ≈ 524bits ≈ 65bytes.
1000 strings: log2 (1000!) ≈ 8529bits ≈ 1066bytes. You can store some text here!
How would you force a C/C++ compiler to make specific order of text strings? This is crude, but workable:
char blob []=" hello1 \0 hello2 \0";
char *msg1=blob;
char *msg2=blob +8;
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
90
The result has exactly 100 digits, which is 2 times less than our initial idea can offer. By scanning visually this 100-digits array,
you’ll find any number in 00..99 range. All numbers are overlapped with each other: second half of each number is also first
half of the next number, etc.
Here is another. We need a sequence of binary bits with all 3-bit numbers in it:
In []:= DeBruijnSequence [{0 , 1}, 3]
Out []= {1, 0, 1, 0, 0, 0, 1, 1}
Sequence length is just 8 bits, but it has all binary numbers in 000..111 range. You may visually spot 000 in the middle of
sequence. 111 is also present: two first bits of it at the end of sequence and the last bit is in the beginning. This is so because
De Bruijn sequences are cyclic.
There is also visual demonstration: http://demonstrations.wolfram.com/DeBruijnSequences/.
The symbols of a De Bruijn sequence written around a circular object (such as a wheel of a robot) can be
used to identify its angle by examining the n consecutive symbols facing a fixed point.
Indeed: if we know De Bruijn sequence and we observe only part of it (any part), we can deduce exact position of this part
within sequence.
Let’s see, how this feature can be used.
Let’s say, there is a need to detect position of input bit within 32-bit word. For 0x1, the algorithm should report 1. 2 for 0x2. 3
for 0x4. And 31 for 0x80000000.
The result is in 0..31 range, so the result can be stored in 5 bits.
We can construct binary De Bruijn sequence for all 5-bit numbers:
In []:= tmp = DeBruijnSequence [{0 , 1}, 5]
Out []= {1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0,
1, 0, 0, 0, 0, 0}
Let’s also recall that division some number by 2n number is the same thing as shifting it by n bits. So if you divide 0xe6bec520
by 1, the result is not shifted, it is still the same. If if divide 0xe6bec520 by 4 (22 ), the result is shifted by 2 bits. We then take
result and isolate lowest 5 bits. This result is unique number for each input. Let’s shift 0xe6bec520 by all possible count
number, and we’ll get all possible last 5-bit values:
In []:= Table[ BitAnd [ BitShiftRight [ FromDigits [tmp , 2], i], 31] , {i, 0, 31}]
Out []= {0, 16, 8, 4, 18, 9, 20, 10, 5, 2, 17, 24, 12, 22, 27, 29, \
30, 31, 15, 23, 11, 21, 26, 13, 6, 19, 25, 28, 14, 7, 3, 1}
Using this table, it’s easy to build a magic table. OK, now working C example:
# include <stdint .h>
# include <stdio .h>
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
91
int bitpos ( uint32_t i)
{
return magic_tbl [(0 xe6bec520 /i) & 0x1F ];
};
int main ()
{
// construct magic table
// may be omitted in production code
for (int i=0; i <32; i++)
magic_tbl [(0 xe6bec520 /(1<<i)) & 0x1F ]=i;
// test
for (int i=0; i <32; i++)
{
printf (" input =0x%x, result =%d\n", 1<<i, bitpos (1<<i));
};
};
Here we feed our bitpos() function with numbers in 0..0x80000000 range and we got:
input =0x1 , result =0
input =0x2 , result =1
input =0x4 , result =2
input =0x8 , result =3
input =0x10 , result =4
input =0x20 , result =5
input =0x40 , result =6
input =0x80 , result =7
input =0x100 , result =8
input =0x200 , result =9
input =0x400 , result =10
input =0x800 , result =11
input =0 x1000 , result =12
input =0 x2000 , result =13
input =0 x4000 , result =14
input =0 x8000 , result =15
input =0 x10000 , result =16
input =0 x20000 , result =17
input =0 x40000 , result =18
input =0 x80000 , result =19
input =0 x100000 , result =20
input =0 x200000 , result =21
input =0 x400000 , result =22
input =0 x800000 , result =23
input =0 x1000000 , result =24
input =0 x2000000 , result =25
input =0 x4000000 , result =26
input =0 x8000000 , result =27
input =0 x10000000 , result =28
input =0 x20000000 , result =29
input =0 x40000000 , result =30
input =0 x80000000 , result =31
The bitpos() function actually counts trailing zero bits, but it works only for input values where only one bit is set. To make
it more practical, we need to devise a method to drop all leading bits except of the last one. This method is very simple and
well-known:
input & (- input )
This bit twiddling hack can solve the job. Feeding 0x11 to it, it will return 0x1. Feeding 0xFFFF0000, it will return 0x10000. In
other words, it leaves lowest significant bit of the value, dropping all others.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
92
It works because negated value in two’s complement environment is the value with all bits flipped but also 1 added (because
there is a zero in the middle of ring). For example, let’s take 0xF0. -0xF0 is 0x10 or 0xFFFFFF10. ANDing 0xF0 and 0xFFFFFF10
will produce 0x10.
Let’s modify our algorithm to support true trailing zero bits count:
# include <stdint .h>
# include <stdio .h>
int main ()
{
// construct magic table
// may be omitted in production code
for (int i=0; i <32; i++)
magic_tbl [(0 xe6bec520 /(1<<i)) & 0x1F ]=i;
// test:
printf ("%d\n", tzcnt (0 xFFFF0000 ));
printf ("%d\n", tzcnt (0 xFFFF0010 ));
};
It works!
16
4
But it has one drawback: it uses division, which is slow. Can we just multiplicate De Bruijn sequence by the value with the bit
isolated instead of dividing sequence? Yes, indeed. Let’s check in Mathematica:
In []:= BaseForm [16^^ e6bec520 *16^^80000000 , 16]
Out []:= 0 x735f629000000000
The result is just too big to fit in 32-bit register, but can be used. MUL/IMUL instruction 32-bit x86 CPUs stores 64-bit result into
two 32-bit registers pair, yes. But let’s suppose we would like to make portable code which will work on any 32-bit architecture.
First, let’s again take a look on De Bruijn sequence Mathematica first produced:
In []:= tmp = DeBruijnSequence [{0 , 1}, 5]
Out []= {1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, \
0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0}
There is exactly 5 bits at the end which can be dropped. The ”magic” constant will be much smaller:
In []:= BaseForm [ BitShiftRight [ FromDigits [tmp , 2], 5], 16]
Out []:=0 x735f629
The ”magic” constant is now ”divided by 32 (or 1»5)”. This mean that the result of multiplication of some value with one
isolated bit by new magic number will also be smaller, so the bits we need will be stored at the high 5 bits of the result.
De Bruijn sequence is not broken after 5 lowest bits dropped, because these zero bits are ”relocated” to the start of the se-
quence. Sequence is cyclic after all.
# include <stdint .h>
# include <stdio .h>
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
93
int magic_tbl [32];
int main ()
{
// construct magic table
// may be omitted in production code
for (int i=0; i <32; i++)
magic_tbl [(0 x735f629 <<i >>27) & 0x1F ]=i;
// test:
printf ("%d\n", tzcnt (0 xFFFF0000 ));
printf ("%d\n", tzcnt (0 xFFFF0010 ));
};
For example, 0x100 becomes 0x1ff, 0x1000 becomes 0x1fff, 0x20000 becomes 0x3ffff, 0x12340000 becomes 0x1fffffff. It works
because all 1 bits are gradually propagated towards the lowest bit in 32-bit number, while zero bits at the left of most significant
1 bit are not touched.
It’s possible to add 1 to resulting number, so it will becomes 0x2000 or 0x20000000, but in fact, since multiplication by magic
number is used, these numbers are very close to each other, so there are no error.
This example I used in my reverse engineering exercise from 15-Aug-2015: https://yurichev.com/blog/2015-aug-18/.
int v [64]=
{ -1,31, 8,30, -1, 7,-1,-1, 29,-1,26, 6, -1,-1, 2,-1,
-1,28,-1,-1, -1,19,25,-1, 5,-1,17,-1, 23 ,14 , 1,-1,
9,-1,-1,-1, 27,-1, 3,-1, -1,-1,20,-1, 18 ,24 ,15 ,10 ,
-1,-1, 4,-1, 21 , -1 ,16 ,11 , -1,22,-1,12, 13,-1, 0,-1 };
This piece of code I took from here. It is slightly different: the table is twice bigger, and the function returns -1 if input value
is zero. The magic number I found using just brute-force, so the readers will not be able to google it, for the sake of exercise.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
94
(By the way, I’ve got 12,665,720 magic numbers which can serve this purpose. This is about 0.294
The code is tricky after all, and the moral of the exercise is that practicing reverse engineer sometimes may just observe
input/outputs to understand code’s behaviour instead of diving into it.
9.5.4 Performance
The algorithms considered are probably fastest known, they has no conditional jumps, which is very good for CPUs starting
at RISCs. Newer CPUs has LZCNT and TZCNT instructions, even 80386 had BSF/BSR instructions which can be used for this:
https://en.wikipedia.org/wiki/Find_first_set. Nevertheless, these algorithms can be still used on cheaper RISC
CPUs without specialized instructions.
9.5.5 Applications
Number of leading zero bits is binary logarithm of value: 11.
These algorithms are also extensively used in chess engines programming, where each piece is represented as 64-bit bitmask
(chess board has 64 squares): http://chessprogramming.wikispaces.com/BitScan.
There are more: https://en.wikipedia.org/wiki/Find_first_set#Applications.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 10
This is what is called “avalanche effect” in cryptography: one bit flip of input can affect many bits of output. Go figure out
which bits must be also flipped to preserve specific remainder.
You can build such a divisor in hardware, but it would require at least one adder or subtractor, you will have a carry-ripple
problem in simple case, or you would have to create more complicated circuit.
95
96
• 1) Allocate some “tmp” variable and copy dividend to it.
• 2) Pad divisor by zero bits at left so that MSB of divisor is at the place of MSB of the value in tmp.
• 3) If the divisor is larger than tmp or equal, subtract divider from tmp and add 1 bit to the quotient. If the divisor is
smaller than tmp, add 0 bit to the quotient.
• 4) Shift divisor right. If the divisor is 0, stop. Remainder is in tmp.
• 5) Goto 3
The following piece of code I’ve copypasted from somewhere:
unsigned int divide ( unsigned int dividend , unsigned int divisor )
{
unsigned int tmp = dividend ;
unsigned int denom = divisor ;
unsigned int current = 1;
unsigned int answer = 0;
if ( denom == tmp)
return 1;
// align divisor:
while ( denom <= tmp)
{
denom = denom << 1;
current = current << 1;
}
( https://math.recipes/current_tree/GF2/div.c )
Let’s divide 1234567 by 813 and find remainder:
current =1024 , denom =832512
current =512 , denom =416256
current =256 , denom =208128
current =128 , denom =104064
current =64, denom =52032
current =32, denom =26016
current =16, denom =13008
current =8, denom =6504
current =4, denom =3252
current =2, denom =1626
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
97
current =1, denom =813
tmp/ remainder =433
1518
int get_bit ()
{
if ( buf_pos == -1)
return -1; // end
for (;;)
{
int bit= get_bit ();
if (bit == -1)
{
printf ("exit. remainder =%d\n", tmp);
return tmp;
};
tmp=tmp <<1;
tmp=tmp|bit;
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
98
}
( https://math.recipes/current_tree/GF2/div_both.c )
Let’s divide 1234567 by 813 and find remainder:
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =0, can 't subtract
tmp =1, can 't subtract
tmp =2, can 't subtract
tmp =4, can 't subtract
tmp =9, can 't subtract
tmp =18, can 't subtract
tmp =37, can 't subtract
tmp =75, can 't subtract
tmp =150 , can 't subtract
tmp =301 , can 't subtract
tmp =602 , can 't subtract
1205 greater or equal to 813
new tmp =392
tmp =785 , can 't subtract
1570 greater or equal to 813
new tmp =757
1515 greater or equal to 813
new tmp =702
1404 greater or equal to 813
new tmp =591
1182 greater or equal to 813
new tmp =369
tmp =738 , can 't subtract
1476 greater or equal to 813
new tmp =663
1327 greater or equal to 813
new tmp =514
1029 greater or equal to 813
new tmp =216
tmp =433 , can 't subtract
exit. remainder =433
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
99
n
Wherever x is present, that means, you have a bit at position n. Just x means, bit present at LSB. There is, however, bit at
x32 , so the CRC32 polynomial has the size of 33 bits, bit the MSB is always 1 and is omitted in all algorithms.
It’s important to say that unlike in algebra, GF(2) polynomials are never evaluated here. x is symbol is present mereley as a
convention. People represent GF(2) ”structures” as polynomials to emphasize the fact that ”numbers” are isolated from each
other.
Now, subtraction and addition are the same operations in GF(2) and actually works as XOR. This is present in many tutorials,
so I’ll omit this here.
Also, by convention, whenever you compare two numbers in GF(2), you only compare two most significant bits, and ignore
the rest.
10.6 CRC32
Now we can take the binary division algorithm and change it a little:
uint32_t remainder_GF2 ( uint32_t dividend , uint32_t divisor )
{
// necessary bit shuffling/negation to make it compatible with other CRC32
implementations.
// N.B.: input data is not an array, but a 32-bit integer, hence we need to swap
endiannes.
uint32_t dividend_negated_swapped = ∼swap_endianness32 ( bitrev32 ( dividend ));
buf =( uint8_t *)& dividend_negated_swapped ;
buf_pos =3;
buf_bit_pos =7;
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
100
( https://math.recipes/current_tree/GF2/div_both.c )
And voila, this is the function which computes CRC32 for the input 32-bit value.
There are only 3 significant changes:
• XOR instead of minus.
• Only MSB is checked during comparison. But the MSB of all CRC polynomials is always 1, so we only need to check MSB
(33th bit) of the tmp variable.
• There are 32+32=64 iterations instead of 32. As you can see, only MSB of tmp affects the whole behaviour of the algo-
rithm. So when tmp variable is filled by 32 bits which never affected anything so far, we need to ”blow out” all these
bits through 33th bit of tmp variable to get correct remainder (or CRC32 sum).
All the rest algorithms you can find on the Internet are optimized version, which may be harder to understand. No algorithms
used in practice “blows” anything “out” due to optimization. Many practical algorithms are either bytewise (process input
stream by bytes, not by bits) or table-based.
My goal was to write two functions, as similar to each other as possible, to demonstrate the difference.
So the CRC value is in fact remainder of division of input date by CRC polynomial in GF(2) environment. As simple as that.
10.7 Rationale
Why use such an unusual mathematics? The answer is: many GF(2) operations can be done using bit shifts and XOR, which
are very cheap operations.
Electronic circuit for CRC generator is extremely simple, it consists of only shift register and XOR gates. This one is for CRC16:
Figure 10.1:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
101
The following page has animation: https://en.wikipedia.org/wiki/Computation_of_cyclic_redundancy_checks.
It can be implemented maybe even using vacuum tubes.
And the task is not to compute remainder according to rules of arithmetics, but rather to detect errors.
Compare this to a division circuit with at least one binary adder/subtractor, which will have carry-ripple problem. On the
other hand, addition over GF(2) has no carries, hence, this problem absent.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 11
Logarithms
11.1 Introduction
11.1.1 Children’s approach
When children argue about how big their favorite numbers are, they speaking about how many zeroes it has: “x has n zeroes!”
“No, my y is bigger, it has m > n zeroes!”
This is exactly notion of common (base 10) logarithm.
Googol (10100 ) has 100 zeroes, so log10 (googol) = 100.
Let’s take some big number, like 12th Mersenne prime:
Wow, it’s so big. How can we measure it in childish terms? How many digits it has? We can count using common (base 10)
logarithm:
So it has 39 digits.
Another question, how may decimal digits 1024-bit RSA key has?
102
103
The common way to represent a real number in handheld calculators and FPUs is:
For example:
It was common for scientific handheld calculators to use the first 10 digits of significand and ignore everything behind. Storing
the whole number down to the last digit is 1) very expensive; 2) hardly useful.
The number in IEEE 754 format (most popular way of representing real numbers in computers) has these three parts, however,
it has different base (2 instead of 10).
Another logarithmic scale familiar to anyone is decibel. Even on cheap mp3 players and smartphones, where the volume is
measured in conventional percents, this scale is logarithmic, and the difference between 50% and 60% may be much larger
in sound pressure terms.
Yet another familiar to anyone logarithmic scale is Richter magnitude scale 1 . The Richter scale is practical, because when
people talk about earthquakes, they are not interesting in exact scientific values (in Joules or TNT equivalent), they are inter-
esting in how bad damage is.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
104
Figure 11.2: Frequency response (also known as Bode plot) of some loudspeaker
Both axis on this plot are logarithmic: y axis is loudness in decibel and x axis is frequency in Hertz. Needless to say, the
typical loudspeaker has bass/medium speaker + tweeter (high frequency speaker). Some of more advanced loudspeaker has
3 speakers: bass, medium and tweeter. Or even more. And since the plot is logarithmic, each of these 2 or 3 speakers has
their own part of plot, and these parts has comparable size. If the x axis would be linear instead of logarithmic, the main part
of it would be occupied by frequency response of tweeter alone, because it has widest frequency range. While bass speaker
has narrowest frequency range, it would have very thin part of the plot.
y axis (vertical) of the plot is also logarithmic (its value is shown in decibels). If this axis would be linear, the main part of it
would be occupied by very loud levels of sound, while there would be thinnest line at the bottom reserved for normal and
quiet level of sounds.
Both of that would make plot unusable and impractical. So both axis has logarithmic scale. In strict mathematics terms, the
plot shown is called log-log plot, which means that both axis has logarithmic scale.
Summarizing, both electronics engineers and HiFi audio enthusiasts use these plots to compare quality of speakers. These
plots are often used in loudspeakers reviews.
Some of speakers of USSR era (like Latvian Radiotehnika S-30 and S-90) had such plots right on the surface of speaker box,
presumably, for marketing purposes:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
105
11.2.3 In IT
git, like any other VCS, can show a graph, how many changes each file got in each commit, for example:
$ git log --stat
...
commit 2 fb3437fa753d59ba37f3d11c7253583d4b87c99
Author : Dennis Yurichev <dennis@yurichev .com >
Date: Wed Nov 19 14:14:07 2014 +0200
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
106
patterns /185 _64bit_in_32_env /4 _MSVC .asm | 30 ------
patterns /185 _64bit_in_32_env / conversion /4.c | 6 ++
patterns /185 _64bit_in_32_env / conversion / Keil_ARM_O3 .s | 4 +
patterns /185 _64bit_in_32_env / conversion / MSVC2012_Ox .asm | 6 ++
patterns /185 _64bit_in_32_env / conversion /main.tex | 48 ++++++++++
patterns /185 _64bit_in_32_env /main.tex | 127
+-------------------------
...
This scale is not logarithmical (I had a look into git internals), but this is exact place where logarithmical scale can be used.
When software developer got such report, he/she don’t interesting in exact numbers of lines changed/added/removed. He/she
wants to see an outlook: which files got most changes/additions/removals, and which got less.
There is also a constraint: the space on the terminal is limited, so it’s not possible to draw a minus or plus sign for each
changed line of code.
Another example is Bitcoin client “signal reception strength”, apparently, modeled after mobile phone signal indicator:
These bars indicating, how many connections client currently has. Let’s imagine, client can support up to 1000 connections,
but user is never interesting in precise number, all he/she wants to know is how good its link with Bitcoin network is. I don’t
know how Bitcoin calculates this, but I think one bar could indicate that client has only 1 connection, two bars — 2-5, three
bars — up to 10-20, and four bars — anything bigger. This is also logarithmic scale. On contrary, if you divide 1000 by 4 even
parts, and one bar will fired if you’ve got 250 connections, two bars if you’ve got 500, etc, this would make the indicator
useless, such indicators are no better than simple “on/off” lamp.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
107
11.2.7 Money
Banknotes and coins. It’s just wouldn’t be possible without logarithmic scale.
Here logarithmic scale allows you to pay an arbitrary sum of money using a reasonable number of banknotes/coins.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
108
11.3 Multiplication and division using addition and subtraction
It is possible to use addition instead of multiplication, using the following rule:
Now shift the core of rule so C scale at 1 will point to 1.2 at D scale:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
109
Find 2 at C scale and find corresponding value at D scale (which is 2.4). Indeed, 1.2 · 2 = 2.4. It works because by sliding
scales we actually add distance between 1 and 1.2 (at any scale) to the distance between 1 and 2 (at any scale). But since these
scales logarithmic, addition of logarithmic values is the same as multiplication.
Values on scales can be interpreted as values of other order of magnitude. We can say that 1 at C scale is actually point to 12
at D scale. Find 1.8 at D scale (which is 18 now), it points somewhere between 21 and 22. It’s close: 12 · 18 = 216.
It works because of equation 11.3.
Here is another example from Wikipedia:
Then add these numbers. Find the number you got in table of powers of 10 (10x , also called “anti-log table”):
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
110
Resulting number is a product. The whole process may be faster than to multiply using long multiplication method using
paper-n-pencil taught in schools.
Screenshots I took from the Bradis’ book, once popular in USSR. Another well-known book in western world with logarithmic
and other tables is Daniel Zwillinger - CRC Standard Mathematical Tables and Formulae (up to 30th edition, the logarithmic
tables are dropped after).
int main ()
{
double a =1.234e -300;
double b =2.345678901234e -24;
double c =3.456789e -50;
printf ("%.30 e\n", a*b/c);
};
The output is 1.429261797122261460966983388190 × 10−274 , which is incorrect. When using debugger, we can see that the
multiplication operation raises inexact exception and underflow exception in FPU. The division operation also raises inexact
exception.
Let’s check in Wolfram Mathematica:
In []:= a*b/c
Out []= 8.37357*10^ -275
The underflow exception raised in my C program because result of multiplication is in fact 2.894567764122756∗10−324 , which
is even smaller than smallest denormalized number FPU can work with.
Let’s rework our example to compute it all using natural logarithms (exp(x) is a C standard function, which computes ex and
log(x) here is loge (x) (or ln(x))):
int main ()
{
double a =1.234e -300;
double b =2.345678901234e -24;
double c =3.456789e -50;
printf ("%.30 e\n", exp(log(a)+log(b)-log(c)));
};
4 Denormalized numbers in double-precision floating point format are numbers between ≈ 10324 and ≈ 10308
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
111
−275
Now the output is 8.373573753338710216281125792150 × 10 , same as Mathematica reported.
The same problem with very large numbers.
int main ()
{
double a =1.234 e +300;
double b =2.345678901234 e+24;
double c =3.456789 e+50;
printf ("%.30 e\n", a*b/c);
};
When this program running, its result is “inf”, meaning ∞, i.e., overflow occurred. When using debugger, we can see than the
multiplication operation raises inexact exception plus overflow exception. The correct value in Wolfram Mathematica is...
In []:= b = 2.345678901234*10^24;
In []:= c = 3.456789*10^50;
In []:= a*b/c
Out []= 8.37357*10^273
Given that, the FPU may process significands and exponents separately during multiplication, but when it processes expo-
nents of two numbers, they are just summed up. For example:
5 https://en.wikipedia.org/wiki/Logarithmic_number_system
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
112
signif icand1 × 210 · signif icand2 × 250 = signif icand3 × 260 (11.5)
…precise values of significands are omitted, but we can be sure, if the first number has exponent of 10, the second has 50,
the exponent of the resulting number will be ≈ 60.
Conversely, during division, exponent of divisor is subtracted from the exponent of the dividend.
I don’t have access to Intel or AMD FPU internals, but I can peek into OpenWatcom FPU emulator libraries 6 .
Here is summing of exponents during multiplication:
https://github.com/open-watcom/open-watcom-v2/blob/86dbaf24bf7f6a5c270f5a6a50925f468d8d292b/bld/fpuemu/
386/asm/fldm386.asm#L212.
And here is subtracting of exponents during division:
https://github.com/open-watcom/open-watcom-v2/blob/e649f6ed488eeebbc7ba9aeed8193d893288d398/bld/fpuemu/
386/asm/fldd386.asm#L237.
Here is also multiplication function from FPU emulator in Linux kernel: https://github.com/torvalds/linux/blob/
da957e111bb0c189a4a3bf8a00caaecb59ed94ca/arch/x86/math-emu/reg_u_mul.S#L93.
11.4 Exponentiation
Using equation 11.3 we may quickly notice that
That works with any logarithmic base. In fact, this is the way how exponentiation is computed on computer. x86 CPU and
x87 FPU has no special instruction for it.
This is the way how pow() function works in Glibc: https://github.com/lattera/glibc/blob/master/sysdeps/x86_
64/fpu/e_powl.S#L189:
...
7: fyl2x // log2(x) : y
8: fmul %st (1) // y*log2(x) : y
fst %st (1) // y*log2(x) : y*log2(x)
frndint // int(y*log2(x)) : y*log2(x)
fsubr %st , %st (1) // int(y*log2(x)) : fract (y*log2(x))
fxch // fract (y*log2(x)) : int(y*log2(x))
f2xm1 // 2^ fract (y*log2(x)) -1 : int(y*log2(x))
faddl MO(one) // 2^ fract (y*log2(x)) : int(y*log2(x))
fscale // 2^ fract (y*log2(x))*2^ int(y*log2(x)) : int(y*log2(x
))
fstp %st (1) // 2^ fract (y*log2(x))*2^ int(y*log2(x))
...
x87 FPU has the following instructions used Glibc’s version of pow() function: FYL2X (compute y · log2 x), F2XM1 (compute
2x –1). Even more than that, FYL2X instruction doesn’t compute binary logarithm alone, it also performs multiplication oper-
ation, to provide more easiness in exponentiation computation.
6 It was a time in 1980s and 1990s, when FPU was expensive and it could be bought separately in form of additional chip and added to x86 computer.
And if you had run a program which uses FPU on the computer where it’s missing, FPU emulating library might be an option. Much slower, but better than
nothing.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
113
x
It works because calculating 2 (exponentiation with base 2) is faster than exponentiation of arbitrary number.
Using hacker’s tricks, it’s also possible to take advantage of the IEEE 754 format and SSE instructions set:
http://stackoverflow.com/a/6486630/4540328.
√ log2 x
2
x=2 2 (11.8)
This leads to an interesting consequence: if you have a value stored in logarithmical form and you need to take square root
of it and leave it in logarithmical form, all you need is just to divide it by 2.
And since floating point numbers encoded in IEEE 754 has exponent encoded in logarithmical form, you need just to shift it
right by 1 bit to get square root: https://en.wikipedia.org/wiki/Methods_of_computing_square_roots#Approximations_
that_depend_on_the_floating_point_representation.
Likewise, cube root and nth root can be calculated using logarithm of corresponding base:
√ logb x
b
x=b b (11.9)
loga (x)
logy (x) = (11.10)
loga (y)
So, to compute common (base 10) logarithm using available x87 FPU instructions, we may use this equation:
log2 (x)
log10 (x) = (11.11)
log2 (10)
1
logy (x) = (11.12)
logx (y)
Knowing that, and the fact that x87 FPU has FYL2X instruction (compute y · log2 x), logarithm base conversion can be done
using multiplication:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
114
Listing 11.11: Assembly language code
lab fFLOGm
fldlg2 ; main LOG10 entry point
jmp short fFYL2Xm
lab fFYL2Xm
fxch
or cl , cl ; if arg is negative
JSNZ Yl2XArgNegative ; return a NAN
fyl2x ; compute y*log2(x)
ret
11.7.1 Weights
An interesting problem I’ve found in the Simon Singh — Fermat’s Last Theorem (1997) book:
The Arithmetica which inspired Fermat was a Latin translation made by Claude Gaspar Bachet de
Méziriac, reputedly the most learned man in all of France. As well as being a brilliant linguist, poet and
classics scholar, Bachet had a passion for mathematical puzzles. His first publication was a compilation of
puzzles entitled Problemes plaisans et délectables qui se font par les nombres, which included river-crossing
problems, a liquid-pouring problem and several think-of-a-number tricks. One of the questions posed was
a problem about weights:
What is the least number of weights that can be used on a set of scales to weigh any whole
number of kilograms from 1 to 40
Bachet had a cunning solution which shows that it is possible to achieve this task with only four weights...
...
In order to weigh any whole number of kilograms from 1 to 40 most people will suggest that six weights are
required: 1, 2, 4, 8, 16, 32 kg. In this way, all the weights can easily be achieved by placing the following
combinations in one pan:
1 kg = 1,
2 kg = 2,
3 kg = 2 + 1,
4 kg = 4,
5 kg = 4 + 1,
...
40 kg = 32 + 8.
However, by placing weights in both pans, such that weights are also allowed to sit alongside the ob-
ject being weighed, Bachet could complete the task with only four weights: 1, 3, 9, 27 kg. A weight placed in
the same pan as the object being weighed effectively assumes a negative value. Thus, the weights can be
achieved as follows:
1 kg = 1,
2 kg = 3 - 1,
3 kg = 3,
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
115
4 kg = 3 + 1,
5 kg = 9 - 3 - 1,
...
40 kg = 27 + 9 + 3 + 1.
We’ll talk here only about the first part of the solution: 1/2/4/8/etc kg. weights. Indeed, any weight can be achieved using
only a combination of weights in 2n form.
Say, 11 kg is:
• 1 kg weight = 1 (on)
• 2 kg weight = 1 (on)
• 4 kg weight = 0 (off)
• 8 kg weight = 1 (on)
It’s like a number encoded in binary form.
How many weights you would need to weight an n kg? It’s ⌈log2 n⌉.
And ⌈log2 40⌉ = 6.
As of the second part of the problem, “SAT/SMT by Example”7 has an example about it.
It doesn’t work: LED D7 has been burned, but yet, you don’t know, which. You have an ohmmeter to find it. You can check
them all, but there 8 of them. Or, you can divide that chain by two and try the left pin of D1 and the right pin of D4. Working?
If yes, try the left pin of D4 and the right pin of D8. It doesn’t. No you have a failed chain of 4 LEDs, you know that the right
half of chain is failing, but the left is OK. You divide that chain by two and repeat the process. Approximalety, a number of all
measurements is a binary logarithm of number of LEDs.
At best (by luck, you always hit failed part of chain), a number of all measurements is binary logarithm of number of LEDs (8)
minus 1, and that is 3. At worst (by unluck, you always hit working part of chain then you switch to failed part), is the same
number, doubled, that is 6.
Binary logarithm of some number is the number of how many bits needs to be allocated.
7 https://sat-smt.codes/
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
116
x
If you have a variable which always has 2 form, it’s a good idea to store a binary logarithmic representation (log2 (x)) instead
of it. There are at least two reasons: 1) the programmer shows to everyone that the number has always 2x form; 2) it’s error-
prone, it’s not possible to accidentally store a number in some other form to this variable, so this is some kind of protection;
3) logarithmic representation is more compact. There is, however, performance issue: the number must be converted back,
but this is just one shifting operation (1<<log_n).
Here is an example from NetBSD NTP client ( netbsd-5.1.2/usr/src/dist/ntp/include/ntp.h ):
If you need to generate bitmasks in range 1, 2, 4, 8...0x80000000, it is good idea to assign self-documenting name to iterator
variable:
Now about compactness, here is the fragment I found in OpenBSD, related to SGI IP22 architecture 8
( OS/OpenBSD/sys/arch/sgi/sgi/ip22_machdep.c ):
Here is another example of using binary logarithm in Mozilla JavaScript engine (JIT compiler) 9 . If some number is multiplied
by 2x , the whole operation can be replaced by bit shift left.
The following code ( js/src/jit/mips/CodeGenerator-mips.cpp ), when translating multiplication operation into MIPS
machine code, first, get assured if the number is really has 2x form, then it takes binary logarithm of it and generates MIPS
SLL instruction, which states for “Shift Left Logical”.
8 http://www.linux-mips.org/wiki/IP22
9 http://fossies.org/linux/seamonkey/mozilla/js/src/jit/mips/CodeGenerator-mips.cpp
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
117
Listing 11.18: Mozilla JavaScript JIT compiler (translating multiplication operation into MIPS bit shift instruction)
bool
CodeGeneratorMIPS :: visitMulI ( LMulI *ins)
{
default :
uint32_t shift = FloorLog2 ( constant );
...
Thus, for example, x = y · 1024 (which is the same as x = y · 210 ) translates into x = y << 10.
inline uint_fast8_t
CountLeadingZeroes32 ( uint32_t aValue )
{
return __builtin_clz ( aValue );
}
Latest x86 CPUs has LZCNT (Leading Zeroes CouNT) instruction for that 11 , but there is also BSR (Bit Scan Reverse) instruction
appeared in 80386, which can be used for the same purpose. More information about this instruction on various architec-
tures: https://en.wikipedia.org/wiki/Find_first_set.
There are also quite esoteric methods to count leading zeroes without this specialized instruction: http://yurichev.com/
blog/de_bruijn/.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
118
O(n) – time is linear, growing according to the size of input data. Simplest example is search for some value in the input array.
The larger array, the slowest search.
O(log n) – time is logarithmic to the input data. Let’s see how this can be.
Let’s recall child’s number guessting game13 . One player think about some number, the other should guess it, offering various
versions. First player answers, is guessed number is larger or less. A typical dialogue:
-- I think of a number in 1..100 range .
-- Is it 50?
-- My number is larger .
-- 75?
-- It is lesser .
-- 63?
-- Larger .
-- 69?
-- Larger .
-- 72?
-- Lesser .
-- 71?
-- Correct .
Best possible strategy is to divide the range in halves. The range is shorten at each step by half. At the very end, the range has
lenght of 1, and this is correct answer. Maximal number of steps using the strategy described here are log2 (initial_range).
In our example, initial range is 100, so the maximum number of steps is 6.64... or just 7. If the initial range is 200, maximum
number of steps are log2 (200) = 7.6.. or just 8. The number of steps increasing by 1 when the range is doubled. Indeed,
doubled range indicates that the guesser needs just one more step at the start, not more. If the initial range is 1000, numbers
of steps are log2 (1000) = 9.96... or just 10.
This is exactly O(log n) time complexity.
Now let’s consider couple of practical real-world algorithms. One interesting thing is that if the input array is sorted, and its
size is known, and we need to find some value in it, the algorithm works exactly in the same way as child’s number guessing
game! The algorithm starts in the middle of array and compare the value there with the value sought-after. Depending on
the result (larger or lesser), the cursor is moved left or right and operating range is decreasing by half. This is called binary
search14 , and there is the bsearch() function in standard C/C++ library15 .
Another prominent example in CS is binary trees. They are heavily used internally in almost any programming language,
when you use set, map, dictionary, etc.
Here is a simple example with the following numbers (or keys) inserted into binary tree: 0, 1, 2, 3, 5, 6, 9, 10, 11, 12, 20, 99, 100,
101, 107, 1001, 1010.
10
1 100
0 5 20 107
3 6 12 99 101 1001
2 9 11 1010
13 http://rosettacode.org/wiki/Guess_the_number
14 https://en.wikipedia.org/wiki/Binary_search_algorithm
15 http://en.cppreference.com/w/cpp/algorithm/bsearch
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
119
And here is how binary tree search works: put cursor at the root. Now compare the value under it with the value sought-after.
If the value we are seeking for is lesser than the current, take a move into left node. If it’s bigger, move to the right node. Hence,
each left descendant node has value lesser than in ascendant node. Each right node has value which is bigger. The tree must
be rebalanced after each modification (I gave examples of it in my book about reverse engineering (http://beginners.re/,
51.4.4)). Nevertheless, lookup function is very simple, and maximal number of steps is logn (number_of _nodes). We’ve got
17 elements in the tree at the picture, log2 (17) = 4.08..., indeed, there are 5 tiers in the tree.
11.9 Printing
Common logarithms are sometimes used to calculate space for decimal number in the string or on the screen. How many
characters you should allocate for 64-bit number? 20, because log10 (264 ) = 19.2....
Functions like itoa()16 (which converts input number to a string) can calculate output buffer size precisely, calculating com-
mon logarithm of the input number.
Fine, but what would you do if you have 64-bit addresses? This is too bulky:
0 x0000000000000000 00 01 02 03 04 05 06 07 -08 09 0a 0b 0c 0d 0e 0f "................"
0 x0000000000000010 10 11 12 13 14 15 16 17 -18 19 1a 1b 1c 1d 1e 1f "................"
0 x00000000000000e0 e0 e1 e2 e3 e4 e5 e6 e7 -e8 e9 ea eb ec ed ee ef "................"
...
0 x00000000000000f0 f0 f1 f2 f3 f4 f5 f6 f7 -f8 f9 fa fb fc fd fe ff "................"
0 x0000000000000100 00 01 02 03 04 05 06 07 -08 09 0a 0b 0c 0d 0e 0f "................"
0 x0000000000000110 10 11 12 13 14 15 16 17 -18 19 1a 1b 1c 1d 1e 1f "................"
One solution is to align all values somehow according to the last value (or address). How many hex digits we need to print
the last (0x110) address? 3. So let’s all addresses will be printed as 3-digit numbers.
16 http://www.cplusplus.com/reference/cstdlib/itoa/
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
120
To get exact number of hex digits, we just use binary logarithm.
⌈ log2 (0x110)
4 ⌉
(Hexadecimal logarithm would also suffice.)
Why ceiling? Say, the last address has 11 bits, and 11/4=2, but to print 11 binary digits, we want 3 hexadecimal digits. So the
result of division must be ”ceiled” to 3.
Now it’s neat:
0x000 00 01 02 03 04 05 06 07 -08 09 0a 0b 0c 0d 0e 0f "................"
0x010 10 11 12 13 14 15 16 17 -18 19 1a 1b 1c 1d 1e 1f "................"
0x0e0 e0 e1 e2 e3 e4 e5 e6 e7 -e8 e9 ea eb ec ed ee ef "................"
...
0x0f0 f0 f1 f2 f3 f4 f5 f6 f7 -f8 f9 fa fb fc fd fe ff "................"
0x100 00 01 02 03 04 05 06 07 -08 09 0a 0b 0c 0d 0e 0f "................"
0x110 10 11 12 13 14 15 16 17 -18 19 1a 1b 1c 1d 1e 1f "................"
The code:
...
// http://stackoverflow.com/questions/11376288/fast-computing-of-log2-for-64-bit-integers
static const int tab64 [64]=
{
63, 0, 58, 1, 59, 47, 53, 2,
60, 39, 48, 27, 54, 33, 42, 3,
61, 51, 37, 40, 49, 18, 28, 20,
55, 30, 34, 11, 43, 14, 22, 4,
62, 57, 46, 52, 38, 26, 32, 41,
50, 36, 17, 19, 29, 10, 13, 21,
56, 45, 25, 31, 35, 16, 9, 12,
44, 24, 15, 8, 23, 7, 6, 5
};
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
121
fill_zeroes = division_ceil ( binary_digits , 4);
};
...
out << "0x" << std :: hex << std :: setw( fill_zeroes ) << ( starting_offset
+ pos + ofs) << " ";
...
current = initial
# 40 years
for year in range (40):
# what you get at the end of each year?
current = current + current *APY
print "year=", year , " amount at the end", current
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
122
The thing is that the final amount (268.50...) is aimed toward e constant.
Now there is another bank, which offers to recapitalize your deposit each month. We’ll rewrite our script slightly:
current = initial
# 40 years
for year in range (40):
for month in range (12):
# what you get at the end of each month?
current = current + current *( APY /12)
print "year=", year , " month =", month , " amount ", current
current = initial
# 40 years
for year in range (40):
for month in range (12):
for day in range (30):
# what you get at the end of each day?
current = current + current *( APY /12/30)
print "year=", year , "month =", month , "day=", day , " amount ", current
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
123
From electronics engineering course we may know that the capacitor discharging by half after RC ln(2) seconds, where C is
capacity of capacitor in farads and R resistance of resistor in ohms. Given 1kΩ resistor and 1000µF capacitor, what its voltage
after 1 seconds will be? after 2 seconds? It’s discharge can be calculated using this equation:
−t
V = V0 · e RC
…where V0 is initial charge in volts, t is time in seconds and e is base of natural logarithm.
Let’s see it in Wolfram Mathematica:
v = 1; (* initial voltage *)
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
124
Text ["1/8" , {0.1 , 0.128}]} , AxesLabel -> {seconds , voltage }]
1 1
As we can see, 2 of initial charge is left after ln(2) seconds ( 0.69...), and 4 of charge is left after ln(4) seconds ( 1.38...).
1
Indeed, if we interesting in precise time in seconds, when charge will be x , just calculate ln(x).
1
Now here is the same plot, but I added two more labels, 3 and 17 :
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
125
…and we see that these points corresponds to ln(3) and ln(7). That means, 1
3 of charge is left after ln(3) ≈ 1.098... seconds
and 71 of charge after ln(7) ≈ 1.945... seconds.
Radioactive decay
Radioactive decay is also exponential decay. Let’s take Polonium 210 as an example17 . It’s half-life (calculated) is ≈ 138.376
days. That means that if you’ve got 1kg of Polonium 210, after ≈ 138 days, half of it (0.5 kg) left as 210 Po and another half is
transformed into 206 Pb (isotope of lead18 ). After another ≈ 138 days, you’ll get 34 of isotope of lead and 14 will left as 210 Po.
After another ≈ 138 days, amount of Polonium will be halved yet another time, etc.
The equation of radioactive decay is:
N = N0 e−λt
…where N is number of atoms at some point of time, N0 is initial number of atoms, t is time, λ is decay constant. Decay of
Polonium is exponential, but decay constant is the constant, defining how fast (or slow) it will fall.
Here we go in Mathematica, let’s get a plot for 1000 days:
hl = Log [2]/l
138.376
17 https://en.wikipedia.org/wiki/Polonium
18 https://en.wikipedia.org/wiki/Isotopes_of_lead#Lead-206
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
126
Beer froth
There is even the paper (got Ig Nobel prize in 2002), author’s of which demonstrates that beer froth is also decays exponen-
tially: http://iopscience.iop.org/0143-0807/23/1/304/, https://classes.soe.ucsc.edu/math011a/Winter07/
lecturenotes/beerdecay.pdf.
The paper can be taken as a joke, nevertheless, it’s a good demonstration of exponential decay.
Conclusion
Capacitor discharge and radioactive decay obeys the same law of halving some amount after equal gaps of time:
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
127
Decay constant in case of capacitor discharge defined by product of resistance and capacity. The bigger one of them, the
slower decay.
Natural logarithm is used to calculate gap of time (half-life or half-time) judging by decay constant.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 12
Symbolic computation
Some numbers can only be represented in binary system approximately, like 13 and π. If we calculate 13 · 3 step-by-step, we
may have loss of significance. We also know that sin( π2 ) = 1, but calculating this expression in usual way, we can also have
some noise in result. Arbitrary-precision arithmetic1 is not a solution, because these numbers cannot be stored in memory
as a binary number of finite length.
How we could tackle this problem? Humans reduce such expressions using paper and pencil without any calculations. We
can mimic human behaviour programmatically if we will store expression as tree and symbols like π will be converted into
number at the very last step(s).
This is what Wolfram Mathematica2 does. Let’s start it and try this:
In []:= x + 2*8
Out []= 16 + x
Since Mathematica has no clue what x is, it’s left as is, but 2 · 8 can be reduced easily, both by Mathematica and by humans,
so that is what has done. In some point of time in future, Mathematica’s user may assign some number to x and then, Math-
ematica will reduce the expression even further.
Mathematica does this because it parses the expression and finds some known patterns. This is also called term rewriting3 .
In plain English language it may sounds like this: “if there is a + operator between two known numbers, replace this subex-
pression by a computed number which is sum of these two numbers, if possible”. Just like humans do.
Mathematica also has rules like “replace sin(π) by 0” and “replace sin( π2 ) by 1”, but as you can see, π must be preserved as
some kind of symbol instead of a number.
So Mathematica left x as unknown value. This is, in fact, common mistake by Mathematica’s users: a small typo in an input
expression may lead to a huge irreducible expression with the typo left.
Another example: Mathematica left this deliberately while computing binary logarithm:
In []:= Log [2, 36]
Out []= Log [36]/ Log [2]
Because it has a hope that at some point in future, this expression will become a subexpression in another expression and it
will be reduced nicely at the very end. But if we really need a numerical answer, we can force Mathematica to calculate it:
1 https://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic
2 Another well-known symbolic computation system are Maxima and SymPy
3 https://en.wikipedia.org/wiki/Rewriting
128
129
Characters in the expression are just unresolved symbols4 with no connections to numbers or other expressions, so Mathe-
matica left them as is.
Another real world example is symbolic integration5 , i.e., finding formula for integral by rewriting initial expression using
some predefined rules. Mathematica also does it:
In []:= Integrate [1/(x^5) , x]
Out []= -(1/(4 x^4))
Benefits of symbolic computation are obvious: it is not prone to loss of significance6 and round-off errors7 , but drawbacks
are also obvious: you need to store expression in (possible huge) tree and process it many times. Term rewriting is also slow.
All these things are extremely clumsy in comparison to a fast FPU8 .
“Symbolic computation” is opposed to “numerical computation”, the last one is just processing numbers step-by-step, using
calculator, CPU9 or FPU.
Some task can be solved better by the first method, some others – by the second one.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 13
Graph theory
Graph is a group of nodes, some of them may be connected with each other, some are not. One of the popular examples is
the map of country: there are cities and roads. ”Cities” are called ”nodes” or ”vertices” in mathematics lingo, while ”roads”
are called ”edges”. Another popular example of graph is computer network, including Internet. Computer network is graph
indeed, but it’s closer to ”sparse graph”, because for the most part, computer networks are trees.
Indeed, each of these four persons is connected to each among other 3. Wolfram Mathematica can highlight subgraph in
graph:
HighlightGraph [community , clique ]
130
131
It’s not a rule, but well-established practice, so we can recover the information, which users talks to which users most of-
ten. Let’s say, we would build a link between two IRC users if 1) they talk to each other at least 10-11 days (not necessary
consequent); 2) do this at least 6 months (not necessary consequent).
The largest cliques of #ubuntu IRC channel in 10-11 years period are these:
* clique size 11
['ubottu ', 'ActionParsnip ', 'ikonia ', 'Ben64 ', 'zykotick9 ', 'theadmin ', 'dr_willis ',
'MonkeyDust ', 'usr13 ', 'bekks ', 'iceroot ']
* clique size 10
['ubottu ', 'ActionParsnip ', 'ikonia ', 'jrib ', 'bazhang ', 'Pici ', 'iceroot ', 'theadmin
', 'IdleOne ', 'erUSUL ']
* clique size 10
['ubottu ', 'ActionParsnip ', 'ikonia ', 'jrib ', 'bazhang ', 'Pici ', 'iceroot ', 'theadmin
', 'zykotick9 ', 'usr13 ']
* clique size 10
['ubottu ', 'ActionParsnip ', 'ikonia ', 'jrib ', 'bazhang ', 'Pici ', 'iceroot ', '
sebsebseb ', 'IdleOne ', 'erUSUL ']
* clique size 10
['ubottu ', 'ActionParsnip ', 'ikonia ', 'jrib ', 'Dr_Willis ', 'Pici ', 'edbian ', 'IdleOne
', 'Jordan_U ', 'theadmin ']
* clique size 10
['ubottu ', 'ActionParsnip ', 'ikonia ', 'jrib ', 'Dr_Willis ', 'Pici ', 'edbian ', 'IdleOne
', 'Jordan_U ', 'sebsebseb ']
* clique size 10
['ubottu ', 'ActionParsnip ', 'ikonia ', 'jrib ', 'Dr_Willis ', 'Pici ', 'erUSUL ', 'iceroot
', 'IdleOne ', 'theadmin ']
* clique size 10
['ubottu ', 'ActionParsnip ', 'ikonia ', 'jrib ', 'Dr_Willis ', 'Pici ', 'erUSUL ', 'iceroot
', 'IdleOne ', 'sebsebseb ']
* clique size 10
['ubottu ', 'ActionParsnip ', 'ikonia ', 'jrib ', 'Dr_Willis ', 'Pici ', 'erUSUL ', 'iceroot
', 'ubuntu ', 'sebsebseb ']
* clique size 10
['ubottu ', 'ActionParsnip ', 'ikonia ', 'Ben64 ', 'histo ', 'bekks ', 'MonkeyDust ', '
dr_willis ', 'iceroot ', 'usr13 ']
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
132
...
Perhaps, these users are frequenters of the channel. List of all cliques are here: https://math.recipes/current_tree/
graph/clique/files/IRC/results.txt. The output is not terse, because all listed cliques are cliques indeed, and single
user or users group can be member of several cliques, that’s correct. Cliques can be overlapped and be members of bigger
cliques. It’s possible to produce more human-like results using more complex algorithms for finding communities.
The source code of my scripts here: https://math.recipes/current_tree/graph/clique/files/IRC. I used the excel-
lent networkx graph library.
There some artifacts (at the bottom) which can be ignored so far, I think. There is prominent centers: one huge and two
others are smaller. I’m not sure, but I can suggest these parts of graph are just users who has different sleep paterns, or, more
likely, from different time zones, so each important time zone (like Americas, Europe, Asia/Oceania) may have their own social
communities. But again, I’m not sure, this should be investigated first.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
133
Let’s try to find communities and hightlight them within the graph:
c2 = FindGraphCommunities [g2 ];
HighlightGraph [g2 , Map[ Subgraph [g2 , #] &, c2 ]]
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
134
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
135
Is it right? Maybe. Needless to say, since timespan is so long (at least 10 years), we can belive that some communities which
may exists in 2004-2006 may be extinct in 2014-2015 (people got older, lost their interest in Ubuntu Linux, etc), but they all
are visible on this graph.
Summary: perhaps, on our next experiment we should filter out IRC data by years and time zones.
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
136
• Space (9): New Horizons, Pioneer 11, Voyager 1, Europa (moon), Callisto (moon), Ganymede (moon), Jupiter, Io (moon),
Pioneer 10.
• Hip hop music (9): G-funk, Dr. Dre, Death Row Records, Snoop Dogg, The Chronic, Gangsta rap, West Coast hip hop,
N.W.A, Hip hop music.
• Metal music (9): Master of Puppets, Thrash metal, Cliff Burton, James Hetfield, Kirk Hammett, Metallica, Kill ’Em All,
Ride the Lightning, Dave Mustaine.
• The Beatles (8): Break-up of the Beatles, The Beatles, George Harrison, Let It Be, John Lennon, Paul McCartney, Ringo
Starr, Abbey Road.
Each Wikipedia article within any of these cliques has links to each article in clique.
Full lists of first 1000 largest cliques in English, Russian and Ukrainian Wikipedias plus source code of my scripts is here:
https://math.recipes/current_tree/graph/clique/files/wikipedia.
1 https://sat-smt.codes/
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 14
Linear algebra
R1 =2000
R2 =5000
R3 =1000
V1 =5 # left
1 http://falstad.com/circuit/
137
138
V2 =15 # right
s= Solver ()
s.add(i3 == i1+i2)
print s. check ()
m=s. model ()
print m
print m[i1]. as_decimal (6)
print m[i2]. as_decimal (6)
print m[i3]. as_decimal (6)
Or in matrix form:
[ 1, 1, -1 | 0 ]
[ 2000 , 0, 1000 | 5 ]
[ 0, 5000 , 1000 | 15 ]
In [2]:= 3/3400// N
Out [2]= 0.000882353
In [3]:= 1/425// N
Out [3]= 0.00235294
In [4]:= 11/3400// N
Out [4]= 0.00323529
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
139
[ 1,0,0 | 3/3400 ]
[ 0,1,0 | 1/425 ]
[ 0,0,1 | 11/3400 ]
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
140
tmp = b[row ];
for (j = n - 1; j > row; j--)
tmp -= x[j] * A(row , j);
x[row] = tmp / A(row , row);
}
# undef A
}
int main(void)
{
double a[] = {
1, 1, -1,
2000 , 0, 1000 ,
0, 5000 , 1000
};
double b[] = { 0, 5, 15 };
double x[3];
int i;
return 0;
}
I run it:
0.000882353
0.00235294
0.00323529
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!
Chapter 15
Acronyms used
141
142
MSB Most Significant Bit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
If you noticed a typo, error or have any suggestions, do not hesitate to drop me a note: my emails. Thanks!