Converting a regular BIND ZONE to DNSSEC
Recently I wanted to sign a regular zone in BIND9.7. Google wasn't very helpful so I thought I'd write up a little bit about it here.
My /etc/named.conf looks like this:
zone "myzone.com" IN { type master; file "/var/named/zones/myzone.com/myzone.com"; notify no; };
I want to keep my dnssec zones in a separate directory.
# mkdir -p /var/named/signed/myzone.com/ # cp /var/named/zones/myzone.com/myzone.com /var/named/signed/myzone.com/
Now I sign the zone.
# cd /var/named/signed/myzone.com/ # dnssec-keygen -r /dev/urandom myzone.com # dnssec-keygen -r /dev/urandom -f KEY myzone.com # dnssec-signzone -r /dev/urandom -S myzone.com # ls myzone.com Kmyzone.com.+005+02971.key Kmyzone.com.+005+29262.private myzone.com.signed Kmyzone.com.+005+02971.private dsset-myzone.com. Kmyzone.com.+005+29262.key
Finally I want to change the named.conf to the myzone.com.signed.
zone "myzone.com" IN { type master; file "/var/named/signed/myzone.com/myzone.com.signed"; notify no; };
Make sure that all the files are owned by user "named" and reload bind
# chown -R named:named /var/named/signed # /etc/init.d/named reload
Using SVN with Git
Once I learned Git I never wanted to go back to the days of svn. The only problem is that SVN is used almost in every linux shop I worked for. Luckily there is git-svn. With git-svn I can just import an svn repositories and use git to browse, branch, merge etc.
My typical workflow:
1) Import the repository into git.
mkdir reponame && cd reponame git svn init -s https://svn.hostname.com/svn/reponame/ git svn fetch --fetch-all #If this step fails because the repo is huge, just run it again
2) Get a branch to hack on
git br -a git co --track -b branchname remotes/branchname git br -vv #I like -vv to see which branches are tracked and which are just local branches
3) Keep up with svn commits:
git svn fetch git svn rebase
This fetches revisions from the SVN parent of the current HEAD and rebases the current (uncommitted to SVN) work against it.
This works similarly to svn update or git pull except that it preserves linear history with git rebase instead of git merge for ease of dcommitting with git svn.
This accepts all options that git svn fetch and git rebase accept. However, --fetch-all only fetches from the current [svn-remote], and not all [svn-remote] definitions.
Like git rebase; this requires that the working tree be clean and have no uncommitted changes.
4) Commit changes back to svn
git svn dcommit
sin(n) = -1 where n is an integer in radians
I saw a fun math problem on reddit the other day.
find a number n, so that sin(n)=-1, where n is an integer in radians; so sin(270 degrees) doesn't count. Obviously it will never be exactly -1 but close enough for the difference to be lost in rounding.

I need the argument of sin function to be as close to an integer as possible. Call this integer m.

Solving for
leads to:
If I have a rational approximation to
with an even numerator I can divide it by two get my m. I also have to make sure that the denominator is in the form of 4n+3.
It's possible to use continued fractions to approximate real numbers. Here's a continued fraction sequence for pi: http://www.research.att.com/~njas/sequences/A001203
The first rational approximation I learned in elementary school is 22/7 which is perfect.
> (sin 11)
-.9999902065507035
For the others I'll have to evaluate the continued fraction to get my approximation of a simple fraction.
> (eval-terms (list 3 7 15 1 292 1))
104348/33215
> (sin (/ 104348 2))
-.9999999999848337
> (eval-terms (list 3 7 15 1 292 1 1 1 2 1 3 1 14 2 1))
245850922/78256779
> (sin (/ 245850922 2))
-.99999999999999999532
Looks like a good candidate was found.
This is the code to evaluate a continued fraction coefficients. It's very convenient that scheme has a native rational data type.
(define (eval-terms ts) (cond ((= (length ts) 1) (car ts)) (else (+ (car ts) (/ 1 (eval-terms (cdr ts))))))) (define (eval-terms-iter ts) (let ((rev-ts (reverse ts))) (define (ev-terms ts frac) (if (null? ts) frac (ev-terms (cdr ts) (+ (car ts) (/ 1 frac))))) (ev-terms rev-ts (car rev-ts))))
QMISMF: Chapter 2
2-1:
> (+ 3-i 2+4i)
5+3i
> (+ 1+3i 2)
3+3i
> (- -5+2i 2+2i)
-7
> (+ -2+i 2+2i)
+3i
> (* 3-i 2+4i)
10+10i
> (* 1+3i 2)
2+6i
> (* 0+i 1+3i)
-3+i
> (* -5+2i 2+3i)
-16-11i
> (* 2+3i -2+3i)
-13
> (* 2+3i 3+2i)
+13i
2-2:




2-3:





2-4:





2-5:


2-6:

for


for




2-7:



since the above is now in the usual form of the quadratic formula it is clear z is a solution.
Exercise 2.42 of SICP
Exercise 2.42:

A solution to the eight-queens puzzle.
The "eight-queens puzzle'' asks how to place eight queens on a chessboard so that no queen is in check from any other (i.e., no two queens are in the same row, column, or diagonal). One possible solution is shown in figure 2.8. One way to solve the puzzle is to work across the board, placing a queen in each column. Once we have placed k - 1 queens, we must place the kth queen in a position where it does not check any of the queens already on the board. We can formulate this approach recursively: Assume that we have already generated the sequence of all possible ways to place k - 1 queens in the first k - 1 columns of the board. For each of these ways, generate an extended set of positions by placing a queen in each row of the kth column. Now filter these, keeping only the positions for which the queen in the kth column is safe with respect to the other queens. This produces the sequence of all ways to place k queens in the first k columns. By continuing this process, we will produce not only one solution, but all solutions to the puzzle.
We implement this solution as a procedure queens, which returns a sequence of all solutions to the problem of placing n queens on an n×n chessboard. Queens has an internal procedure queen-cols that returns the sequence of all ways to place queens in the first k columns of the board.
(define (queens board-size) (define (queen-cols k) (if (= k 0) (list empty-board) (filter (lambda (positions) (safe? k positions)) (flatmap (lambda (rest-of-queens) (map (lambda (new-row) (adjoin-position new-row k rest-of-queens)) (enumerate-interval 1 board-size))) (queen-cols (- k 1)))))) (queen-cols board-size))
In this procedure rest-of-queens is a way to place k - 1 queens in the first k - 1 columns, and new-row is a proposed row in which to place the queen for the kth column. Complete the program by implementing the representation for sets of board positions, including the procedure adjoin-position, which adjoins a new row-column position to a set of positions, and empty-board, which represents an empty set of positions. You must also write the procedure safe?, which determines for a set of positions, whether the queen in the kth column is safe with respect to the others. (Note that we need only check whether the new queen is safe -- the other queens are already guaranteed safe with respect to each other.)
(define (filter pred? seq) (cond ((null? seq) '()) ((pred? (car seq)) (cons (car seq) (filter pred? (cdr seq)))) (else (filter pred? (cdr seq))))) (define (flatmap pred seq) (accumulate append '() (map pred seq))) (define (accumulate op init seq) (if (null? seq) init (op (car seq) (accumulate op init (cdr seq))))) (define (enumerate-interval low high) (if (> low high) '() (cons low (enumerate-interval (+ 1 low) high)))) (define (queens board-size) (define (queen-cols k) (if (= k 0) (list empty-board) (filter (lambda (positions) (safe? k positions)) (flatmap (lambda (rest-of-queens) (map (lambda (new-row) (adjoin-position new-row k rest-of-queens)) (enumerate-interval 1 board-size))) (queen-cols (- k 1)))))) (queen-cols board-size)) (define empty-board '()) (define (adjoin-position new-row k rest-of-queens) (append rest-of-queens (list new-row) )) (define (safe? k positions) (let ((new-q-row (list-ref positions (- k 1)))) (safe-queen? new-q-row k positions 1))) (define (safe-queen? new-q-row new-q-col positions col) (cond ((null? positions) #t) ((and (= new-q-col col) (= new-q-row (car positions))) #t) ;check for last col ((= new-q-row (car positions)) #f) ;rook moves ((= (abs (- new-q-row (car positions))) ;bishop moves (abs (- new-q-col col))) #f) (else (safe-queen? new-q-row new-q-col (cdr positions) (+ col 1)))))
> (length (queens 8))
92
> (length (queens 11))
2680
> (length (queens 10))
724
> (queens 4)
((2 4 1 3) (3 1 4 2))
Exercise 2.41 of SICP
Exercise 2.41: Write a procedure to find all ordered triples of distinct positive integers i, j, and k less than or equal to a given integer n that sum to a given integer s.
(define (filter pred seq) (cond ((null? seq) '()) ((pred (car seq)) (cons (car seq) (filter pred (cdr seq)))) (else (filter pred (cdr seq))))) (define (range start end) (define (iter end seq) (if (> start end) seq (iter (- end 1) (cons end seq)))) (iter end '())) (define (accumulate fn init-value sequence) (if (null? sequence) init-value (fn (car sequence) (accumulate fn init-value (cdr sequence))))) (define (flatmap proc seq) (accumulate append '() (map proc seq))) (define (unique-triples n) (flatmap (lambda (i) (flatmap (lambda (j) (map (lambda (k) (list i j k)) (range 1 (- j 1)))) (range 1 (- i 1)))) (range 1 n))) (define (sum-triples n s) (define (sum-desired? triple) (= s (accumulate + 0 triple))) (define (make-sum-of-triple triple) (append triple (list (accumulate + 0 triple)))) (map make-sum-of-triple (filter sum-desired? (unique-triples n))))
> (sum-triples 5 9)
((4 3 2 9) (5 3 1 9))
> (sum-triples 5 10)
((5 3 2 10) (5 4 1 10))
> (sum-triples 5 11)
((5 4 2 11))
> (sum-triples 5 12)
((5 4 3 12))
> (sum-triples 5 13)
()
> (sum-triples 6 13)
((6 4 3 13) (6 5 2 13))
> (sum-triples 6 10)
((5 3 2 10) (5 4 1 10) (6 3 1 10))
> (sum-triples 6 11)
((5 4 2 11) (6 3 2 11) (6 4 1 11))
> (sum-triples 6 12)
((5 4 3 12) (6 4 2 12) (6 5 1 12))
> (sum-triples 6 13)
((6 4 3 13) (6 5 2 13))
Exercise 2.40 of SICP
Exercise 2.40: Define a procedure unique-pairs that, given an integer n, generates the sequence of pairs
with
. Use unique-pairs to simplify the definition of prime-sum-pairs given above.
Using Miller-Rabin primality test as well as taking advantage of some lexical scoping here is the solution:
(define (prime? n times) (define (miller-rabin-test n) (define (random n) (random-integer n)) (define (expmod base exp m) (define (square-mod x) (remainder (* x x) m)) (define (square-signal-root x) (if (and (not (or (= 1 x) (= x (- m 1)))) (= 1 (square-mod x))) 0 (square-mod x))) (cond ((= exp 0) 1) ((even? exp) (square-signal-root (expmod base (/ exp 2) m))) (else (remainder (* base (expmod base (- exp 1) m)) m)))) (define (try-it a) (= (expmod a (- n 1) n) 1)) (try-it (+ 1 (random (- n 1))))) (cond ((= times 0) #t) ((miller-rabin-test n) (fast-prime? n (- times 1))) (else #f))) (define (filter pred seq) (cond ((null? seq) '()) ((pred (car seq)) (cons (car seq) (filter pred (cdr seq)))) (else (filter pred (cdr seq))))) (define (range start end) (define (iter end seq) (if (> start end) seq (iter (- end 1) (cons end seq)))) (iter end '())) (define (flatmap proc seq) (define (accumulate fn init-value sequence) (if (null? sequence) init-value (fn (car sequence) (accumulate fn init-value (cdr sequence))))) (accumulate append '() (map proc seq))) (define (unique-pairs n) (flatmap (lambda (i) (map (lambda (j) (list i j)) (range 1 (- i 1)))) (range 1 n))) (define (prime-sum-pairs n) (define (make-pair-sum pair) (list (car pair) (cadr pair) (+ (car pair) (cadr pair)))) (define (prime-sum? pair) (prime? (+ (car pair) (cadr pair)) 10)) (map make-pair-sum (filter prime-sum? (unique-pairs n))))
> (prime-sum-pairs 1)
()
> (prime-sum-pairs 2)
((2 1 3))
> (prime-sum-pairs 3)
((2 1 3) (3 2 5))
> (prime-sum-pairs 4)
((2 1 3) (3 2 5) (4 1 5) (4 3 7))
> (prime-sum-pairs 5)
((2 1 3) (3 2 5) (4 1 5) (4 3 7) (5 2 7))
Exercise 2.39 of SICP
Exercise 2.39: Complete the following definitions of reverse (exercise 2.18) in terms of fold-right and fold-left from exercise 2.38:
(define (reverse sequence) (fold-right (lambda (x y) <??>) nil sequence)) (define (reverse sequence) (fold-left (lambda (x y) <??>) nil sequence))
(define (fold-right fn init-value items) (if (null? items) init-value (fn (car items) (fold-right fn init-value (cdr items))))) (define (fold-left op initial sequence) (define (iter result rest) (if (null? rest) result (iter (op result (car rest)) (cdr rest)))) (iter initial sequence)) (define (reverse-right sequence) (fold-right (lambda (x y) (append y (list x))) '() sequence)) (define (reverse-left sequence) (fold-left (lambda (x y) (cons y x)) '() sequence))
> (reverse-right (list 1 2 3))
(3 2 1)
> (reverse-left (list 1 2 3))
(3 2 1)
Exercise 2.38 of SICP
Exercise 2.38: The accumulate procedure is also known as fold-right, because it combines the first element of the sequence with the result of combining all the elements to the right. There is also a fold-left, which is similar to fold-right, except that it combines elements working in the opposite direction:
(define (fold-left op initial sequence) (define (iter result rest) (if (null? rest) result (iter (op result (car rest)) (cdr rest)))) (iter initial sequence))
What are the values of
(fold-right / 1 (list 1 2 3))
3/2
(fold-left / 1 (list 1 2 3))
1/6
(fold-right list nil (list 1 2 3))
(1 (2 (3 ())))
(fold-left list nil (list 1 2 3))
(((() 1) 2) 3)
Give a property that op should satisfy to guarantee that fold-right and fold-left will produce the same values for any sequence.
(fold-right op i (list a1 a2 a3))

(fold-left op i (list a1 a2 a3))

Any binary associative operation will be invariant under fold-right and fold-left.
Since 2.37 involved matrix multiplication which is associative, I will use that as an example.
> (define i (list (list 1 0 0) (list 0 1 0) (list 0 0 1)))
> (define a1 (list (list 8 3 2) (list 1 0 9) (list 3 4 5)))
> (define a2 (list (list 5 6 7) (list 1 2 8) (list 6 7 7)))
> (define a3 (list (list 6 7 9) (list 4 3 1) (list 3 4 5)))
> (fold-left matrix-*-matrix i (list a1 a2 a3))
((884 965 1033) (840 900 950) (802 878 942))
> (fold-right matrix-*-matrix i (list a1 a2 a3))
((884 965 1033) (840 900 950) (802 878 942))
Notice that i is the identity matrix because if it wasn't, I would also be testing commutativity of matrix product. Matrix product doesn't commute. If i is changed to another matrix fold-left and fold-right will return differing results.
































