[plt-scheme] Project Euler was Hash Table implementation
Grant Rettke wrote:
> On 8/3/07, Mark Engelberg <mark.engelberg at gmail.com> wrote:
>> Anyone else want to comment on the relative strengths of languages for
>> Project Euler?
The numeric tower of R5RS is really shining when solving these problem.
Here is how I find reals solution of a second degree equation:
(define (second-degree-solutions a b c)
; return list of solutions to a a x^2 + b x + c = 0
(let ([d (- (* b b) (* 4 a c))])
(cond
[(< d 0)
'()]
[(= d 0)
(list (/ b (* -2 a)))]
[else
(let ([sqrt-d (sqrt d)])
(list (/ (- (- b) sqrt-d) (* 2 a))
(/ (+ (- b) sqrt-d) (* 2 a))))])))
For example the equation 2x^2-8=0 has the solutions -2 and 2:
> (second-degree-solutions 2 0 -8)
(-2 2)
The thing to notice is that I get exact solutions, even
though I used sqrt above.
Finding *integer* solutions is now easy:
(define (second-degree-integer-solutions a b c)
; return list of integer solutions to a x^2 + b x + c = 0
(filter integer? (second-degree-solutions a b c)))
The tools I use to solve Project Euler problems are:
- the following library of number theory functions, released
through PLaneT today:
http://www.scheme.dk/blog/2007/08/number-theory.html
- Herman's memoize package (remember to use memoize*)
- Eager Comprehensions
> It is a lot of fun to read everyone's solution to the problems on
> project Euler.
Sure is.
--
Jens Axel Søgaard