Answers:
2\!:^2^..292^15.2/3]{(.)2/.9>+{\+.((}*;.}do;;]-1%{^0@{2$*+\}/"/"\n}/;
(假设您没有在stdin上传递任何内容)
我不想再听到那些没有pi内置常量的人的抱怨。我什至没有浮点数!
有关背景,请参见http://en.wikipedia.org/wiki/Continued_fraction#Best_rational_approximations。
# No input, so the stack contains ""
2\!:^2^..292^15.2/3]
# ^ is used to store 1 because that saves a char by allowing the elimination of whitespace
# Otherwise straightforward: stack now contains [2 1 2 1 1 1 292 1 15 7 3]
# Pi as a continued fraction is 3+1/(7+1/(15+1/(...)))
# If you reverse the array now on the stack you get the first 10 continuants followed by 2
# (rather than 3)
# That's a little hack to avoid passing the denominator 1000000
{
# Stack holds: ... [c_n c_{n-1} ... c_0]
(.)2/.9>+
# Stack holds ... [c_{n-1} ... c_0] c_n (1+c_n)/2+((1+c_n)/2 > 9 ? 1 : 0)
# (1+c_n)/2 > 9 is an ad-hoc approximation of the "half rule"
# which works in this case but not in general
# Let k = (1+c_n)/2+((1+c_n)/2 > 9 ? 1 : 0)
# We execute the next block k times
{
# ... [c_{n-1} ... c_0] z
\+.((
# ... [z c_{n-1} ... c_0] [c_{n-1} ... c_0] z-1
}*
# So we now have ... [c_n c_{n-1} ... c_0] [(c_n)-1 c_{n-1} ... c_0] ...
# [(c_n)-k+1 c_{n-1} ... c_0] [c_{n-1} ... c_0] c_n-k
;
# Go round the loop until the array runs out
.
}do
# Stack now contains all the solutions as CFs in reverse order, plus two surplus:
# [2 1 2 1 1 1 292 1 15 7 3] [1 2 1 1 1 292 1 15 7 3] ... [6 3] [5 3] [4 3] [3] [2] []
# Ditch the two surplus ones, bundle everything up in an array, and reverse it
;;]-1%
# For each CF...
{
# Stack holds ... [(c_n)-j c_{n-1} ... c_0]
# We now need to convert the CF into a rational in canonical form
# We unwind from the inside out starting with (c_n)-j + 1/infinity,
# representing infinity as 1/0
^0@
# ... 1 0 [c_n-j c_{n-1} ... c_0]
# Loop over the terms of the CF
{
# ... numerator denominator term-of-CF
2$*+\
# ... (term-of-CF * numerator + denominator) numerator
}/
# Presentation
"/"\n
# ... numerator "/" denominator newline
}/
# Pop that final newline to avoid a trailing blank line which isn't in the spec
;
2\!:^2^..292^15.2/3]
已经让我震惊。
这不会很快,但是我认为从技术上来说是正确的。
Round[π,1/Range@1*^6]//.x_:>First/@Split[x,#2≥#&@@Abs[π-{##}]&]
Round[π, x]
给出最接近π的分数(步长为)x
。这是“可列出的”,因此Round[π,1/Range@1*^6]
对所有分数1/10^6
按顺序排列都是如此。然后,//.
通过删除距离π比前一个元素更远的任何元素来重复()处理具有许多“不良”有理近似值的结果列表。
Round[Pi, x]
给出最接近的分数。这是“可列出的”,因此对于所有小到1/10 ^ 6的分数都是如此。然后,通过删除比pi更远离pi的任何元素来重复()处理具有许多“不良”有理近似值的结果列表。Pi
x
Round[Pi,1/Range@1*^6]
//.
Select[Round[f=Pi,1/Range@1*^6],If[#<f,f=#;True]&@Abs[#-Pi]&]
... ...但由于主要偏见而无用
$e=$p=atan2 0,-1;($f=abs$p-($==$p*$_+.5)/$_)<$e&&($e=$f,say"$=/$_")for 1..1e6
一个小挑战是Perl没有内置的π常数可用,因此我首先必须将其计算为atan2(0,-1)
。我敢肯定,这将被更适合该工作的语言击败,但是对于主要用于文本处理的语言来说,这还不错。
999999
为1e6
并保存3个字符。
String found where operator expected at prog.pl line 1, near "say"$=/$_""
-M5.01
用于say
命令的开关(以及Perl 5.10.0或更高版本)。对不起,我没有提及。
a=b=d=1.
while b<=1e6:
e=3.14159265359-a/b;x=abs(e)
if x<d:print a,b;d=x
a+=e>0;b+=e<0
p=3.14159265359;d=1
for a in range(3,p*1e6):
b=round(a/p);e=abs(p-a/b)
if e<d:print a,b;d=e
注意:写的字符p=3.14159265359;
少于from math import*
。该死的罗imports进口!
1.0
-> 1.
,10**6
->1e6
import math._
def p(z:Int,n:Int,s:Double):Unit=
if(n==1e6)0 else{val q=1.0*z/n
val x=if(abs(Pi-q)<s){println(z+"/"+n)
abs(Pi-q)}else s
if(Pi-q<0)p(z,n+1,x)else p(z+1,n,x)}
p(3,1,1)
//取消高尔夫:457
val pi=math.Pi
@annotation.tailrec
def toPi (zaehler: Int = 3, nenner: Int = 1, sofar: Double=1): Unit = {
if (nenner == 1000000) ()
else {
val quotient = 1.0*zaehler/nenner
val diff = (pi - quotient)
val adiff= math.abs (diff)
val next = if (adiff < sofar) {
println (zaehler + "/" + nenner)
adiff
}
else sofar
if (diff < 0) toPi (zaehler, nenner + 1, next)
else toPi (zaehler + 1, nenner, next)
}
}
tailrec批注只是一种检查,以验证它是否为尾递归,这通常是性能上的改进。
pi.scala:1 error: not found: value math
math
与Math
可能就足够了。我在此元线程上提到过simplescala,如果您碰巧要再次搜索它:meta.codegolf.stackexchange.com/a/401/373
我选择使用连续分数表示π中的项数作为“最佳”的度量。根据这个标准,π的最佳有理逼近是它的收敛。
有10个π的分母小于一百万的收敛子。这少于所要求的167个字词,但我将其包含在此处,因为其他人可能会感兴趣。
Convergents[π, 10]
(* out *)
{3, 22/7, 333/106, 355/113, 103993/33102, 104348/33215, 208341/66317,
312689/99532, 833719/265381, 1146408/364913}
如果您真的想查看第一个收敛的分母,那么它将花费额外的11个字符:
Convergents[π, 10] /. {3 -> "3/1"}
(* out *)
{"3/1", 22/7, 333/106, 355/113, 103993/33102, 104348/33215,
208341/66317, 312689/99532, 833719/265381, 1146408/364913}
对于有兴趣的人,下面显示了π的收敛,偏商和连续分数表达之间的关系:
Table[ContinuedFraction[π, k], {k, 10}]
w[frac_] := Row[{Fold[(#1^-1 + #2) &, Last[#], Rest[Reverse[#]]] &[Text@Style[#, Blue, Bold, 14] & /@ ToString /@ ContinuedFraction[frac]]}];
w /@ FromContinuedFraction /@ ContinuedFraction /@ Convergents[π, 10]
请原谅连续分数的格式不一致。
double n=3,d=1,e=d;while(n<4e5){double w=n/d-Math.PI,a=Math.Abs(w);if(a<e){e=a;Console.WriteLine(n+"/"+d);}if(w>0)d++;else n++;}
未压缩的代码
var numerator = 3d;
var denominator = 1d;
var delta = 4d;
while (numerator < 4e5)
{
var newDelta = (numerator / denominator) - Math.PI;
var absNewDelta = Math.Abs(newDelta);
if (absNewDelta < delta)
{
delta = absNewDelta;
Console.WriteLine(string.Format("{0}/{1}", numerator, denominator));
}
if (newDelta > 0)
{
denominator++;
}
else
{
numerator++;
}
}
var
并不总是你的朋友。通过删除它有利于double
您获得合并声明的能力,无需使用双字面量,并且可以节省16个字符。OTOH问题要求一个程序,因此添加类声明和Main
方法将使您不知所措。
"#{Math.PI}"
。