Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

Check out this challenge:

https://www.hackerrank.com/codesprint4/challenges/leibniz

(The competition has already ended, so I'm just curious about the Python solution, which the organizers refused to reveal, so this is not cheating in any way.)

I only managed to solve the challenge in 77 characters. (Talking only about Python here.) However, one adr2370 managed to solve the challenge in 66 characters!

Please help me find the 66-character solution! I'm very very curious because I had to work really hard to bring mine down to 77, and I'm amazed as to how someone came up with a 66-character solution.

For reference, my solution:

for j in'x'*input():print'%.15f'%sum((-1)**i/(2.*i+1)for i in range(input()))
share|improve this question
Very close to being a duplicate of Calculate pi to 5 decimals (which has a Python solution, FWIW). – Peter Taylor Feb 18 at 23:09
Sorry, I'm curious about a solution for this exact puzzle, not a related puzzle. – Ram Rachum Feb 19 at 0:08
Suggest you read through tips-for-golfing-in-python. You can easily drop a few more chars – gnibbler Feb 19 at 0:30
You should open it up to other languages and see how short people can program it :) – beary605 Feb 19 at 1:03
1  
Actually, it seems the top python solution wasn't really python, it was just a hack. So no one got a 66-character solution. – Ari Feb 19 at 18:07
show 2 more comments

1 Answer

up vote 4 down vote accepted
+50

Python 71 bytes

exec"print'%.15g'%sum((-1.)**i/(i-~i)for i in range(input()));"*input()

Not quite 66 bytes, but with a score of 22.90, it would have ranked second overall.

Edit: regarding Ari's comment about the 66 byte Python solution being a 'hack', I don't think that's exactly what happened. I've noticed several times that the submitted language is displayed incorrectly. For example here: Baconian Cipher. This challenge may only be submitted in Brainf_ck, yet the second ranked solution was submitted in Python 2 (interestingly enough, spot 23 is also Python 2. If I were to speculate, I would say that if the language information is missing from a submission for whatever reason, it seems to default to Python).

I suspect that a similar error may have occurred here, and that the shortest Python solution submitted during the challenge was actually 72 bytes.

Update: After speaking with a moderator, it seems that Ari was right:

enter image description here

share|improve this answer
Delightful hacks here! – Ram Rachum Feb 21 at 0:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.