Pi Bowling to 10 in 10

3

Your Goal: Estimate pi to at least 10 decimal places. The format is your choice - it can output as a number, as a string, as an output to STDOUT, etc.

Seems easy, right? But wait, it's not as simple as it seems...

The Rules: You get exactly 10 function calls (this includes operators). These functions cannot be ones that naturally induce or define pi, such as the arc-trig functions or a function converting degrees to radians (this also includes any function that operates as a "solver", such as one that can find roots of sin(x)). If your language has complex number capabilities, this capability cannot be used. All functions must be part of your languages built-in functions or within a standard library (loading the library does not count as an operation, but it must be a standard library, not just a popular one).

All function calls must be crucial to your code - this means that you cannot use division by one, or factorial of two. You also cannot use any literal (except for three digits in the form of one, two, or three integers)*, reading of files of any sort, or a code's ability to convert itself into values (such as a function to get the name of a variable as a string).

*Note: an exception is made for format strings for languages that require them for output. However, only formats, not actual contents, can be found in the format strings, so you can have printf("%f%f",x,y), but not printf("%f.%f",x,y).

Loops may be used, but will eat up your function calls - the 10 function calls are calls, not total functions used. Assignment does not count as an operation, but operations such as array slicing (including obtaining a single value from an array) count, in which case a[1] would count as two bytes (for []).

If you think you've found a loophole for the rules, then it's also against the rules. This is not a challenge for finding the best bending of the rules, or the best way of abusing a minor oversight in the challenge question... if your instinct is to call it a "loophole", then post it as a comment to help close it.

Scoring: This is code bowling, so you're after the highest score. But it's not the total number of bytes in your code that is being counted.

Instead, it's the sum of bytes in the functions being used. That is, if you use 4*cos(2/3) (which happens to be approximately 3.14355), then you have three functions - *, cos, and /, with the total being 5 bytes. If you were to use, for instance (using Julia), sum(map(n->1/n,1:4)), then the operations are sum, map, /, and :, for 8 bytes... but as 1/n is being called four times, it uses 7 function calls (sum, map, four instances of /, and :). User-made functions do not count towards either the function call allowance or the byte count (note: functions used in those user-made functions do count).

Additional emphasis: The point, here, is to find the best way to abuse your language's longest built-in/standard function names in the context of calculating pi. More digits of pi aren't necessary, but I will personally be more likely to upvote an answer that gets more digits than necessary.

Remember, since you're after long function names, you should probably avoid unary and binary operators, as most of these have full function equivalents (in Julia, for instance, * can be replaced by prod).

Quick Summary: You have 10 built-in function calls (includes operators/subroutines/etc). You must estimate pi to 10 decimal places. You can use a total of 3 characters for integer literals (so you can have 1 and 36, or 1 and 2 and 3, or 328, but you can't have 22 and 49). Score is count of built-in function name bytes (if the user defines the name, it doesn't count). No functions that calculate or use pi directly. No complex numbers. This is not a challenge to find loopholes.

Glen O

Posted 2015-07-22T13:39:25.887

Reputation: 2 548

Question was closed 2016-07-15T01:45:19.797

Can we count TI-BASIC by displayed characters, not tokens? – lirtosiast – 2015-07-22T17:44:21.877

2I don't understand the scoring example. If the : counts as a function for the purpose of counting bytes, why does it not count as a function for the purpose of number of invocations? – Peter Taylor – 2015-07-22T18:17:34.337

10If you think you've found a loophole for the rules, then it's also against the rules. This doesn't excuse you from making precise rules. Some of the fun of code golf is doing things in a clever and unexpected ways. We should not have to guess your intent to do a challenge. – xnor – 2015-07-22T19:33:14.060

1Could you summarize the rules. eg 10 function calls only, 3 literals only (must be integer 0 to 999), etc – MickyT – 2015-07-22T21:14:08.757

2Found a loophole capable of giving infinite scores. In python you can do import function from module as aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... – Maltysen – 2015-07-23T00:00:51.107

@PeterTaylor - you're right, that was an oversight. It'll be fixed in a moment. – Glen O – 2015-07-23T03:18:11.723

@xnor - the point was that I had tried to deal with all possible loopholes, but it was possible that I'd missed some. That statement was my way of saying "looking for loopholes that are obviously loopholes and not real opportunities is against the point of this challenge". I may not have closed every loophole, but I was pretty explicit about what the challenge is about. – Glen O – 2015-07-23T03:20:10.980

@Maltysen - I would consider that to be the equivalent of typing aaaaaaaaaaaaaaaaaaaaa = sum in Julia - it's a user-defined function name, and thus not counted. – Glen O – 2015-07-23T03:21:18.637

Since the built-in name would be function, it would count as 8 bytes, no matter how many a characters are used in the user-defined function name. Also, @MickyT - I've added a quick summary, let me know if there's any issue with it. – Glen O – 2015-07-23T03:37:58.440

I was all set for a Monte Carlo approach, until I read the rule about loops. Somehow I don't think I can stick to 10 function calls in several hundred (or thousand) trials... – Tim Pederick – 2015-07-23T09:53:03.110

@TimPederick - yes, that would be quite a challenge. I put the rule about loops in because it's pretty easy to come up with algorithms that use just a few operations within a loop, and with enough iterations, it'll converge. I might try to come up with a code-bowling problem (I'm enjoying the challenge of designing good code-bowling challenges) that could use Monte Carlo or similar approaches soon. – Glen O – 2015-07-23T11:16:17.410

'If you think you've found a loophole for the rules, then it's also against the rules.' You sir, you should be a lawyer. – Zukki – 2015-07-23T14:01:20.137

@Zukki - Can you think of a better way to emphasise it? You wouldn't call it a "loophole" if it was a clearly-acceptable action. – Glen O – 2015-07-23T16:57:17.310

I'm voting to close this as off-topic because, without a clear list of what is and isn't allowed, there is no way to determine if a solution is valid or not. – Mego – 2016-07-14T23:37:51.263

@Mego - can you be more specific? What's something that is unclear in terms of what is and isn't allowed? I'm particularly confused by the "objective primary winning criterion" issue, given that it's quite clear - you count up the bytes used in the built-in names of function calls and equivalent (operators are functions), and the most bytes wins. The only imperfection is the "loophole" line... and that's nothing more than saying "if you see it as a loophole, don't use it". – Glen O – 2016-07-15T06:07:51.270

@GlenO That loophole line is a big part of the problem - what one person may view as a loophole, another may view as a clever solution. It's entirely arbitrary. – Mego – 2016-07-15T06:09:40.993

@Mego - it's not intended as an absolute, but a way to ask people to think about the intent of the rules as well as the explicit wording. There are multiple examples in the "Standard Loopholes" listing on Meta where everyone would recognise it as a loophole irrespective of the problem at hand - for instance, "Optimising for the given test cases". Can you give an example of something that some people might think is a loophole, and others would just call a clever solution? – Glen O – 2016-07-15T06:27:16.290

Note that it specifically says "if your instinct is to call it a "loophole", then post it as a comment to help close it." - in other words, I've tried my best to close all loopholes, but if I've missed one, let me know. – Glen O – 2016-07-15T06:41:50.937

Answers

4

R, 34 42 Points

I think I have complied with all the rules and scored it correctly.

Edit Turns out not complying with the rules in the first place was a bit of a bonus.

Use this formula to calculate pi (63/25)x((17+sqrt(5)*15)/(7+sqrt(5)*15))

# set up the required numbers   
a<-5
b<-2
# Calculate 5^.5 * 15                    3 Operations 11 Bytes
c<-prod(sqrt(a),sum(a,a,a))
# Calculate (17 + c) / (7 + C)           3 Operations 7 Bytes
d<-sum(a,a,a,b,c)/sum(a,b,c)
# Calculate (63/25)*d                    3 Operations 19 Bytes
pi<-prod(as.double(paste0(b,'.',a,b)),d)
# Output pi showing all digits           1 Operations 5 Bytes
strwrap(pi)                    

Test run

> a<-5
> b<-2
> c<-prod(sqrt(a),sum(a,a,a))  # 3 Operations 11 Bytes
> d<-sum(a,a,a,b,c)/sum(a,b,c) # 3 Operations 7 Bytes
> pi<-prod(as.double(paste0(b,'.',a,b)),d) # 3 Operations 19 Bytes
> strwrap(pi)                    # 1 Operations 5 Bytes
[1] "3.14159265380569"

MickyT

Posted 2015-07-22T13:39:25.887

Reputation: 11 735

Unfortunately, you've used 5 digits in integer literals - 5, 2, 2, and 63. Also, it might be a misunderstanding on my part of what R does, but it seems like the number it produces is 2.607955506546497. I'm guessing that strwrap isn't just outputting the number calculated (an explanation and the final output would be helpful). – Glen O – 2015-07-23T03:39:01.320

OK, regarding the "number it produces" part, I just realised I was thinking of the second part of the product for c as a product itself (so a^3 rather than 3a). Now the calculation makes sense, it's just the number of integer literal digits. – Glen O – 2015-07-23T04:33:48.557

@GlenO Sorry about the numbers. The second 2 snuck in when I should have used b and I mistook the rule around the numbers. Will see if I can still make it work or delete – MickyT – 2015-07-23T18:52:23.590

2

C#

Exactly 10 function calls and 3 numeric literals.

using System;
using System.Linq;

namespace ConsoleApplication2
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            /*3:*/
            int a = 3; //0 calls: 0
            /*14->15:*/
            int b = 14; //0 calls: 0 + 0 = 0
            /*14:*/
            int c = (b)++; //1 call: 0 + 1 = 1
            /*926:*/
            int d = 926; //0 calls: 1 + 0 = 1
            /*53:*/
            int e = (a*c) + (a*a); //3 calls: 1+3 = 4
            //3*14 + 3*3 = 42+9 = 51
            e++; //1 call: 4 + 1 = 5
            e++; //1 call: 5 + 1 = 6
            /*5:*/
            char f = e.ToString().ToCharArray().First(); //3 call: 6 + 3 = 9

            Console.WriteLine("{0}.{1}{2}{3}{4}{5}", a, c, b, d, e, f); //1 call: 9 + 1 = 10
        }
    }
}

Outputs: 3.1415926535

Zukki

Posted 2015-07-22T13:39:25.887

Reputation: 121

3"Three numeric literals" isn't the rule. Three digits in numeric literals is the rule (so the total number of digits in your code that are used for numeric literals must be no more than 3). Technically is a string literal, but I think I'll make a special exception for languages that can't output without a format string, as long as it's only the format (which means the decimal place has to go). That being said, props for thinking of a non-numeric way to answer. It might just require some more tweaking to cut back on use of literals. – Glen O – 2015-07-23T15:13:37.137

1

C, 23 bytes

I think this is correct now...

#include <stdio.h>
#include <math.h>

main()
{
    double pi= (double)22 / 7; /* 1 */
    pi+=sin(pi); /* 1 + 3 bytes = 4 */
    pi+=sin(pi); /* 1 + 3 bytes = 4 */
    pi+=sin(pi); /* 1 + 3 bytes = 4 */
    pi+=sin(pi); /* 1 + 3 bytes = 4 */

    printf("%.10f", pi); /* 6 bytes = 6 */
    return 0;
}

Cole Cameron

Posted 2015-07-22T13:39:25.887

Reputation: 1 013

Main is effectively just a user-named function (it just happens to be a special one). That being said, I count twelve operations in your loop - four instances each of ++, <, += (which counts for the + part), and sin (some languages wouldn't use operations for some loops - for instance, Julia has iterators - but C does). You're better off writing it as a sequence of instructions, rather than a loop (true of any language, probably). Writing it as a sequence also gives it more of a "code-bowling" feel, anyway, since you're making the code longer. – Glen O – 2015-07-23T14:55:03.637

That's why I said I could unroll the loop in the comment. I'll do so. Thanks for the clarification :) – Cole Cameron – 2015-07-23T15:19:19.820

0

Racket, 72 bytes

Gotta love those verbose built-in names!

((lambda() ; lambda is a Racket-defined function too, 6 bytes
  (define-values (a b) (integer-sqrt/remainder 491)) ; 13 (define-values) + 22 (integer-sqrt/remainder) = 35 (on this line alone).  a and b are 22 and 7 respectively
  (define-values (q) (/ a b)) ; 13 (define-values) + 1 (/) = 14
  (define-values (c) (+ (sin q) q)) ; 13 + 1 + 3  = 17
  (+ (sin c) c) ; 1 + 3 = 4
  )) ; total: 6 + 35 + 14 + 17 + 4 = 72

With the amount of precision that in-exact numbers in Racket have, this is actually equal to pi such that (- pi (lambda...) outputs 0.0.

Also, I know this is a realllllllllllly old challenge. But all of the current challenge answers do horribly in comparison to this bowl, so I had to do it.

Steven H.

Posted 2015-07-22T13:39:25.887

Reputation: 2 841