计算


16

挑战

给定一个整数s作为输入,其中输出的值(其中表示黎曼Zeta函数)。s1个ζsζX

更多信息

ζs被定义为:

ζs=ñ=1个1个ñs

您应该将答案输出到5个小数位(不多也不少)。如果答案是无穷大,则应输出或您所用语言的同等语言。

允许使用Riemann Zeta内置插件,但是这样做不太有趣;)

例子

输出必须完全如下所示

Input -> Output
1 -> ∞ or inf etc.
2 -> 1.64493
3 -> 1.20206
4 -> 1.08232
8 -> 1.00408
19 -> 1.00000

赏金

作为允许内置的安慰,我将为使用内置zeta函数的最短答案提供100 rep赏金。(绿色复选标记仍将是最短的解决方案)

获奖

以字节为单位的最短代码获胜。


7
这项挑战具有如此巨大的潜力...直到您允许内置功能为止
HyperNeutrino

@HyperNeutrino是的,我发布了,因为我看到了允许内置的挑战。FGITW
NoOneIsHere

2
“精确到小数点后五位”是否严格?(即,我们可以输出更高的精度吗?)如果不是,则测试用例应该真正显示6dp。
乔纳森·艾伦

@JonathanAllen我已经清理了四舍五入的规范
Beta Decay

3
@BetaDecay(叹息不ping)输入是否应该19 真正输出文本1.00000?无效1还是1.0有效?看来您已经将它变成了变色龙挑战。
乔纳森·艾伦

Answers:


11

Mathematica,9 7 11个字节

Zeta@#~N~6&

说明:

Zeta@#       (* Zeta performed on input *)
      ~N     (* Piped into the N function *)
        ~6   (* With 6 digits (5 decimals) *)
          &  (* Make into function *)

Mathematica result

没有内置:

Mathematica,23个UTF-8字节

Sum[1/n^#,{n,∞}]~N~6&

多亏了凯利·洛德(Kelly Lowder)


3
N@*Zeta保存两个字节。
马丁·恩德

@*是(左)复合算:f@*g表示在该参数,其值的函数xf[g[x]]
格雷格·马丁

@BetaDecay对于1它输出ComplexInfinity,并四舍五入到位5。(例如1.64493
NoOneIsHere17年

@MartinEnder如何*工作?
NoOneIsHere

1
@NoOneIsHere您的答案使用,N~5但您的解释使用6
numbermaniac

8

Javascript,81 70 66 65字节

s=>s-1?new Int8Array(1e6).reduce((a,b,i)=>a+i**-s).toFixed(5):1/0

可运行的示例:

ζ=s=>s-1?new Int8Array(1e6).reduce((a,b,i)=>a+i**-s).toFixed(5):1/0

const values = [ 1, 2, 3, 4, 8, 19 ];
document.write('<pre>');
for(let s of values) {
  document.write('ζ(' + s + ') = ' + ζ(s) + '\n')
}


为什么叫Z?zeta符号在JS中是有效的函数名称,您不需要打高尔夫球。
基金莫妮卡的诉讼

更换Array(1e6).fill()[...Array(1e6)],并更换第一(s)s
康纳尔奥布莱恩

1
@QPaysTaxes好点!Unicode变量名称ftw!
Frxstrem '17

@ ConorO'Brien Huh,我从没意识到数组技巧(我认为稀疏数组不会迭代,但是我想我错了)。谢谢!
Frxstrem '17

@Frxstrem注意ζ需要两个字节
CocoaBean

6

APL(Dyalog)22 21字节

看,没有内置功能!-1感谢ngn。

由于Dyalog APL没有局限性,因此我使用了Iverson提出的表示法

{1=⍵:'¯'5⍕+/÷⍵*⍨⍳!9}

在线尝试!

{ 匿名功能:

1=⍵: 如果参数为1,则:

  '¯' 返回马克龙

 其他

  !9 阶乘九(362880)

   第一,许多整数 ntegers

  ⍵*⍨ 将他们提升为论点的力量

  ÷ 倒数

  +/ 和

  5⍕ 五位小数的格式

} [匿名功能结束]


1
1E6-> !9
ngn

@ngn谢谢。
亚当

5

C,74 70 69字节

n;f(s){double z=n=0;for(;++n>0;)z+=pow(n,-s);printf("%.5f",z/=s!=1);}

用编译-fwrapv。产生输出将需要一些时间。

看到它在这里工作。该零件++n>0已替换为++n<999999,因此您无需等待。这样可以保持相同的功能和输出。


float工作吗?
l4m2

5

TI-Basic,16字节(无内置)

Fix 5:Σ(X^~Ans,X,1,99

您确实需要提高到大约150000,才能正确回答Ans = 2,而使用84 Plus CE可能要花费半小时以上的时间。同样,您可以在某处乘以(Ans-1)^ 0来获得Ans = 1(TI-Basic最接近的无穷大表示法)的错误!
pizzapant184 '17

@ pizzapants184我完全知道2、3等可能需要99次以上的迭代。您可以通过替换实现这一功能99E9其中E是科学E,即占10 ^ 9。(或者显然是较小的东西,如E5)。如果总和的上限为,则理解E99通常用于正无穷大,理论上也可以实现此功能E99。仿真器可以提供比物理计算器更快的速度。感谢您的想法:)
Timtech '17

我不认为这显示无穷大。由于浮点数的不精确性,如果无限加1,它甚至不会抛出错误。
lirtosiast



2

MATL,21字节

q?'%.5f'2e5:G_^sYD}YY

在线尝试!

说明

输入1是特殊情况下的输出inf,这是MATL显示无穷大的方式。

对于除以外的输入1,将第一2e5项相加就可以达到5个小数位的精度。原因是,从直接计算来看,该项数足以满足输入要求2,对于较大的指数,级数的尾部较小。

q         % Input (implicit) minus 1
?         % If non-zero
  '%.5f'  %   Push string: format specifier
  2e5:    %   Push [1 2 ... 2e5]
  G       %   Push input again
  _       %   Negate
  ^       %   Power. element-wise
  s       %   Sum of array
  YD      %   Format string with sprintf
}         % Else
YY        %   Push infinity
          % End (implicit)
          % Display (implicit)

2

R,54个字节

function(a){round(ifelse(a==1,Inf,sum((1:9^6)^-a)),5)}

直接求和并按要求设置格式,Inf如果a为1 ,则输出。总和9^6似乎足以获得五位精度,同时仍然可以测试;9^9在相同长度的代码中将获得更好的准确性。如果R具有适当的三元运算符,我可以使它更短。


1
function(a)round("if"(a-1,sum((1:9^6)^-a)),5)短几个字节。
朱塞佩

是的,但是如果a = 1会引发错误 function(a)round("if"(a-1,sum((1:9^6)^-a),Inf),5),并且仍然比我原来的解决方案短。
迈克尔·卢戈

哦,是的,当然!我忘了包括Inf,这就是我在注释框中直接输入代码所得到的...
Giuseppe

2

C,129130128字节

#include<math.h>
f(s,n){double r=0;for(n=1;n<999;++n)r+=(n&1?1:-1)*pow(n,-s);s-1?printf("%.5f\n",r/(1-pow(2,1-s))):puts("oo");}

它使用以下公式

\ zeta(s)= \ frac {1} {1-2 ^ {1-s}} \ sum \ limits_ {n = 1} ^ {\ infty} {\ frac {(-1)^ {n + 1} } {n ^ s}}

测试与结果

main(){f(2,0);f(1,0);f(3,0);f(4,0);f(8,0);f(19,0);}

1.64493
+oo
1.20206
1.08232
1.00408
1.00000

为什么用这个方程代替Σ(1/(n^s))?似乎要复杂得多...
Beta Decay's

@BetaDecay,因为在我看来查找结果的速度更快;这是在1..999中求和s的范围,在'Σ(1 /(n ^ s))'中,需要s在1..10 ^ 6的范围内
RosLuP

1
我知道了。仅供参考,oo很好,您无需将其指定为正值
Beta Decay's


@ceilingcat您可以为这个问题写另一个条目...似乎我记得这里没有math.h标头,它没有链接...
RosLuP

2

Python 3:67字节(无内置)

f=lambda a:"∞"if a<2else"%.5f"%sum([m**-a for m in range(1,10**6)])

没什么特别的,因为隐式utf-8编码,只使用python 3

在线测试用例。




1

果冻,23 字节

ȷ6Rİ*⁸S÷Ị¬$ær5;ḷỊ?”0ẋ4¤

在线尝试!

怎么样?

  • 总结前一百万个术语
  • 除以0何时abs(input)<=1产生inf(而不是14.3927267228649891
  • 四舍五入到小数点后五位
  • 如果abs(result)<=11.0as 格式化为四个零1.00000
  • 打印结果

ȷ6Rİ*⁸S÷Ị¬$ær5;ḷỊ?”0ẋ4¤ - Main link: s
ȷ6                      - literal one million
  R                     - range: [1,2,...,1000000]
   İ                    - inverse (vectorises)
     ⁸                  - link's left argument, s
    *                   - exponentiate
      S                 - sum
          $             - last two links as a monad:
        Ị               -   insignificant? (absolute value of s less than or equal to 1?)
         ¬              -   not (0 when s=1, 1 when s>1)
       ÷                - divide (yielding inf when s=1, no effect when s>1)
           ær5          - round to 10^-5
                      ¤ - nilad followed by link(s) as a nilad:
                  ”0    -   literal '0'
                    ẋ4  -   repeated four times
                Ị?      - if insignificant (absolute value less than or equal to 1?)
              ;         -       concatenate the "0000" (which displays as "1.00000")
               ḷ        - else: left argument
                        - implicit print


0

果冻,26个字节

⁵*5İH+µŒṘḣ7
⁴!Rİ*³Sǵ’ݵ’?

不要通过此链接在线尝试!(由于这使用了16!〜20万亿个术语,因此在TIO上运行会产生MemoryError)

请通过此链接在线尝试。(改为使用一百万个术语。更易于管理,但又需要占用一个字节)

返回inf输入1。

说明

⁵*5İH+µŒṘḣ7    - format the output number
⁵*5İH+         - add 0.000005
      µŒṘ      - get a string representation
         ḣ7    - trim after the fifth decimal.

⁴!Rİ*³Sǵ’ݵ’? - main link, input s
           µ’? - if input minus 1 is not 0...
⁴!R            -   [1,2,3,...,16!] provides enough terms.
   İ           -   take the inverse of each term
    *³         -   raise each term to the power of s
      S        -   sum all terms
       Ç       -   format with the above link
               - else:
        µ’İ    -   return the reciprocal of the input minus 1 (evaluates to inf)

在26个字节中,7个用于计算,12个用于格式化,7个用于inf在零上生成。为此必须有更好的高尔夫。


ȷ6 是一百万个数字文字,除去了阶乘变通方法。
乔纳森·艾伦

0

MathGolf,14个字节(没有内置函数

┴¿Å'∞{◄╒▬∩Σ░7<

请注意,在TIO链接中,我替换106 代替 107。这是因为此处提交的版本对于所有测试用例都超时。这导致3和8的答案相差1个小数位。但是,MathGolf中有更大的1字节数字文字,允许任意十进制精度。

在线尝试!

说明

┴                check if equal to 1
 ¿               if/else (uses one of the next two characters/blocks in the code)
  Å              start block of length 2
   '∞            push single character "∞"
     {           start block or arbitrary length
      ◄          push 10000000
       ╒         range(1,n+1)
        ▬        pop a, b : push(b**a)
         ∩       pop a : push 1/a (implicit map)
          Σ      sum(list), digit sum(int)
           ░     convert to string (implicit map)
            7    push 7
             <   pop(a, b), push(a<b), slicing for lists/strings

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.