Archive for November, 2008

Euler problem 25

Friday, November 21st, 2008
  1. ;What is the first term in the
  2. ;Fibonacci sequence to contain 1000 digits?
  3. (define (square x) (* x x))
  4.  
  5. (define (pow man exp)
  6.   (cond
  7.     ((= exp 0) 1)
  8.     ((< exp 0) (/ 1 (pow exp (- man))))
  9.     ((even? exp) (square (pow man (/ exp 2))))
  10.     ((odd? exp) (* man (square (pow man (/ (- exp 1) 2)))))
  11.     (else #f)))
  12.  
  13. (define (prob25 digits)
  14.   (let ((big-const (pow 10 (- digits 1))))
  15.     (let fib ((first 1) (second 1) (count 1))
  16.       (if (>= first big-const)
  17.           count
  18.           (begin
  19.             ;(display first) (display " ") (display count) (newline)
  20.             (fib second (+ first second) (+ count 1))
  21.             )))
  22.     )
  23.   )
  24.  
  25. (prob25 1000)

Coding C

Thursday, November 13th, 2008

I’ve been taking a break from my other projects to learn C. Since I already know C++ fairly well, this is most fun when I write hideous C involving pointer arithmetic. Right now I’m writing an interpreter (or more accurately I’m starting by writing a tokenizer, which isn’t working yet and which I may not choose to go past.

More importantly, I’m doing the whole thing over ssh on a BSD box, thus learning *nix at the same time. Since I used about three commands, I’m not learning *much* but there you go.

Euler Problem 1

Monday, November 3rd, 2008

I’ve decided to do a series of posts with my solutions to the Euler problems. They’ll be tagged with “euler”, and once I figure out how I’ll stop them from showing up on the main page.

This is the first Euler Problem
  1. (define sum
  2.   (lambda (x) (apply + x)))
  3. (define product
  4.   (lambda x (apply * x)))
  5. (define sum-1-to-n
  6.   (lambda (x) (* (/ 1 2) x (+ x 1))))
  7. (define (1+ x) (+ x 1))
  8. (define (-1+ x) (- x 1))
  9.  
  10. (let
  11.     ((smalllist '(3 5 6 9 10 12 15))
  12.      (num15s (quotient 1000 15)))
  13.   "(* num15s 15) -> 990, so we add the last few by hand"
  14.   (+ (* 15
  15.         (length smalllist)
  16.         (sum-1-to-n (-1+ num15s)))
  17.      (* num15s (sum smalllist))
  18.      993 995 996 999))

Continuing project, programming contest, perl

Sunday, November 2nd, 2008

The project I’m working on I briefly mentioned last post is a deliberately simple roguelike set in a branching dungeon. It’s 60-70% done at current, and I’ll say no more until I at least get to final bugfixing.
I went to the ACM programming contest, in which our school placed very average. I had more fun that I expected from the day before and less than from the week before. I had lots of delicious free food. I’d do it again, overall, and be more interested in doing so if I knew my team well ahead of time.
I started learning perl, which looks exactly as disgusting as everyone says it is. It seems useful. It has very odd array/hash behavior which relate to its origin in string processing. An array of three arrays is just the contents of each of those three. To make the kind of thing I want, you need to make explicit reference variables. Interestingly, the fact that you can only pass one array (because they’re flattened) to a function means that everything is passed by value to functions. To my knowledge this is also true of the assignment operator. Since perl as I mentioned supports references, this is to my liking… the array flattening less so.

Regular expressions are as always kickass, and I understand perl’s engine is the best to be found. I really need to get a Linux system soon so I can do fun things like use the “grep” command or have very large text consoles.