-
;What is the first term in the
-
;Fibonacci sequence to contain 1000 digits?
-
(define (square x) (* x x))
-
-
(define (pow man exp)
-
(cond
-
((= exp 0) 1)
-
((< exp 0) (/ 1 (pow exp (- man))))
-
((even? exp) (square (pow man (/ exp 2))))
-
((odd? exp) (* man (square (pow man (/ (- exp 1) 2)))))
-
(else #f)))
-
-
(define (prob25 digits)
-
(let ((big-const (pow 10 (- digits 1))))
-
(let fib ((first 1) (second 1) (count 1))
-
(if (>= first big-const)
-
count
-
(begin
-
;(display first) (display " ") (display count) (newline)
-
(fib second (+ first second) (+ count 1))
-
)))
-
)
-
)
-
-
(prob25 1000)
Archive for November, 2008
Euler problem 25
Friday, November 21st, 2008Coding C
Thursday, November 13th, 2008I’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, 2008I’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.
-
(define sum
-
(lambda (x) (apply + x)))
-
(define product
-
(lambda x (apply * x)))
-
(define sum-1-to-n
-
(lambda (x) (* (/ 1 2) x (+ x 1))))
-
(define (1+ x) (+ x 1))
-
(define (-1+ x) (- x 1))
-
-
(let
-
((smalllist '(3 5 6 9 10 12 15))
-
(num15s (quotient 1000 15)))
-
"(* num15s 15) -> 990, so we add the last few by hand"
-
(+ (* 15
-
(length smalllist)
-
(sum-1-to-n (-1+ num15s)))
-
(* num15s (sum smalllist))
-
993 995 996 999))
Continuing project, programming contest, perl
Sunday, November 2nd, 2008The 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.