挑战
您必须以最短的长度计算pi。欢迎加入任何语言,您可以使用任何公式来计算pi。它必须能够计算pi到至少5个小数位。最短,将以字符为单位。比赛持续48小时。开始。
注意:这个类似的问题指出,必须使用4 *(1 – 1/3 + 1/5 – 1/7 +…)级数来计算PI。这个问题并没有这个限制,而事实上很多答案在这里(包括最有可能胜出)将是无效的,其他的问题。因此,这不是重复的。
您必须以最短的长度计算pi。欢迎加入任何语言,您可以使用任何公式来计算pi。它必须能够计算pi到至少5个小数位。最短,将以字符为单位。比赛持续48小时。开始。
注意:这个类似的问题指出,必须使用4 *(1 – 1/3 + 1/5 – 1/7 +…)级数来计算PI。这个问题并没有这个限制,而事实上很多答案在这里(包括最有可能胜出)将是无效的,其他的问题。因此,这不是重复的。
Answers:
在交互式外壳中运行
355/113
输出:3.1415929203539825
,精确到小数点后6位
最后,我有一个击败APL的解决方案!
哦,如果您想知道,该比率称为密率(字面上的“精确比率”),由中国数学家祖崇智(公元429-500)提出。可以在此处找到相关的维基百科文章。祖还给出了22/7的比率作为“粗略比率”,他是第一个提出3.1415926 <= pi <= 3.1415927的数学家。
355/113
。投票率最低的答案:3+.14159
。我认为差异不大。
基本的蒙特卡洛模拟。每进行10M次迭代,它将显示当前状态:
for($i=1,$j=$k=0;;$i++){$x=mt_rand(0,1e7)/1e7;$y=mt_rand(0,1e7)/1e7;$j+=$x*$x+$y*$y<=1;$k++;if(!($i%1e7))echo 4*$j/$k."\n";}
感谢cloudfeet和zamnuts的建议!
样本输出:
$ php pi.php
3.1410564
3.1414008
3.1413388
3.1412641
3.14132568
3.1413496666667
3.1414522857143
3.1414817
3.1415271111111
3.14155092
...
3.1415901754386
3.1415890482759
3.1415925423731
$j+=$x*$x+$y*$y<=1;
这样可以节省四个字节。
$k+=1/4;
和print $j/$k
可以降低到$k++;
与print 4*$j/$k
另一个字节。
{:*._1
说明:*.
给出复数的长度和角度。-1的角度是pi。{:
占据列表的末尾[长度,角度]
莱比尼兹(Leibniz)系列仅适用于21个字节的缓慢收敛的系列装饰论者:
+/(4*_1&^%>:@+:)i.1e6
3.14159
atan(0) + pi
。我认为三角函数和pi本身的使用不应算作“计算”。
Arg
(即复数的参数)不是三角函数,尽管其值类似于反正切值
不是我的答案,但这是我所见过的最佳解决方案:
我的理解是,它会将圆上的像素相加,然后除以半径,然后再除一次。那是:
A = πr² # solve for π
π = A/r²
π = (A/r)/r
在我看来,更好的方法是程序以任意大小生成此图像,然后通过Piet解释器运行它。
资料来源:http : //www.dangermouse.net/esoteric/piet/samples.html
0+3.14159
PI-acos(1)
acos(-1)
"3.14"+"159"
从技术上讲,这个答案很臭。
2ׯ1○1
输出3.141592654
。它计算的反正弦是1的两倍。
13个字符的解决方案是:
--/4÷1-2×⍳1e6
这3.141591654
为我输出,符合要求的精度。
它使用简单的+ 4/1 - 4/3 + 4/5 - 4/7 ...
序列进行计算。
¯2○¯1
?”(即acos -1)。但这给出了repl.it(3.1415926425236J¯1.1066193467303274e¯8
)的复杂近似值。知道为什么吗?所有实现都这样做吗?
2 * asin(1)
不过有点作弊。
sqrt(1-theta^2)
在某个点尝试执行A (它以很多触发身份弹出),并在某处失去了一定的精度,最后得到一个略微的负数1-theta^2
。
acos -0.75
。它不可能计算1 - 0.75 ^ 2
为负数。
|^._1
这意味着|log(-1)|
。
ln(e^(42*pi))/42
或pi*113/113
。
stick of butter*(26557.4489*10^-9)/millimeters^3
取一小块黄油,进行高级计算,然后从中制成pi。我想,既然其他所有人都在做简单的数学答案,那么我会添加一个更独特的答案。
stick of butter
是可爱和滑稽,但是这基本上是另一个pi*x/x+y-y
代数的身份。
解决方案:
l=q=d=0;t,s,n,r=3.,3,1,24
while s!=l:l,n,q,d,r=s,n+q,q+8,d+r,r+32;t=(t*n)/d;s+=t
print s
Python shell中的示例输出:
>>> print s
3.14159265359
设法避免任何进口。可以轻松地交换以使用任意精度的Decimal库;只需替换3.
为Decimal('3')
,然后在前后设置精度,然后一元加结果即可转换精度。
与这里的许多答案不同,实际上是计算 π而不是依赖内置常量或数学伪造函数math.acos(-1)
,例如math.radians(180)
,。
fldpi
Whether this loads a constant from ROM or actually calculates the answer depends on the processor though (but on at least some, it actually does a calculation, not just loading the number from ROM). To put things in perspective, it's listed as taking 40 clock cycles on a 387, which is rather more than seems to make sense if it were just loading the value from ROM.
If you really want to ensure a calculation you could do something like:
fld1
fld1
fpatan
fimul f
f dd 4
[for 27 characters]
fldpi
do ?
asin(-1)
nor fldpi
are particularly interesting or creative. There's not much purpose in competing to see whose favorite language has the shortest name for predefined trig functions and pi constants.
for(p=n=2;n<7^7;n+=2)p*=n*n/(n*n-1);p
I don't see any other answers using the Wallis product, so since its named after my namesake (my History of Mathematics lecturer got a big kick out of that), I couldn't resist.
Turns out its a fairly nice algorithm from the golfing perspective, but its rate of convergence is abysmal - approaching 1 million iterations just to get 5 decimal places:
$ time bc -l<<<'for(p=n=2;n<7^7;n+=2)p*=n*n/(n*n-1);p'
3.14159074622629555058
real 0m3.145s
user 0m1.548s
sys 0m0.000s
$
Alternatively, we can use Newton-Raphson to solve sin(x)=0
, with a starting approximation of 3. Because this converges in so few iterations, we simply hard-code 2 iterations, which gives 10 decimal places:
x=3+s(3);x+s(x)
The iterative formula according to Newton-Raphson is:
x[n+1] = x[n] - ( sin(x[n]) / sin'(x[n]) )
sin'
=== cos
and cos(pi)
=== -1
, so we simply approximate the cos
term to get:
x[n+1] = x[n] + sin(x[n])
Output:
$ bc -l<<<'x=3+s(3);x+s(x)'
3.14159265357219555873
$
pi is actually being calculated without trig functions or constants.
a=4
for i in range(9**6):a-=(-1)**i*4/(2*i+3)
result:
>>> a
3.1415907719167966
Directly computes area / r^2 of a circle.
double p(n,x,y,r){r=10000;for(n=x=0;x<r;++x)for(y=1;y<r;++y)n+=x*x+y*y<=r*r;return(double)n*4/r/r;}
This function will calculate pi by counting the number of pixels in a circle of radius r
then dividing by r*r
(actually it just calculates one quadrant). With r
as 10000, it is accurate to 5 decimal places (3.1415904800). The parameters to the function are ignored, I just declared them there to save space.
x=0;for(i=1;i<1e6;i++){x+=1/i/i};Math.sqrt(6*x)
x
becomes zeta(2)=pi^2/6
so sqrt(6*x)=pi
. (47 characters)
After using the distributive property and deleting the curly brackets from the for
loop you get:
x=0;for(i=1;i<1e6;i++)x+=6/i/i;Math.sqrt(x)
(43 characters)
It returns:
3.14159169865946
Edit:
I found an even shorter way using the Wallis product:
x=i=2;for(;i<1e6;i+=2)x*=i*i/(i*i-1)
(36 characters)
It returns:
3.141591082792245
(6*sum(n**-2for n in range(1,9**9)))**0.5
Or spare two characters, but use scipy
import scipy.special as s
(6*s.zeta(2,1))**0.5
Edit: Saved 16 (!) characters thanks to amcgregor
math
import and sqrt
call by pivoting to exponentiation instead: (6*sum(n**-2 for n in range(1,9**9)))**0.5
Using the formula given by Simon Plouffe in 1996, this works with 6 digits of precision after the decimal point:
function f(k){return k<2?1:f(k-1)*k}for(y=-3,n=1;n<91;n++)y+=n*(2<<(n-1))*f(n)*f(n)/f(2*n);alert(y)
This longer variant (130 characters) has a better precision, 15 digits after the decimal point:
function e(x){return x<1?1:2*e(x-1)}function f(k){return k<2?1:f(k-1)*k}for(y=-3,n=1;n<91;n++)y+=n*e(n)*f(n)*f(n)/f(2*n);alert(y)
I made this based in my two answers to this question.
p (0..9**6).map{|e|(-1.0)**e/(2*e+1)*4}.reduce :+
Online Version for testing.
Another version without creating an array (50 chars):
x=0;(0..9**6).each{|e|x+=(-1.0)**e/(2*e+1)*4}; p x
Online Version for testing.
a=__import__;reduce(a('operator').__add__,a('itertools').imap(lambda e:(-1.0)**e/(2*e+1)*4,xrange(9**6)))
-- note the use of xrange/imap; in Python 3 you can avoid this; basically I don't want all of your RAM to get consumed constructing a list with so many entries.
$\=$\/(2*$_-1)*$_+2for-46..-1;print
Produces full floating point precision. A derivation of the formula used can be seen elsewhere.
Sample usage:
$ perl pi.pl
3.14159265358979
Arbitrary Precision Version
use bignum a,99;$\=$\/(2*$_-1)*$_+2for-329..-1;print
Extend as needed. The length of the iteration (e.g. -329..-1
) should be adjusted to be approximately log2(10) ≈ 3.322 times the number of digits.
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211707
Or, using bigint
instead:
use bigint;$\=$\/(2*$_-1)*$_+2e99for-329..-1;print
This runs noticably faster, but doesn't include a decimal point.
3141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
C# 192
class P{static void Main(){var s=(new System.Net.WebClient()).DownloadString("http://www.ctan.org/pkg/tex");System.Console.WriteLine(s.Substring(s.IndexOf("Ver­sion")+21).Split(' ')[0]);}}
Outputs:
3.14159265
No math involved. Just looks up the current version of TeX and does some primitive parsing of the resulting html. Eventually it will become π according to Wikipedia.
Assumes all uninitialized variables as 0. This is default in some versions of Game Maker.
for(i=1;i<1e8;i++)x+=6/i/i;sqrt(x)
Result:
3.14159169865946
1e8
instead of 999999
for(i=1;i<1e8;)x+=6/i/i++;sqrt(x)
to save a byte (or alternatively for(i=1;i++<1e8;)
)?
Java - 83 55
Shorter version thanks to Navin.
class P{static{System.out.print(Math.toRadians(180));}}
Old version:
class P{public static void main(String[]a){System.out.print(Math.toRadians(180));}}
180
by pi/180
.
sqrt(8*sum(1/seq(1,1000001,2)^2))
[1] 3.141592
Hopefully this follows the rules.
p 1.570796*2
I am technically "calculating" pi an approximation of pi.
acos(-1)
.