Project Euler Code Golf - kevinlawler/kona GitHub Wiki
Project Euler Problems
Rule I. If any Euler problem is unsolved and you solve it, you go on the board
Rule II. If you devise a solution shorter than what’s on the board, you may take that place on the board
Rule III. If you implement an agreed-upon change to the language, you get credit for all shortened solutions
| 1 |
+/&~&/(!1e3)!/:3 5 (Explanation: PE1 Explained) |
Kevin | ||||
| 2 |
|
isawdrones | ||||
| 3 |
|/d@&&/'2_'f'd:&~(f:{x!'!1+_sqrt x})600851475143Works for this case, but not in general. Returns 3 for largest factor of 15. |
isawdrones | ||||
| 4 |
|/b@&{x~|x}'$b:,/a*/:a:!1000Faster: |/b@&{x~|x}'$b:?,/a*/:a:100+!900
|
isawdrones | ||||
| 5 |
Euclid’s algorithm:{x*y%{:[y;_f[y]x!y;x]}[x]y}/1+!20Above solution works in Kona, but not in k2.8 or k3.2 {x*y%{:[_ y;_f[y]x!y;x]}[x]y}/1+!20This solution works in Kona, k2.8 and k3.2 |
isawdrones | ||||
| 6 | {((+/!x)^2)-+/(!x)^2}101 |
BonucciAndrea | ||||
| 7 |
p:{:[x<4;,2;r,1_&~|/x#'~!:'r: _f[_ _ceil _sqrt x]]}b:2;while[1e4>#a:p b*:2];a[10000]Alternative: p:2;i:3;while[10001>#p;if[{&/x!'p} i;p,:i];i+:1];*|pi+2 cuts computation time in half and still arrives at the correct answer. removed 3 characters from the previous shortest. p:2;i:3;while[10001>#p;:[&/i!'p;p,:i];i+:2];*|p |
isawdrones, nrapport, carkat | ||||
| 8 | |/*/'{x@(!5)+/:!-4+#x}0$',/0:` |
isawdrones | ||||
| 9 |
Brute force:*/_*c@&1000=+/'c:b,'(_sqrt+/)'b*b:,/a,/:\:a:!500
|
isawdrones | ||||
| 10 | p:{2_&{:[x@y;x&@[1,-1_ z#(1_ y#1),0;y;:;1];x]}/[x#1;2_! __ceil _sqrt x;x]};+/p@_2e6 |
Kevin | ||||
| 11 |
p:.:'0:`d:{x ./:/:m@-1_1_=+/'m:,/n,/:\:n:!#x}i:|/,/{:[4>#x;x;*/'x@(!4)+/:!-3+#x]}'|/(i p;i@+p;i d@p;i d@|p)
|
isawdrones | ||||
| 12 |
Brute force:f:{1__ x*(x+1)%2};c:1;{x<500}{#{d:&~x!'!1+_sqrt x;d,_ x%|d}f c+:1}\1;f@c
|
hakank | ||||
| 13 |
\p 0
|
isawdrones | ||||
| 14 |
clz:{:[x!2;1+3*x;_ x%2]}
a?|/a:{C:-1 1,(x-2)#0
do[x+i:0
if[~C@i
b:&x>j:((i-1)<)clz\i
C[j@b]:C[*|j]+(|!#j)@b]
i+:1]
C}@_1e6
Source
|
isawdrones | ||||
| 15 |
|
isawdrones danbst |
||||
| 16 | +/1000{x*:2;(*&x)_ x:((x>9),0)+0,x!/:10}/1 /slower: !' |
Kevin | ||||
| 17 |
a:$`one`two`three`four`five`six`seven`eight`nine b:$`ten`eleven`twelve,($`thir`four,v:`fif`six`seven`eigh`nine),\:"teen" c:($`twen`thir`for,v),\:"ty" g:a,b,c,/c,/:\:a h:a,\:"hundred" i:g,h,/h,/:\:"and",/:g z:#,/$i,`onethousand |
Kevin | ||||
| 18 | |/{(y+0,x)|y+x,0}/.:'0:`t.txt /shorter 0:` |
Kevin | ||||
| 19 | +/{(&/"01"=-2#$_dj x)&6=-x!7}'from+!-((from:_(_jd 19010101))-(_jd 20001231)) |
hakank | ||||
| 20 |
|
kuentang | ||||
| 21 |
f:{d:&~x!'!1+_sqrt x;d,_1_ x%d};+/&{(~&/x=b)&x~*|b:2{+/f x}\x}'!1e4Code above not working for some reason; below is another solution (longer): d: {+/(!1+_ceil x%2)*(0=x!/:!1+_ceil x%2)};d 284;n : !10000;a:d' n
b:d' a
j:n*(b=n);j:j@&1<j;j:j@&0<_sqr j-d' j;+/j
|
BonucciAndrea &isawdrones | ||||
| 22 | +/s*1+!#s:+/'-64+_ic m@<(-|/#:'m)$'m:.:'1_'(0,1_&","=n)_ n:",",,/0:`names.txt |
isawdrones | ||||
| 23 |
l:28123;n:l#1 / (l)imit, (n)ot sum of abundant{n[x@&l>x]:0}'a+/:a:1_&{x<+/{?1,d,x%d:c@&~x!'c:1+1_!_sqrt x} x}'!l / (a)bundant+/&n
|
inrick | ||||
| 24 | ({:[1<x;,/(>:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]}10)[_1e6-1] |
hakank | ||||
| 25 |
|
juster | ||||
| 26 | 1+*>#:'{[n] {r:(10**|x)!*x; w:x?r; :[w=#x;x,r;(*x),w _ x]}/(n,1)}'1+!999 |
adamschmideg | ||||
| 27 |
I have a solution in A+. It's reasonably fast (900 ms),
but visually clunky, and the APL characters won't display here.
A remarkably elegant solution in J (from Oleg Kobchenko):
pr=: 1: = #@q:"0
pc=: 4 : '(>:^:((pr@((x,y,1)&p.)) :: 0:))^:_] 1'
($ #: (i. >./)@,)(p:i.168)pc"0/i:999
163 938
(p:163)*938{i:999
_59231
|
tavmem | ||||
| 28 | +/+\1,,/4#'2*1+!_1001%2 |
isawdrones | ||||
| 29 | {#(?,/x^/:x)}2_!101 |
BonucciAndrea | ||||
| 30 | +/2_&{x=+/(0$'$x)^5}'!7*9^5 |
hakank | ||||
| 31 | |
paul7 | ||||
| 32 | +/?,/,/'(!10000;!1000){r*"123456789"~{x@<x}@,/$x,y,r:x*y}/:\:'(1+!9;11+!89) |
nrapport | ||||
| 33 |
|
nrapport | ||||
| 34 | +/2_&{x=+/{*/1+!x}'0$'$x}'!50000 |
hakank | ||||
| 35 |
p:{&/ 2_ x!' !1+_ _sqrt x} q:{*/ p' 0$(1!)\ $x} #2_ v@&q' v:!1000000
|
nrapport | ||||
| 36 |
+/&{&/{y~|y:x _vs y}'[2 10;x]}'!1e6Faster: p:{&{x~|x}'x@y};+/n@p[2_vsx]n:p[$:;!1e6]
|
Kevin | ||||
| 37 | p:{:[x<4;,2;r,1_&{@[x#1;y*!:'-_-x%y;:;0]}[x]r:_f@-_-_ceil(1+(x%2))]}@_1e6;g:5_?p*{(#$p@x)=+/($p@x)_lin"123579"}' !#p
f:{a::(#$x)#x;(2*(#a))=((+/({(x=2)+(x>1)*(&/{x!/:2_!1+_ceil_sqrt x}x)}'{. x}'{|(|$a@x)[!1+x]}'!#$x))+(+/({(x=2)+(x>1)*(&/{x!/:2_!1+_ceil_sqrt x}x)}'{. x}'{($a@x)[!1+x]}'!#$x)))}
(+/g*(f' g)) |
BonucciAndrea | ||||
| 38 | 0$,/$*|v@&{(,/$1+!9)~a@<a:,/$x}'v:(9e3+!_1e3)*\:1 2 |
nrapport | ||||
| 39 |
r is all permutations f eliminates tuples with 0 t is all triangles a is all triangles grouped by perimeter take the first perimiter ordered by count of triangles abc:{c:_sqrt(x^2)+y^2;x,y,:[c=(_ c);_ c;0]}{**a[>#:'a:t[&~1=#:'t:{f:r[&3=+/'~0='r:{abc/',/x,/:\:x}x;];{x,f[&{x=+/y}[x]'f]}'x}x]]}(!1000)
|
carkat | ||||
| 40 | */ . 1_(,/" ",'((,/ $ !2e5)@_10^!7)) |
BonucciAndrea | ||||
| 41 | prime : {&/x!/:2_!(_ceil _sqrt x)}; prime 7654321;
p : {:[1<x;,/(>:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]}; perm : {x@p@#x}
a : {. x}' perm "7654321"; |/ a*(prime' a) |
BonucciAndrea | ||||
| 42 |
m:.:'1_'(0,1+&","=n)_",",n:*:0:`words.txt+/{a=_ a:_sqrt 1+8*x}'+/'-64+_ic m@<(-|/#:'m)$'m
|
isawdrones | ||||
| 43 | pr : 2 3 5 7 11 13 17;p : {:[1<x;,/(>:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]};
perm : {x@p@#x}; a : {. x}' perm "9876543210";
b : {+/ {[x]({[y]. 3#y!1_ $x}'!7)!'pr}x}' a;
+/ a*(b=0) |
BonucciAndrea | ||||
| 44 | \p 0;
t:{(x*((3*x)-1))*%2};a:t'1_!25e2;
*1_?,/{{((a+x)_lin a)*((a-x)_lin a)*(_abs(a-x))}'x}a |
BonucciAndrea | ||||
| 45 | *|{x@&x _lin y}/({x*((3*x)-1)*%2};{x*((2*x)-1)})@\:!_4e4 |
BonucciAndrea | ||||
| 46 |
p:2 3 5 7c:{|/a=_ a:_sqrt %2%x-p} q:{&/x!'p@&p<1+_ _sqrt x}i:9;d:0;while[~d;if[q i;p,:i];if[~c i|i=*|p;d:1];i+:2];i-2
|
nrapport | ||||
| 47 | p : {1_?{x@<x}({&/{x!/:2_!x}x}'2_!x)*2_!x}1000
f : {+/0=x!'p};f 0;ss:{z[&y[z+\:!#x]~\:x]};
*ss["4444";,/$f'!15e4;&((1-#"4444")_,/$f'!15e4)=*"4444"]-2 |
BonucciAndrea | ||||
| 48 |
|
kuentang | ||||
| 49 | p : {&/x!/:2_!x};p 0;
perm:{?({. x}'x@{:[1<x;,/(>:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]}@#x)*(p'{. x}'x@{:[1<x;,/(>:'(x,x)#1,x#0)[;0,'1+_f x-1];,!x]}@#x)};
b : {x,(x+3330), x+6660}' 100_!3340; a : perm' $100_!3340
c:{4= #?(((a@x) _lin (b@x)) * (a@x))}'!3240; .,/ $ b@(,/ 2_ ?c*!3240) |
BonucciAndrea | ||||
| 50 | ? |
? | ||||
| 51 | ? |
? | ||||
| 52 | *1_?(!2e5)*{a::{{x@<x}'${x*'1+!6}x}x;6=+/{(a@x)~(a@0)}'!6}'!2e5 |
BonucciAndrea | ||||
| 53 | +/~1e6>,/{x{+':0,x,0}\1.}100 |
isawdrones | ||||
| 54 | ? |
? | ||||
| 55 | 50+/0={a::x;i::0;while[(0=(x=.|$x))*(i<50);a,:x:{_abs(x+.|$x)}x;i+:1];-1+#a}'!1e4 |
BonucciAndrea | ||||
| 56 | ? |
? | ||||
| 57 | ? |
? | ||||
| 58 | ? |
? | ||||
| 59 | p:,/_ic j:(485 3#(_ci . ","/0 : "0059_cipher.txt"))
a:{x@>x}(?{((+/p=p[x]),p[x])}'!#p);k: _ic (1!_ci {a[x][1]+32}'!3)
xor:{[x;y]{a::2 _vs x;b::2 _vs y;while[(#b)<(#a);b:0,b];2 _sv (~(a=b))}[|/(x,y);&/(x,y)]}
+/,/{xor'[(_ic j[x]);k]}'!#j |
BonucciAndrea | ||||
| 60 | ? |
? | ||||
| 61 | ? |
? | ||||
| 62 | ? |
? | ||||
| 63 | 1+/,/{[x]{[x;y]y=(#$(x^y))}'[x;!20]}'!20 |
BonucciAndrea | ||||
| 64 | ? |
? | ||||
| 65 | ? |
? | ||||
| 66 | ? |
? | ||||
| 67 | |/{(y+0,x)|y+x,0}/.:'-1_'0:`triangle.txt |
Kevin | ||||
| 68 | ? |
? | ||||
| 69 | ? |
? | ||||
| 70 | ? |
? | ||||
| 71 | {a:2;c:5;while[~x<c+7;a+:3;c+:7];a}1e6 |
isawdrones | ||||
| 72 | ? |
? | ||||
| 73 | ? |
? | ||||
| 74 | ? |
? | ||||
| 75 | ? |
? | ||||
| 76 | ? |
? | ||||
| 77 | ? |
? | ||||
| 78 | ? |
? | ||||
| 79 | ? |
? | ||||
| 80 | ? |
? | ||||
| 81 | ? |
? | ||||
| 82 | ? |
? | ||||
| 83 | ? |
? | ||||
| 84 | ? |
? | ||||
| 85 | r:{{{[x;y] x*y*(x+1)*(y+1)*%4}\:[x;x]}[1+!x]}
a:r 1000;b:,/ a;l:b@&b>1990000;l:l@&l<2010000;l:_abs l-2e6;
i:(b?(2e6-(&/l)))&(b?(2e6+(&/l)));(1+i!(#a))*(1+_floor i%(#a)) |
BonucciAndrea | ||||
| 86 | ? |
? | ||||
| 87 | ? |
? | ||||
| 88 | ? |
? | ||||
| 89 | A general solution with roman-arabic and arabic-roman number conversion |
adamschmideg | ||||
| 90 | ? |
? | ||||
| 91 | ? |
? | ||||
| 92 | ? |
? | ||||
| 93 | ? |
? | ||||
| 94 | ? |
? | ||||
| 95 | ? |
? | ||||
| 96 | ? |
? | ||||
| 97 | {a:28433;do[x;a:(a*2)!1e10];a+1}7830457 |
BonucciAndrea | ||||
| 98 | ? |
? | ||||
| 99 | t:0:"base_exp.txt";n:+{{. x}'","\(t@x)}'!#t;a:f[n@0;n@1];a?(|/a) |
BonucciAndrea | ||||
| 100 | ? |
? | ||||
| 119 | \p 0
i : (? ,/ (!70) ^/: (!10));
f : {|/ x=(+/. 1_(,/" ",'$x))^!10}; f 512;
i : i * f' i; i : i @& 0 < i; i : 9_{x@<x}i; i@29 |
BonucciAndrea | ||||
| 206 | f:{s:.,/" ",'$x;m:~(#s)#1 0;s:s _di(((!#$s)*m)_dv0);.,//" "\'$s}
{while[~+/a:1234567890=f'*/2 2#(b:((2#x)-50 90));x-:100];+/a*b}1389026620 |
BonucciAndrea | ||||
| 301 | |/{x{x,+/-2#x}/!2}31 |
BonucciAndrea |