Login [Register]
Don't have an account? Register now to chat, post, use our tools, and much more.
Below are six small code-golf brainteasers that I originally wrote for PPCG SE but are now off-topic for that site. The solutions mostly involve tricks that I've used or seen in the past.


Code:
Problem 1
Output the following list: {10,15,21,28}; that is, the 4th through 7th triangular numbers.

//15 bytes:
seq(X(X+1)/2,X,4,7

Problem 2
Given an integer in Ans representing an integer between 1 and 10000000, output the number of factors of 2 that divide it.

//31 bytes
Ans→X
0→Y
While X/2=int(X/2
Y+1→Y
X/2→X
End
Y

Problem 3
Given a list with 2 or more elements in Ans, output 0 if all of its elements are the same, and a nonzero value if any of its elements are different.

//6 bytes
max(Ans≠Ans(1

Problem 4
Given a list in Ans of at least 4 integers from 1 to 6, determine if there is a sublist of four elements that form either two pairs or four of a kind: for example, 5,5,6,6, 1,4,4,1, and 3,3,3,3 are all sublists. Output a nonzero value iff there is at least one.

//??? bytes

Problem 5
Given a list in Ans of positive integers, calculate their harmonic mean.

//8 bytes
dim(Ans)/sum(1/Ans

Problem 6
Given a list of integers in Ans, find the sum of the elements greater than its minimum.

//10 bytes
sum(Ans*(Ans>min(Ans


Please hide any spoilers if you solve the problems.

EDIT: Fixed a couple of reference solutions.
Problem 1, 11 bytes: cumSum({10,5,6,7

Problem 2, 15 bytes: sum(not(fPart(Ans/2^cumSum(binomcdf(23,0 Explanation: This counts how many powers of 2 are factors of Ans, which is the same as the amount of factors of 2. It works because if X is not a factor of Ans, X*2 isn't either, so that's why I can just do sum(not(fPart(

Problem 3, 5 bytes: max(Ans≠max(Ans

Problem 4, 21 bytes: 3<sum(1<seq(sum(Ans=Ans(X)),X,1,dim(Ans Explanation: For each element it counts how many elements have the same value. If that's more than 1, there's a pair with the else element(s). However, since the other element has it as well, and there are 2 pairs, the amount of (sum of amount of elements with value of elemtn X) should be at least 4. This applies to 4 same elements as well, all 4 have 3 other elements.

Problem 5, 7 bytes: dim(Ans)/sum(Ans^^-1

Problem 6, 7 bytes: sum(Ansnot(Ans=min(Ans

Your answer to problem 2 is wrong, because While X=int(X/2 doesn't loop at all.

Your answer to problem 3 is wrong, it returns 1 when all elements are the same.
Problem 2, 16 bytes: :sum(not(fPart(Ans/seq(2^X,1,24

I had a suspicion it could be one-lined...

Remark: P_T and I seemed to have found this one at about the same time!
Remark 2: Looks like you can drop a byte on this solution. See P_T's answer for that one.

Problem 3, 5 bytes: max(abs(ΔList(Ans

Problem 5, 5 bytes: mean(Ans⁻)⁻
I'll just post an 8-byter for Problem 1 based on a wonderful trick I espied you using recently: timeCnv(919288

Then there's the overly-expensive now-passable solution 2412→rand:int(37rand(4, which I had Mathematica spit out for me, to boot.

No ingenuity here today. Just cheats.

PT_: Regarding the core of your Problem 2 solution, the argument should generally be one less than the intended dimension/maximum. While its counterpart goes from 0N, it itself will take on 1N+1.
Problem 4, 18 bytes: 2<=sum(iPart(.5seq(sum(Ans=X),X,1,6
Explanation: For each integer X from 1 to 6, count the number of elements equal to X. Floor-divide these counts by 2, so integers appearing 0 or 1 times produce 0, integers appearing 2 or 3 times produce 1, integers appearing 4 or 5 times produce 2, etc. Sum these divided counts and check if greater than or equal to 2.
Oops, I intended problem 4 to read
"Given a list in Ans of at least 4 integers from 1 to 6, determine if there are four *consecutive* elements that form either two pairs or four of a kind: for example, 5,5,6,6, 1,4,4,1, and 1,2,3,3,3,3 all qualify but 2,3,4,3,2 does not. Output a nonzero value if so and zero if not." Let's call the original version 4A and this 4B.

EDIT: Weregoose's solution for 1 was the intended solution. Runer's for 4A looks optimal. No one has optimal solutions for 2, 3, 4B, or 5 yet.
I think this works for 2:
Problem 2, 13 bytes: sum(not(fPart(Ans/2^randIntNoRep(1,23 Explanation: same as PT_'s solution, but it doesn't matter if they are in order or not

and I think this works for problem 3:
Problem 3, 4 bytes: min(ΔList(Ans

I haven't tested either one though.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are GMT - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement