7
1
I'm starting with golf code. But im only do C# so I know wont be a breaking record, but I want give it a try. Leibniz
Challenge is here
The first line contains the number of test cases (N) which is less than 100. Each additional line is a test case for a positive integer value (T) less than 10^7.
I format the code to easy the reading, removing spaces I reduce it to 210 chars. The best code submit for this problem is 54 char, but For C# lenguaje the best is 172 chars.
My guess is should remove reading the N, but couldn't make it stop properly.
using System;
class S
{
static void Main()
{
int n=Convert.ToInt32(Console.ReadLine()),i=0;
for (;i++<n;)
{
double r=0,t=Convert.ToInt32(Console.ReadLine()),j=0;
for(;j<t;)
r+=(1-j%2*2)/(2*j+++1);
Console.WriteLine(r);
}
}
}
NOTE
Mono C# compiler 3.2.8.0 .NET 4.0 CLR
So using C=System.Console
doesn't work.
This isn't an appropriate question for this site. Questions here are challenges for people to compete on (and a few other things like language tips). If you need help, submit your code as an answer to the challenge, and ask for help in the comments of your answer. – Sparr – 2015-04-22T17:24:30.550
2
@Sparr this is an old challenge, you can see this question was already here for Python. http://codegolf.stackexchange.com/questions/10716/solve-the-codesprint4-leibniz-code-golf-challenge-in-python-in-66-characters , and is +2 years old, same challenge I just ask for a diferent lenguaje. Im not trying to get any prize, just trying to learn.
– Juan Carlos Oropeza – 2015-04-22T17:30:58.09710
@Sparr Asking for golfing advice is definitely on topic, and has been officially for almost a year. Also see the help centre.
– Martin Ender – 2015-04-22T17:47:10.3901It's not clear to me whether you want help golfing the code in the question or solving the Hacker Rank challenge. The code in the question doesn't meet the spec for the Hacker Rank challenge. Consider e.g. that the correct output for
100000
is0.785395663397448
. – Peter Taylor – 2015-04-22T18:33:24.273@PeterTaylor My request is for help golfing the code, especially the first FOR and try reading the number of cases. But you are right for that case doesn't work. As I say in the description Im new with this and I considere my program correct because pass the test case. Didnt consider have to try all the cases by myself. Do you think this is because a wrong data type, or is something wrong with my code logic? – Juan Carlos Oropeza – 2015-04-22T18:55:52.167
It's a logic problem. One of the basic rules of applied numerical analysis is that when adding a list you should process the numbers in order of increasing absolute value. – Peter Taylor – 2015-04-22T19:03:16.147
So should I run the calculation in inverse order? – Juan Carlos Oropeza – 2015-04-22T19:11:39.037
@PeterTaylor, I invert the order of calculation and for
100000
now i got0.785400663372449
need to polish a litle bit more. – Juan Carlos Oropeza – 2015-04-22T19:17:16.133