4
\$\begingroup\$

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.)

Modified from that page:

In Calculus, the Leibniz formula for \$\pi\$ is given by:

$$\sum_{n=0}^\infty \dfrac{\left(-1\right)^n}{2n+1}$$

You will be given an integer \$n\$. Your task is to print the summation of the Leibniz formula up to the \$n\$th term of the series correct to 15 decimal places.

That is given \$n\$ calculate:

$$\sum_{k=0}^n \dfrac{\left(-1\right)^k}{2k+1}$$

with the first 15 decimal places correct.

Input Format

The first line contains the number of test cases (\$T\$) which is less than 100. Each additional line is a test case for a positive integer value less than 10^7.

Output Format

Output \$T\$ lines each containing the calculated result for the corresponding line of the input.

Scoring

This is .

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()))
\$\endgroup\$
9
  • \$\begingroup\$ Very close to being a duplicate of Calculate pi to 5 decimals (which has a Python solution, FWIW). \$\endgroup\$ Commented Feb 18, 2013 at 23:09
  • \$\begingroup\$ Sorry, I'm curious about a solution for this exact puzzle, not a related puzzle. \$\endgroup\$
    – Ram Rachum
    Commented Feb 19, 2013 at 0:08
  • \$\begingroup\$ Suggest you read through tips-for-golfing-in-python. You can easily drop a few more chars \$\endgroup\$
    – gnibbler
    Commented Feb 19, 2013 at 0:30
  • \$\begingroup\$ You should open it up to other languages and see how short people can program it :) \$\endgroup\$
    – beary605
    Commented Feb 19, 2013 at 1:03
  • 1
    \$\begingroup\$ Actually, it seems the top python solution wasn't really python, it was just a hack. So no one got a 66-character solution. \$\endgroup\$
    – Ari
    Commented Feb 19, 2013 at 18:07

3 Answers 3

6
\$\begingroup\$

Python, 65 characters

exec'print`sum((-1.)**x/(x-~x)for x in range(input()))`;'*input()
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Interesting. The result of input 10, for example, is not correctly rounded, but is still accepted as correct. Nice find. \$\endgroup\$
    – primo
    Commented Sep 30, 2013 at 18:34
5
+50
\$\begingroup\$

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

\$\endgroup\$
1
  • \$\begingroup\$ Delightful hacks here! \$\endgroup\$
    – Ram Rachum
    Commented Feb 21, 2013 at 0:28
1
\$\begingroup\$

Python 2 (50 chars, with input loop grows)

a=n=1.;s=0;exec's+=a/n;a=-a;n+=2;'*input();print s

also prints only 12 digits of result, with loop and formatting

exec input()*"a=n=1.;s=0;exec's+=a/n;a=-a;n+=2;'*input();print '%.15f'%s;"

74 chars

Python 3 (57 chars, with input loop grows to 79 chars)

without loop

a=n=1;s=0;exec('s+=a/n;a=-a;n+=2;'*int(input()));print(s)

with loop

exec(int(input())*"a=n=1;s=0;exec('s+=a/n;a=-a;n+=2;'*int(input()));print(s);")

Python 3 prints 16 digits by default

exec(int(input())*"print(sum((-1)**i/(i-~i)for i in range(int(input()))));")

76 chars

\$\endgroup\$
1
  • \$\begingroup\$ A few problems: these don't iterate through a list of inputs (but rather only process a single input), and don't output the answer rounded to 15 decimal places (by default, Python only prints 12 digits of a float). Under these conditions, 49 bytes is possible, simply by removing the formatting and iteration from the accepted answer print sum((-1.)**i/(i-~i)for i in range(input())) \$\endgroup\$
    – primo
    Commented Oct 1, 2013 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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