7
1
This Perl code is supposed to calculate the Leibniz formula. The original problem is here.
The input format is:
T
<T test cases, each on their own line>
Each test case is a number < 10^7. The output format should be the same, minus the first line.
For example, an input:
2
10
20
And for output:
0.760459904732351
0.77290595166696
My first solutions, at 62 and 61 characters respectively.
for(1..<>){$s=0;map{$s+=($_&1||-1)/(2*$_-1)}1..<>;print"$s\n"}
for(1..<>){$s=0;$s+=($_&1||-1)/(2*$_-1)for 1..<>;print"$s\n"}
Edit: The best solution so far is 53 characters, by @teebee:
<>;print${\map$a-=(-1)**$_/(2*$_-1),1..$_},$a=$/for<>
possible duplicate of Calculate pi to 5 decimals
– Peter Taylor – 2013-02-05T07:49:58.927Why post the same question on two sites? I feel like it's either help or a puzzle, but not both. And as a puzzle it needs a puzzle-type ([tag:code-golf], [tag:coding-challenge]) and objective winning criteria. – luser droog – 2013-02-05T08:06:22.143
2I was recommended that this question belongs here more then stackoverflow so posted here. – Techmonk – 2013-02-05T08:16:15.263