2
\$\begingroup\$

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>
main(){  
int i,t,p;  
scanf("%d",&t);  
while(t--)  
{  
double s=1;  
scanf("%d",&p);  
for(i=1;i<p;++i)  
s+=pow(-1,i)/(2*i+1);  
printf("%.12lf\n",s);  
}  
} 

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

\$\endgroup\$
4
  • \$\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

4
\$\begingroup\$
  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);}
\$\endgroup\$
3
  • \$\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
1
\$\begingroup\$

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:

main(){  
int i,t,p;
double s;
for(scanf("%d",&t);t--;printf("%.12f\n",s))
for(s=i=scanf("%d",&p);i<p;++i)  
s+=(i&1?-1:1)/(2.*i+1);  
}

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

\$\endgroup\$
4
  • \$\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.