Oops: I had scaled it to 1000 (also runs in under .1 seconds). Here is
the right code:
(define (euler29d)
(- (sqr 399)
(for/sum ([base '(2 3 5 6 7 10 11 12 13 14 15 17 18 19 20)])
(define lst
(for*/list ([exp (stop-before (in-naturals 1) (λ (exp) (>
(expt bas
Thanks for more food for thought.
Cristian, I really like the form of your solution with the answer being
produced at the bottom. You also taught me about stop-before and
stop-after. Very nice.
Maxim, Thanks for bringing up for*/set. Your solution is elegant, but
unfortunately is relatively sl
What about for*/set ?
#lang racket
(require racket/set)
(define (generate-powers lower upper)
(for*/set ([a (in-range lower (add1 upper))]
[b (in-range lower (add1 upper))])
(expt a b)))
(set-count (generate-powers 2 5))
(set-count (generate-powers 2 100))
Best regards
Hi Joe,
Thanks for the tip on on values in for*/fold. That should be added to docs.
I took a stab at converting your example into something more
functional. I'm too new to Racket to claim it's idiomatic. I mostly
went in and tried to remove what I considered iterative idioms; things
like do, let
Cristian,
One more thing, since for*/fold in your code only takes one accumulator,
you don't need "values" at all. Makes the code even shorter!
-Joe
On Tue, Apr 17, 2012 at 12:54 PM, Joe Gilray wrote:
> Hi Guys.
>
> Thanks for the education on for/fold and for*/fold.
>
> Cristian, your soluti
Hi Guys.
Thanks for the education on for/fold and for*/fold.
Cristian, your solution is very elegant, thanks, but unfortunately it does
generate all the numbers so is relatively slow and won't scale too well
with a and b.
-Joe
On Tue, Apr 17, 2012 at 11:01 AM, Cristian Esquivias <
cristian.esqu
I used Project Euler to try out new languages as well. Here was my
attempt at Problem 29 for reference.
(define (prob29 a b)
(set-count
(for*/fold
([nums (set)])
([i (in-range 2 (add1 a))]
[j (in-range 2 (add1 b))])
(values (set-add nums (expt i j))
- Cristian
On
Blindly refactoring the code, I'd use `for/fold' and add a `lst'
accumulator to `loop':
(define (euler29c)
; calculate 99^2 - duplicates
(- (sqr 99)
(for/sum ([d '(2 3 5 6 7 10)])
(let loop ([lst '()] [exp 1])
(if (> (expt d exp) 100)
(- (length lst) (length
Hi,
To continue our conversation about creating idiomatic Racket code, here is
some code I wrote last night to solve projecteuler.net problem #29:
(define (euler29a)
; calculate 99^2 - duplicates
(- (sqr 99)
(for/sum ([d '(2 3 5 6 7 10)])
(let ([lst '()])
(l
9 matches
Mail list logo