Project Euler Code Golf - kevinlawler/kona GitHub Wiki

Project Euler Code Golf

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
Known bound: *+/{x*~x!2}32(|+\)\1 1
Unknown bound(b f/ x): +/{x@&~x!2}(4e6>+/-2#){x,+/-2#x}/1
isawdrones
3 |/d@&&/'2_'f'd:&~(f:{x!'!1+_sqrt x})600851475143
Works for this case, but not in general. Returns 3 for largest factor of 15.
isawdrones
4 |/b@&{x~|x}'$b:,/a*/:a:!1000
Faster:
|/b@&{x~|x}'$b:?,/a*/:a:100+!900
isawdrones
5 Euclid’s algorithm:
{x*y%{:[y;_f[y]x!y;x]}[x]y}/1+!20
Above solution works in Kona, but not in k2.8 or k3.2
{x*y%{:[_ y;_f[y]x!y;x]}[x]y}/1+!20
This solution works in Kona, k2.8 and k3.2
isawdrones
6 {_(_sqr+/a)-+/a*a}1+!100 isawdrones
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];*|p

i+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
10#($+/0.0$’0:`)_dv"."
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
(40{1+':x,0}/1)20
_*/(-2+4*x)%x:1+!20 – implementing formulae \(C_{2n}^n\)
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

xx: 1+til 100;
uc:{[x;y] tO:last[x]+y;aO:tO mod 10;bO:`int$(tO-aO) % 10;(-1_x),aO,bO}/[enlist 0;];
pp:{[x;y] mO:max count each rO:rO{[x;y] (y#0),x}'til count rO: y */: x;
          uc sum {[x;y] x,(y-count x)#0}[;mO] each rO };
lst:{[x] {parse x,"i"} each x} each reverse each string each xx
sum {[x;y] pp[x;y]}/[lst]

kuentang
21 f:{d:&~x!'!1+_sqrt x;d,_1_ x%d};+/&{(~&/x=b)&x~*|b:2{+/f x}\x}'!1e4
Code 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

c:{:[|/x>9;_f (x!10)+0,-1_ x%10;x]}
n:1000;i:2; {0=*|x[1]} {i+:1;{c[x+y]}\|x}/ (2,n)#0=!n; i

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 #?,/p^/:p:2+!99 silentbicycle
30 +/2_&{x=+/(0$'$x)^5}'!7*9^5 hakank
31
s:{+/{(y _ x),y#0}[x]'y*!_ceil(#x)%y+0.0}
e31:{*s/[(x#0),1;1 2 5 10 20 50 100 200]}
paul7
32 +/?,/,/'(!10000;!1000){r*"123456789"~{x@<x}@,/$x,y,r:x*y}/:\:'(1+!9;11+!89) nrapport
33

f:{x@&~|/’x=\:y}
g:{0$(f[$x;$y];f[$y;$x])}
check:{(1=&/#:’$a)&(b<1)&(b:x%y)=%/a:g[x;y]}
combos:{,/x,/:\:x}
v:a@&~0=(a:10 + ! 90)!10
g .’ a@&check .’ a:combos[v]
/do the remaining simplification by hand
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]}'!1e6
Faster:
p:{&{x~|x}'x@y};+/n@p[2_vsx]n:p[$:;!1e6]
Kevin
37 ? ?
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 ? ?
42 m:.:'1_'(0,1+&","=n)_",",n:*:0:`words.txt
+/{a=_ a:_sqrt 1+8*x}'+/'-64+_ic m@<(-|/#:'m)$'m
isawdrones
43 ? ?
44 ? ?
45 *|{x@&x _lin y}/({x*((3*x)-1)*%2};{x*((2*x)-1)})@\:!_4e4 BonucciAndrea
46 p:2 3 5 7
c:{|/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 ? ?
48
uc:{[x;y] tO:last[x]+y;aO:tO mod 10;bO:`int$(tO-aO) % 10;(-1_x),aO,bO}/[enlist 0;];
plus:{[x;y] mO:max count @/:(x;y) ;m:uc (x,(mO-count x)#0) 
+ (y,(mO-count y)#0);?[0=last m ;-1_m;m]};
pp:{[x;y] mO:max count each rO:rO{[x;y] (y#0),x}'til count rO: y */: x;
m:uc sum {[x;y] x,(y-count x)#0}[;mO] each rO ; ?[0=last m ;-1_m;m]};
lst:{[x;y] ll:{parse x,"i"} each reverse string y;
plus[x;{[x;y]tmp:pp[x;y]; ?[10>=count tmp;tmp;10#tmp] }/[y#enlist ll]]}/[enlist[0];1+til 1000];
reverse 10#last lst
kuentang
49 ? ?
50 ? ?
51 ? ?
52 ? ?
53 +/~1e6>,/{x{+':0,x,0}\1.}100 isawdrones
54 ? ?
55 ? ?
56 ? ?
57 ? ?
58 ? ?
59 ? ?
60 ? ?
61 ? ?
62 ? ?
63 ? ?
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
r:"IVXLCDM"
i:(!4)#\:0;i:(i,,0 1),(1,/:i),,0 2
ar:{d:10_vs x;,/d{r@(i+y)@x}'|(#d)#2*!4}
d:.+(`$/:r;*\1,6#5 2)
ra:{n:d[`$/:x];+/n*1-2*>':n,0}
answer:+/{r:-1_ x;r::["M"=*r;1_ r;r];(#r)-#ar ra r}/:0:"roman.txt"
/ "M" hack, because ar fails above 4000
adamschmideg
90 ? ?
91 ? ?
92 ? ?
93 ? ?
94 ? ?
95 ? ?
96 ? ?
97 ? ?
98 ? ?
99 ? ?
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
⚠️ **GitHub.com Fallback** ⚠️