Requesting explanation for project eulers code golf.

42 views
Skip to first unread message

KLearner

unread,
May 17, 2018, 12:11:00 PM5/17/18
to Kona Users
{x*y%{:[y;_f[y]x!y;x]}[x]y}/1+!20

apply a reduce function to a list from 1 to 20

f(x,y):= x *y % f1(x,y)

The function f, reduces 1 to 20. 
f sees x times y divided by the result of the inner f1 applied to each x y where:

f1(x,y):=  if(y) then  recurse on f1(y,x mod y)  else  x

I think I'm reading that right?
[bool; event1; event2;...;else]?


Is that what the
:[...]

syntax is all about?

Why does this work?

KLearner

unread,
May 17, 2018, 12:13:03 PM5/17/18
to Kona Users
and one other question, why doesn't 
{x*y%  x{...}y}/1+!20

work for applying two arguments to the inner function?

KLearner

unread,
May 17, 2018, 12:40:55 PM5/17/18
to Kona Users
I'm tyring to use what I know from the Q docs, 0N! inline will show the value to the right before continuing to pass it along to the left. Is there any command like that?

KLearner

unread,
May 17, 2018, 12:53:39 PM5/17/18
to Kona Users
[...Array(20).keys()].map(x=> x + 1).reduce((a,b)=> a*b / (f = (x,y) => y ? f(y, x%y) : x)(a,b))

So yes, this works in javascript. Still trying to figure out why it works. 
Message has been deleted

KLearner

unread,
May 17, 2018, 1:26:51 PM5/17/18
to Kona Users
It looks like reducing x * y divides the GCD produces the LCM, and recursively applying mod to y appears to produce the GCD. 

I don't fully understand how that works,but it appears to be the case looking at each iteration. 

Anyone know how to inline print in Kona? I'm still not finding it in the \ menu. 

douglas mennella

unread,
Dec 13, 2021, 4:11:07 AM12/13/21
to Kona Users
This is pretty old, but just in case someone else comes along, I believe you're reading it right.  Your long hand version should be enough to understand the algorithm.  :[...] is "if".  It's a control structure whose help is available with \..  The / at here represents converge because the function takes two arguments.  It applies the function to the first two elements and then to the result of that with the next element, etc.  If you use \ instead you can see all the intermediate results.  I haven't used "trace" mentioned below but that may also give you similar insight.  I'd look it up in the K2 manual linked in the wiki.

 \.
...
Control Flow
:[x1;t1;x2;t2;...;xn;tn;else] evaluate xi until true and return ti, otherwise return else
    :[0;10;0;20;1;30;40] yields 30
if[x;e1;...;en] if x then evaluate all e. if[j>i;a:1;b:2]
do[m;e1;...;en] do all e m times. do[100;f[i];i+:1]
while[x;e1;...;en] while x do e.  while[a>b; f a; a-:1]
/ starts a comment. Must begin a line or have a space before
\ is trace when beginning an expression inside a function (todo)
: is early return when beginning an expression inside a function
' is signal (todo)

  {x*y%{:[y;_f[y]x!y;x]}[x]y}/1+!20
232792560
  {x*y%{:[y;_f[y]x!y;x]}[x]y}\1+!20
1 2 6 12 60 60 420 840 2520 2520 27720 27720 360360 360360 360360 720720 12252240 12252240 232792560 232792560

Reply all
Reply to author
Forward
0 new messages