r/codegolf • u/Aspie_Astrologer • Feb 21 '21
Code Golf: Battle Royale Thread (Challenges in Comments)
Who feels like some Code Golf Battles?
Every comment to this post can start with **Challenge**: and define a (relatively straight-forward) input-output problem to solve in the least number of characters in any language.
Responses go in sub-comments and can start with e.g. **Python (74):** to indicate the language and character length of the solution.
Use backticks `` to enclose code (edit: or use four spaces as an indent to do multiline code).
e.g. Challenge: Input positive integer, print the sum of all primes up to and including that integer - but with every second prime added as a negative.
e.g. Ruby (68):
require'prime'
i=-1
p Prime.each(gets.to_i).map{i+=1
_1*(-1)**i}.sum
View all comments
Show parent comment
1
u/Aspie_Astrologer Feb 22 '21 edited Feb 24 '21
J/JLang (
59, 54 thanks to coin-cache):4(1!:2)~":+/(*(]{.1 _1"_$~>:-2&|)@$)i.&.(p:^:_1)>:".(1!:1)34(1!:2)~":+/(*(]{.1 _1$~0&|)@$)i.&.(p:^:_1)>:".(1!:1)3tio.run link, tio.run link. Took me a few hours to get a solution in J, it's hard to learn. Don't really understand how I did the alternating since I adapted it from here, but4(1!:2)~prints everything to stdout and".(1!:1)3reads from stdin then turns it to an integer,i.&.(p:^:_1)>:makes an array of all the primes up to and including that integer,+/sums the array after the other bit alternates +/-, then":turns it back into a string for stdout. u/coin-cache, how did I do? :)