3
\$\begingroup\$

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.

\$\endgroup\$
18
  • \$\begingroup\$ Can we count TI-BASIC by displayed characters, not tokens? \$\endgroup\$
    – lirtosiast
    Jul 22, 2015 at 17:44
  • 2
    \$\begingroup\$ I 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? \$\endgroup\$ Jul 22, 2015 at 18:17
  • 10
    \$\begingroup\$ If 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. \$\endgroup\$
    – xnor
    Jul 22, 2015 at 19:33
  • 1
    \$\begingroup\$ Could you summarize the rules. eg 10 function calls only, 3 literals only (must be integer 0 to 999), etc \$\endgroup\$
    – MickyT
    Jul 22, 2015 at 21:14
  • 2
    \$\begingroup\$ Found a loophole capable of giving infinite scores. In python you can do import function from module as aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa... \$\endgroup\$
    – Maltysen
    Jul 23, 2015 at 0:00

4 Answers 4

4
\$\begingroup\$

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"
\$\endgroup\$
3
  • \$\begingroup\$ 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). \$\endgroup\$
    – Glen O
    Jul 23, 2015 at 3:39
  • \$\begingroup\$ 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. \$\endgroup\$
    – Glen O
    Jul 23, 2015 at 4:33
  • \$\begingroup\$ @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 \$\endgroup\$
    – MickyT
    Jul 23, 2015 at 18:52
2
\$\begingroup\$

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

\$\endgroup\$
1
  • 3
    \$\begingroup\$ "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. \$\endgroup\$
    – Glen O
    Jul 23, 2015 at 15:13
1
\$\begingroup\$

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;
}
\$\endgroup\$
2
  • \$\begingroup\$ 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. \$\endgroup\$
    – Glen O
    Jul 23, 2015 at 14:55
  • \$\begingroup\$ That's why I said I could unroll the loop in the comment. I'll do so. Thanks for the clarification :) \$\endgroup\$ Jul 23, 2015 at 15:19
0
\$\begingroup\$

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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.