From d364c0a40508be756012e7f5eca2a7cc5c632b92 Mon Sep 17 00:00:00 2001 From: anebz Date: Thu, 16 Apr 2020 16:48:25 +0200 Subject: [PATCH] chapter 6 theory and a bash command --- 05. Bit manipulation/README.md | 2 - 06. Math and logic puzzles/README.md | 95 ++++++++++++++++++++++++++++ Missing CS semester/README.md | 5 ++ README.md | 6 +- 4 files changed, 105 insertions(+), 3 deletions(-) create mode 100644 06. Math and logic puzzles/README.md diff --git a/05. Bit manipulation/README.md b/05. Bit manipulation/README.md index d0d60e2..fee7395 100644 --- a/05. Bit manipulation/README.md +++ b/05. Bit manipulation/README.md @@ -1,7 +1,5 @@ # Chapter 5. Bit manipulation -page 127. hints start at 673 - ## 5.1. Two's complement and negative numbers Positive numbers are represented as themselves, negative numbers as the two's complement of its absolute value, with a 1 in its sign bit, indicating it's negative. diff --git a/06. Math and logic puzzles/README.md b/06. Math and logic puzzles/README.md new file mode 100644 index 0000000..ef4d673 --- /dev/null +++ b/06. Math and logic puzzles/README.md @@ -0,0 +1,95 @@ +# Chapter 6. Math and logic puzzles + +## 6.1. Prime numbers + +Every positive integer can be decomposed into a product of primes. + +If y can be divided by x, written like x\y, then all primes in x's prime factorization must be in y's prime factorization. + +### 6.1.1. Checking for primality + +```cpp +boolean primeNaive(int n){ + if(n < 2){ + return false; + } + for (int i = 2; i < n; i++) { + if (n % i == 0){ + return false; + } + } + return true; +} + +boolean primeSlightlyBetter(int n){ + if (n< 2) { + return false; + } + int sqrt = (int) Math.sqrt(n); + for (int i = 2; i <= sqrt; i++) { + if (n % i == 0) return false; + } + return true; +} +``` + +`n` not being prime means that n=a * b, for a number of a,b different than 1 and n. + +* a = b, n is directly not prime +* a < b +* a > b + +Either both numbers are the same, or there's a 'big' number and a 'small' number. Always. Because the multiplicators center around the square root. For a fixed number, if you make one multiplicator higher, you have to make the other smaller. + +Therefore searching for the smaller number is enough. If we don't find any 'small' number divisor, then **the number must be prime**. Because if it had 2 divisors greater than sqrt, then the multiplication of those 2 numbers would be > n. + +Other optimizations include iterating only over prime numbers, dynamic programming. + +### 6.1.2. The sieve of Eratosthenes + +Generates a list of primes taking the advantage that all non-prime numbers are divisible by a prime number. + +```cpp +void std::vector sieveOfEratosthenes(int max) { + std::vector flags(max + 1); + + init(flags); // set all flags to true other than 0 and 1. + int prime = 2; + + while (prime <= sqrt(max)) { + crossOff(flags, prime); + prime = getNextPrime(flags, prime); + } + return flags; +} + +void crossOff(boolean[] flags, int prime) { + /* Cross off remaining multiples of prime. We can start with prime*prime), because if we have a k * prime, where k < prime, this value would have already been crossed off in a prior iteration, namely in the iteration of k. + if prime=5, we can start at 25 because 5*2=10, 5*3=15, 5*4=20 are already crossed off.*/ + for (int i = prime * prime; i < flags.length; i += prime) { + flags[i] = false; + } +} + +int getNextPrime(boolean[] flags, int prime) { + int next = prime + 1; + // find next number in flags which is True + while (next < flags.length && !flags[next]) { + next++; + } + return next; +} +``` + +## 6.2. Probability + +* Bayes' Theorem: P(AB) = P(B|A) * P(A) = P(A|B) * P(B) +* P(A+B) = P(A) + P(B) - P(AB). +* If two events are independent, P(AB) = P(A) * P(B) +* If two events are mutually exclusive, P(A+B) = P(A) + P(B) + +## 6.3. Start talking + +Write the problem down. Many brainteasers are worst-case minimization problems, in terms of minimizing an action or in doing something at most a specific number of times. + +A useful technique is to try to "balance" the worst case. That is, if an early decision results in a skewing of the worst case, we can sometimes change the decision to balance out the worst case. diff --git a/Missing CS semester/README.md b/Missing CS semester/README.md index 0c9b3a5..2263c2c 100644 --- a/Missing CS semester/README.md +++ b/Missing CS semester/README.md @@ -42,6 +42,11 @@ chmod +x my_file ./my_file ``` +Execute script and write print output to file +```bash +python script.py |& tee file.txt +``` + [Stackoverflow info](https://askubuntu.com/a/229592): > Generally, using ./filename.sh specifies a file in the current directory and using filename.sh specifies a file in the current directory or any directory of PATH. The first usage removes any uncertainty as to which file is accessed. In this case, you are attempting to execute the script with bash or another interpreter (by virtue of assumed #!/bin/bash as first line in your script) just by entering the filename. This usage requires the directory is specified. Alternatively, you can try bash filename.sh which seems to work with unspecified directory. diff --git a/README.md b/README.md index c923a30..5ff4f6a 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ If you can't afford to buy the book, you can find a free pdf [here](http://ahmed * Create minimal binary search tree from array * Create a linked list for each depth in the tree -## Chapter 5 +## Chapter 5 Bit manipulation * Insert bit * Binary to string @@ -57,6 +57,10 @@ If you can't afford to buy the book, you can find a free pdf [here](http://ahmed * (( n & (n-1)) == 0). n=? * How many different bits do two numbers have. 11001 vs. 11010 = 2. Levenstein distance +## Chapter 6 Math and logic puzzles + +* Prime numbers + ## Chapter 7 Object-oriented design * Hash tables