www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Let's Play Code Golf

reply "Charles" <csmith.ku2013 gmail.com> writes:
For the uninitiated, Code Golf is producing the correct answer to 
a question with minimal syntax (whitespace included). I found a 
couple questions on HackerRank, which allows D compilation. So 
far there's only two entries for D (mine and another) for the 
first problem.

Here's the problem:

In Calculus, the Leibniz formula for π is given by:

1 - 1/3 + 1/5 - 1/7 + 1/9 - ... = pi/4

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

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 (p) less than 10^7.

Sample Input

2
10
20

Output Format

Output T lines, with each line containing the summation up to nth 
term.

Sample Output

0.760459904732351
0.772905951666960

Scoring

This is a code golf question. The goal is to write a solution 
with as little code as possible. A correct submission with a 
source code of X characters will receive the following score:

maxScore * (300 - X)/300

Any correct code longer than 300 characters will receive a score 
of maxScore * 0.001.
MaxScore is the maximum score attainable for the problem.
Note that whitespace is also treated as a character.





My solution (150 characters, 15 points):

     void main(){import std.stdio;int t,n;readf(" 
%d",&t);while(t--){readf(" %d",&n);real 
a=0,i=0;for(;i<n;i++)a+=(i%2?-1:1)/(i+i+1);writefln("%.15f",a);}}

Link to problem site: 
https://www.hackerrank.com/challenges/leibniz

Anyone care to do better? :)
Feb 23 2015
next sibling parent Justin Whear <justin economicmodeling.com> writes:
On Mon, 23 Feb 2015 20:21:19 +0000, Charles wrote:
 
 My solution (150 characters, 15 points):
 
      void main(){import std.stdio;int t,n;readf("
 %d",&t);while(t--){readf(" %d",&n);real
 a=0,i=0;for(;i<n;i++)a+=(i%2?-1:1)/(i+i+1);writefln("%.15f",a);}}
 
 Link to problem site: https://www.hackerrank.com/challenges/leibniz
 
 Anyone care to do better? :)
Minor refactorings of your solution to get 15.7: https:// www.hackerrank.com/challenges/leibniz/submissions/code/11073459 Dirty solution (read and decrement a real as if it's a int), but passes the test cases.
Feb 23 2015
prev sibling next sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/23/15 3:21 PM, Charles wrote:
 For the uninitiated, Code Golf is producing the correct answer to a
 question with minimal syntax (whitespace included). I found a couple
 questions on HackerRank, which allows D compilation. So far there's only
 two entries for D (mine and another) for the first problem.

 Here's the problem:

 In Calculus, the Leibniz formula for π is given by:

 1 - 1/3 + 1/5 - 1/7 + 1/9 - ... = pi/4

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

 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
 (p) less than 10^7.

 Sample Input

 2
 10
 20

 Output Format

 Output T lines, with each line containing the summation up to nth term.

 Sample Output

 0.760459904732351
 0.772905951666960

 Scoring

 This is a code golf question. The goal is to write a solution with as
 little code as possible. A correct submission with a source code of X
 characters will receive the following score:

 maxScore * (300 - X)/300

 Any correct code longer than 300 characters will receive a score of
 maxScore * 0.001.
 MaxScore is the maximum score attainable for the problem.
 Note that whitespace is also treated as a character.





 My solution (150 characters, 15 points):

      void main(){import std.stdio;int t,n;readf("
 %d",&t);while(t--){readf(" %d",&n);real
 a=0,i=0;for(;i<n;i++)a+=(i%2?-1:1)/(i+i+1);writefln("%.15f",a);}}

 Link to problem site: https://www.hackerrank.com/challenges/leibniz

 Anyone care to do better? :)
I didn't beat your score, but I did it with ranges (full character count was 174): stdin.readln(); foreach(x; stdin.byLine) writefln("%0.15f", map!(a => (a&1?-1:1)/(2.0*a+1))(iota(x.to!int)).sum); I think if I didn't have to import so many things, I would have done much better :) -Steve
Feb 23 2015
next sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 2/23/15 1:30 PM, Steven Schveighoffer wrote:
 foreach(x; stdin.byLine)
      writefln("%0.15f", map!(a =>
 (a&1?-1:1)/(2.0*a+1))(iota(x.to!int)).sum);
Wonder why didn't you lead with the iota? -- Andrei
Feb 23 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 2/23/15 5:04 PM, Andrei Alexandrescu wrote:
 On 2/23/15 1:30 PM, Steven Schveighoffer wrote:
 foreach(x; stdin.byLine)
      writefln("%0.15f", map!(a =>
 (a&1?-1:1)/(2.0*a+1))(iota(x.to!int)).sum);
Wonder why didn't you lead with the iota? -- Andrei
Cuz that's how it came out my brain ;) -Steve
Feb 23 2015
prev sibling next sibling parent reply "Charles" <csmith.ku2013 gmail.com> writes:
 I didn't beat your score, but I did it with ranges (full 
 character count was 174):

 stdin.readln();
 foreach(x; stdin.byLine)
     writefln("%0.15f", map!(a => 
 (a&1?-1:1)/(2.0*a+1))(iota(x.to!int)).sum);

 I think if I didn't have to import so many things, I would have 
 done much better :)

 -Steve
Yeah, imports were my issue too. Especially with readln, because using that meant I needed std.conv. Why don't reals initialize to zero? That'd save me 4 characters! :P
Feb 23 2015
parent "Chris Williams" <yoreanon-chrisw yahoo.co.jp> writes:
On Monday, 23 February 2015 at 22:58:22 UTC, Charles wrote:
 I didn't beat your score, but I did it with ranges (full 
 character count was 174):

 stdin.readln();
 foreach(x; stdin.byLine)
    writefln("%0.15f", map!(a => 
 (a&1?-1:1)/(2.0*a+1))(iota(x.to!int)).sum);

 I think if I didn't have to import so many things, I would 
 have done much better :)

 -Steve
Yeah, imports were my issue too. Especially with readln, because using that meant I needed std.conv. Why don't reals initialize to zero? That'd save me 4 characters! :P
For real programming purposes, having floating types initialize to an invalid state helps developers to catch logic errors that allow a variable to go into a mathematical operation without having been initialized.
Feb 24 2015
prev sibling parent reply "Steve Sobel" <s.sobellian gmail.com> writes:
On Monday, 23 February 2015 at 21:30:51 UTC, Steven Schveighoffer 
wrote:
 On 2/23/15 3:21 PM, Charles wrote:
 For the uninitiated, Code Golf is producing the correct answer 
 to a
 question with minimal syntax (whitespace included). I found a 
 couple
 questions on HackerRank, which allows D compilation. So far 
 there's only
 two entries for D (mine and another) for the first problem.

 Here's the problem:

 In Calculus, the Leibniz formula for π is given by:

 1 - 1/3 + 1/5 - 1/7 + 1/9 - ... = pi/4

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

 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
 (p) less than 10^7.

 Sample Input

 2
 10
 20

 Output Format

 Output T lines, with each line containing the summation up to 
 nth term.

 Sample Output

 0.760459904732351
 0.772905951666960

 Scoring

 This is a code golf question. The goal is to write a solution 
 with as
 little code as possible. A correct submission with a source 
 code of X
 characters will receive the following score:

 maxScore * (300 - X)/300

 Any correct code longer than 300 characters will receive a 
 score of
 maxScore * 0.001.
 MaxScore is the maximum score attainable for the problem.
 Note that whitespace is also treated as a character.





 My solution (150 characters, 15 points):

     void main(){import std.stdio;int t,n;readf("
 %d",&t);while(t--){readf(" %d",&n);real
 a=0,i=0;for(;i<n;i++)a+=(i%2?-1:1)/(i+i+1);writefln("%.15f",a);}}

 Link to problem site: 
 https://www.hackerrank.com/challenges/leibniz

 Anyone care to do better? :)
I didn't beat your score, but I did it with ranges (full character count was 174): stdin.readln(); foreach(x; stdin.byLine) writefln("%0.15f", map!(a => (a&1?-1:1)/(2.0*a+1))(iota(x.to!int)).sum); I think if I didn't have to import so many things, I would have done much better :) -Steve
It can get down to 155 using ranges, but those imports really are killer. void main(){import std.algorithm,std.conv,std.range,std.stdio;foreach(n;stdin.byLine.drop(1))writefln("%.15f",iota(n.to!int).map!"(-1.0)^^a/(2*a+1)".sum);}
Feb 23 2015
parent "bearophile" <bearophileHUGS lycos.com> writes:
Steve Sobel:

 It can get down to 155 using ranges, but those imports really 
 are killer.
You usually don't want to design a language for code golfing (but several exist, like http://esolangs.org/wiki/GolfScript ).
 void main(){import 
 std.algorithm,std.conv,std.range,std.stdio;foreach(n;stdin.byLine.drop(1))writefln("%.15f",iota(n.to!int).map!"(-1.0)^^a/(2*a+1)".sum);}
You can remove one char: iota(n.to!int). n.to!int.iota. Bye, bearophile
Feb 23 2015
prev sibling next sibling parent reply "anonymous" <anonymous example.com> writes:
On Monday, 23 February 2015 at 20:21:20 UTC, Charles wrote:
 My solution (150 characters, 15 points):

     void main(){import std.stdio;int t,n;readf(" 
 %d",&t);while(t--){readf(" %d",&n);real 
 a=0,i=0;for(;i<n;i++)a+=(i%2?-1:1)/(i+i+1);writefln("%.15f",a);}}

 Link to problem site: 
 https://www.hackerrank.com/challenges/leibniz

 Anyone care to do better? :)
126: void main(){import std.stdio;real n,a;for(readln;a=0,readf(" %f",&n);writefln("%.15f",a))while(--n>=0)a+=(n%2?-1:1)/(n+n+1);}
Feb 23 2015
parent "Charles" <csmith.ku2013 gmail.com> writes:
On Monday, 23 February 2015 at 23:10:32 UTC, anonymous wrote:
 On Monday, 23 February 2015 at 20:21:20 UTC, Charles wrote:
 My solution (150 characters, 15 points):

    void main(){import std.stdio;int t,n;readf(" 
 %d",&t);while(t--){readf(" %d",&n);real 
 a=0,i=0;for(;i<n;i++)a+=(i%2?-1:1)/(i+i+1);writefln("%.15f",a);}}

 Link to problem site: 
 https://www.hackerrank.com/challenges/leibniz

 Anyone care to do better? :)
126: void main(){import std.stdio;real n,a;for(readln;a=0,readf(" %f",&n);writefln("%.15f",a))while(--n>=0)a+=(n%2?-1:1)/(n+n+1);}
Nice going. I didn't realize that the exponent operator was outside of std.math with ^^ which is why I used the ternary operator to achieve the same results, importing the standard library is probably the most expensive thing for this challenge. Yay learning things. With that in mind, and switching around --n to n--, we can get the code down to 120 characters: void main(){import std.stdio;real n,a=0;for(readln;readf(" %f",&n);writefln("%.15f",a))while(n--)a+=(-1)^^n/(n+n+1);} On Tuesday, 24 February 2015 at 00:03:55 UTC, bearophile wrote:
 Steve Sobel:

 It can get down to 155 using ranges, but those imports really 
 are killer.
You usually don't want to design a language for code golfing (but several exist, like http://esolangs.org/wiki/GolfScript ).
Yeah I know that they're languages designed for this, but they feel like cheating for sure. Plus, GolfScript would be kinda odd on DLang's forums ;)
Feb 23 2015
prev sibling parent Artur Skawina via Digitalmars-d <digitalmars-d puremagic.com> writes:
On 02/23/15 21:21, Charles via Digitalmars-d wrote:
 My solution (150 characters, 15 points):
 
     void main(){import std.stdio;int t,n;readf(" %d",&t);while(t--){readf("
%d",&n);real a=0,i=0;for(;i<n;i++)a+=(i%2?-1:1)/(i+i+1);writefln("%.15f",a);}}
void main(){import std.stdio;int t,n;for(readf(" %d",&t);t--;){real a=0;for(readf(" %d",&n);n--;)a+=(-1)^^n/(n+n+1.);writefln("%.15f",a);}} artur
Feb 23 2015