This is the same question as Solve the CodeSprint4 Leibniz code golf challenge in Python in 66 characters. The variation is that I'm a C programmer. My code for the above question is:

#include <math.h>
int i,t,p;  
double s=1;  

Now I just need to know the places where I could reduce my code length.

  • \$\begingroup\$ You missed #include<stdio.h> \$\endgroup\$
    – Spikatrix
    Mar 1, 2015 at 10:21
  • \$\begingroup\$ Actually, you don't need neither of the two #include @CoolGuy. Most C compilers will complain but compile nevertheless. \$\endgroup\$ Mar 2, 2015 at 21:03
  • \$\begingroup\$ Please include a specification of the requirements in the body of the question. When the link's dead, it's impossible to know what inputs are expected and what output to produce, other than by grokking the code. \$\endgroup\$ Jul 20, 2018 at 8:55
  • \$\begingroup\$ I'm guessing that the objective is to compute the Leibniz formula for π (1 - 1/3 + 1/5 - 1/7 +...) to p terms, t times - is that right? \$\endgroup\$ Jul 20, 2018 at 9:07

2 Answers 2

  1. Do everything in one line.
  2. Don't read number of test cases, scanf will return -1 on EOF
  3. Don't include anything, pow(-1,i) can be replaced by 1-i%2*2
  4. Do the sum loop in reverse to save a variable.

Here is my 102 bytes code

main(n){gets(&n);for(double s;s=scanf("%d",&n)>0;printf("%.15f\n",s))while(--n)s+=(1-n%2*2)/(1.+2*n);}
  • \$\begingroup\$ (1-n%2*2) - It seems that signs depend on n's parity, but they shouldn't. Or not? \$\endgroup\$
    – Qwertiy
    Feb 15, 2015 at 10:28
  • \$\begingroup\$ @Qwertiy n here is decreased every loop \$\endgroup\$
    – JayXon
    Feb 15, 2015 at 10:31
  • \$\begingroup\$ Yes, you are right. By some reason I thought about sign using n and formula using i, but there is n in both parts. \$\endgroup\$
    – Qwertiy
    Feb 15, 2015 at 10:34

Remove include and replace pow by check.
Use the fact that scanf returns number of read and saved fields.
Replace while via for.
Remove figure brackets.
Use %f in printf for double - only scanf needs %lf.

I didn't check if following code works fine, but even if smth is wrong, the correct result have to be somewhere near it:

int i,t,p;
double s;

By the way, what about making all variables double instead of int?

  • \$\begingroup\$ Can't i write a code in the comments here? \$\endgroup\$
    – Gaurav
    Feb 14, 2015 at 23:23
  • \$\begingroup\$ Drop the i variable, you just need p and s in the inner loop: for(s=scanf("%d",&p);--p;s+=(p&1?-1:1)/(1+2.*p)); \$\endgroup\$
    – edc65
    Feb 15, 2015 at 0:02
  • \$\begingroup\$ (i&1?-1:1) --> (1-i%2*2) \$\endgroup\$ Feb 15, 2015 at 1:48
  • \$\begingroup\$ From memory, I think you can do: main(i,t,p){ to declare them as ints. \$\endgroup\$
    – grc
    Feb 15, 2015 at 3:23

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.