[Jprogramming] Code review / Euler
Lau B. Jensen
lau.jensen at bestinclass.dk
Sun Dec 6 05:13:14 HKT 2009
Dear J-Community,
It has been remarked that solving Euler problems using Clojure is almost
cheating and I
followed up by saying that solving them using J felt like I was being
cheated :)
I stepped back, started reading 'Learning J' and retried. This time the
solutions come quicker
but feel...less than idiomatic. I'm hoping a few friendly J'ers will be
able to spot where Im going
wrong here.
#1: Find the sum of all multiples of 3 and 5 below 1000
+/~. (((0=3&|) # ]) i. 1000),(((0=5&|) # ]) i. 1000)
What I really dislike is that I'm repeating myself throughout, I want
3 5 (some func) i.1000
But how would I go about that?
#2: Sum of all fibs below 4 mill
Firstly the Fibonacci definition for Learning J throws a curved ball
near the end:
fib=: u ^: (v1 `v2)
Generally I understand that the gerund lets me pass multiple arguments
to a function which only
takes a single, but what is it doing here?
Well, I did the crime of continuing without understanding that, so
here's what I arrived at
+/ (#~ <&(4*1e6)) (((0=2&|) # ]) fib 40x)
And I think that's almost acceptable. My two main complaints is that I
have 2 parenthesised functions
lined up as a sequence-parser and I imagine that they would be coupled
some way, ie
+/ (take while < 4 mill and only the evens) fib 40x
Secondly, I couldn't figure out how to just let an infinite amount of
Fibonacci numbers return, so I hardcoded
40. fibs^:a: gave me a domain error for some reason.
#3: Largest primefactor of 600851475143
I'll admit, I felt like cheating on this one
>./q: 600851475143x
And thats as far as I got, because this simply makes no sense to me :)
((-:|.)@:":)"0 NB. Tests for palindrome
Thanks,
Lau
More information about the Programming
mailing list