Help translating K

At the moment I’m trying to learn Q via projecteuler.net problems.  Since there are no Q solutions I’m trying to get some feedback by comparing my solution to the often shorter K solutions that are posted in the forum.  Take for example

http://projecteuler.net/problem=46

The K solution is a fairly compact

p:+{:[x<4;,2;r,1_&{@[x#1;y*!:'-_-x%y;:;0]}r:_f@-_-_sqrt x]}@_1e6

n@&{~|/{x=_ x}_sqrt .5*x-/:(p _bin x)#p}'n:(3+2*!10000)_dvl p

Unfortunately I cannot run this in my interpreter (the solution was posted in 2005 so perhaps language changes?) so I’m having trouble translating to Q. My Q solution is quite clunky in comparison

isPrime:{ if[2=x;:1b]; $[0=x mod 2;:0b;:min 0<>x mod 2+til -1+ceiling sqrt x];};

comp:{(not isPrime) & 1=x mod 2}

a: 1+ til 10000

cmps: a where comp each a

prms: asc a where isPrime each a

lsth: {prms where prms<x}

rmd:{x-lsth}

issq:{sqrt rmd%2}

conj:  {0<count a where (floor each a)=a:issq}

b: cmps where not conj each cmps

1#b

Would somebody be able to help me translate the two lines of K into Q so I can compare and start to see whereabouts I can begin making my code more compact. 

thanks

Hi Don,

i calculated the solution with k 3.0 and tested it via projecteuler.net. (5x7x)

So if you are able to get this k version you can at least run the code.
Moreover learning k 3.0 code wont help you to improve your knowledge about q since it is not working in k 4.0 anymore.

Best,

Kim

Am 27.11.2011 10:31, schrieb Don Nguyen:

At the moment I’m trying to learn Q via projecteuler.net problems.  Since there are no Q solutions I’m trying to get some feedback by comparing my solution to the often shorter K solutions that are posted in the forum.  Take for example

http://projecteuler.net/problem=46

The K solution is a fairly compact

p:+{:[x<4;,2;r,1_&{@[x#1;y*!:'-_-x%y;:;0]}r:_f@-_-_sqrt x]}@_1e6

n@&{~|/{x=_ x}_sqrt .5*x-/:(p _bin x)#p}'n:(3+2*!10000)_dvl p

Unfortunately I cannot run this in my interpreter (the solution was posted in 2005 so perhaps language changes?) so I’m having trouble translating to Q. My Q solution is quite clunky in comparison

isPrime:{ if[2=x;:1b]; $[0=x mod 2;:0b;:min 0<>x mod 2+til -1+ceiling sqrt x];};

comp:{(not isPrime) & 1=x mod 2}

a: 1+ til 10000

cmps: a where comp each a

prms: asc a where isPrime each a

lsth: {prms where prms<x}

rmd:{x-lsth}

issq:{sqrt rmd%2}

conj:  {0<count a where (floor each a)=a:issq}

b: cmps where not conj each cmps

1#b

Would somebody be able to help me translate the two lines of K into Q so I can compare and start to see whereabouts I can begin making my code more compact. 

thanks

On Nov 27, 2011, at 4:31 AM, Don Nguyen wrote:

> At the moment I’m trying to learn Q via projecteuler.net problems.
Since there are no Q solutions I’m trying to get some feedback by
comparing my solution to the often shorter K solutions that are posted
in the forum. Take for example
>
> http://projecteuler.net/problem=46
>
> The K solution is a fairly compact
>
> p:+{:[x<4;,2;r,1_&{@[x#1;y*!:'-_-x%y;:;0]}r:_f@-_-_sqrt x]}@_1e6
> n@&{~|/{x=_ x}_sqrt .5*x-/:(p _bin x)#p}'n:(3+2*!10000)_dvl p

that’s k3 (the _ functions are the easiest way to tell – k4 & q don’t
have any english-word functions that start with an underscore

in k4:

q)p:1,{$[x<4;enlist 2;r,1_where{@[x#1;y*til each ceiling
x%y;:;0]}r:.z.s ceiling sqrt x]}floor 1e6
q)n where{not any{x=floor x}sqrt .5*x-(p bin x)#p}each n:(3+2*til
10000)except p
5777 5993

in q:

q)p:1,{$[x<4;enlist 2;r,1_where{@[x#1;y*til each ceiling
x%y;:;0]}r:.z.s ceiling sqrt x]}floor 1e6
q)n where{not any{x=floor x}sqrt .5*x-(p bin x)#p}each n:(3+2*til
10000)except p
5777 5993

(note i had to add 1 to the list of “primes” otherwise you get false
positives on 9 & 33)=

Thanks for the references and translation.  Helps me see where I can tidy up my code and style