คำนวณค่าของ


16

ท้าทาย

รับจำนวนเต็ม, , เป็นอินพุตโดยที่เอาท์พุทค่าของ (โดยที่หมายถึงRiemann Zeta Function )ss1ζ(s)ζ(x)

ข้อมูลเพิ่มเติม

ζ(s)ถูกกำหนดเป็น:

ζ(s)=n=11ns

คุณควรส่งคำตอบไปที่ทศนิยม 5 ตำแหน่ง (ไม่มากไม่น้อย) ถ้าคำตอบออกมาเป็นอินฟินิตี้คุณควรเอาท์พุทหรือเทียบเท่าในภาษาของคุณ

บิวด์อิน Riemann Zeta ได้รับอนุญาต แต่ก็สนุกน้อยกว่าที่จะทำเช่นนั้น;)

ตัวอย่าง

ผลลัพธ์จะต้องตรงตามที่แสดงด้านล่าง

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

เงินรางวัล

ในฐานะที่เป็นปลอบใจที่อนุญาตให้มีบิวด์อินฉันจะเสนอ 100-rep ค่าหัวให้แก่คำตอบที่สั้นที่สุดซึ่งไม่ได้ใช้ฟังก์ชันซีตาในตัว (เครื่องหมายถูกสีเขียวจะยังคงเป็นโซลูชันที่สั้นที่สุดโดยรวม)

การชนะ

รหัสที่สั้นที่สุดในหน่วยไบต์ชนะ


7
ความท้าทายนี้มีศักยภาพเช่นนี้ ... จนกว่าคุณจะอนุญาตให้สร้างภายใน ...
HyperNeutrino

@HyperNeutrino ใช่ฉันโพสต์เพราะฉันเห็นความท้าทายที่อนุญาตให้สร้างขึ้นภายใน FGITW
NoOneIsHere

2
"เพื่อความแม่นยำของทศนิยม 5 ตำแหน่ง" เข้มงวดหรือไม่? (เช่นเราสามารถส่งออกไปยังความแม่นยำมากขึ้น?) ถ้าไม่ใช่กรณีทดสอบควรแสดง 6dp จริง ๆ
Jonathan Allan

@JanathanAllen ฉันล้างข้อมูลการปัดเศษแล้ว
เบต้าการสลายตัว

3
@BetaDecay (ถอนหายใจไม่ ping) ควรป้อนข้อความที่ส่งออก19 จริง1.00000หรือไม่ จะไม่1หรือ1.0ถูกต้อง? ดูเหมือนว่าคุณได้ทำให้มันกลายเป็นความท้าทายกิ้งก่า
Jonathan Allan

คำตอบ:


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

ไม่มี builtin:

Mathematica, 23 UTF-8 ไบต์

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

ขอบคุณ Kelly Lowder


3
N@*Zetaบันทึกสองไบต์
Martin Ender

@*เป็น (ซ้าย) ผู้ประกอบการองค์ประกอบ: f@*gหมายถึงการใช้ฟังก์ชั่นที่มีค่าในการโต้แย้งคือx f[g[x]]
Greg Martin

@BetaDecay 1มันจะส่งออกComplexInfinityและมันจะไปรอบ5ๆ สถานที่ (เช่น1.64493)
NoOneIsHere

@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

ตัวอย่าง Runnable:

ζ=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 สัญลักษณ์ซีต้าเป็นชื่อฟังก์ชั่นที่ถูกต้องใน JS และคุณไม่ต้องการให้เล่นกอล์ฟ
คดีของกองทุนโมนิกา

แทนที่Array(1e6).fill()ด้วย[...Array(1e6)]และแทนที่รายการแรก(s)ด้วยs
Conor O'Brien

1
@QPaysTaxes จุดดี! ชื่อตัวแปร Unicode ftw!
Frxstrem

@ ConorO'Brien Huh ฉันไม่เคยรู้เลยว่าเคล็ดลับของ Array (ฉันคิดว่าอาร์เรย์เบาบางไม่ได้ซ้ำ แต่ฉันคิดว่าฉันผิด) ขอบคุณ!
Frxstrem

@ Frxstrem โปรดทราบว่าζใช้เวลาสองไบต์
CocoaBean

6

APL (Dyalog) , 22 21 ไบต์

ดูสิไม่มีบิวด์อิน! -1 ขอบคุณ ngn

ตั้งแต่ Dyalog APL ไม่ได้มีอนันต์ผมใช้สัญกรณ์เสนอ Iverson ของ

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

ลองออนไลน์!

{ ฟังก์ชั่นไม่ระบุชื่อ:

1=⍵: หากการโต้แย้งเป็นหนึ่งแล้ว:

  '¯' คืน macron

 อื่น

  !9 แฟกทอเรียลของเก้า (362880)

   แรกที่จำนวนเต็มฉัน ntegers

  ⍵*⍨ ยกพวกเขาไปสู่พลังของการโต้แย้ง

  ÷ ค่าต่าง ๆ

  +/ รวม

  5⍕ รูปแบบที่มีห้าทศนิยม

} [สิ้นสุดฟังก์ชั่นที่ไม่ระบุชื่อ]


1
1E6 -> !9­­
ngn

@ngn Thank you.
Adám

5

C, 74 70 69 bytes

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

Compile with -fwrapv. It will take some time to produce an output.

See it work here. The part ++n>0 is replaced with ++n<999999, so you don't have to wait. This keeps identical functionality and output.


Does float work?
l4m2

5

TI-Basic, 16 bytes (no builtins)

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

You really need to go up to about 150000 to get the answer right for Ans=2, which would take upwards of half an hour to calculate on an 84 Plus CE. Also, you could multiply by (Ans-1)^0 somewhere to get an error for Ans=1, TI-Basic's closest representation of infinity!
pizzapants184

@pizzapants184 I'm fully aware that 2, 3, etc. might take more than 99 iterations. You can achieve this functionality by replacing 99 with E9 where E is the scientific E, i.e. representing 10^9. (Or obviously something smaller like E5). Understanding that E99 is generally used for positive infinity also allows for this functionality theoretically, if the upper bound of the summation was E99. Emulators can provide this much faster than a physical calculator. Thanks for your thoughts :)
Timtech

I don't think this counts as displaying infinity. It won't even throw an error if you added 1 infinitely, due to floating-point imprecision.
lirtosiast

4

C (gcc), 112 101 94 84 bytes

Thanks for the golfing tips from ceilingcat.

n;f(s){float r;for(n=98;n;r+=pow(n--,-s));printf("%.5f",r+pow(99,-s)*(.5+99./--s));}

Try it online!


1
The question has been edited. You can output language native infinity symbols.
2501

@2501 I reverted back to the previous answer, although I'm still quite a few bytes away from your solution.
cleblanc

@ceilingcat f(1) doesn't seem correct.
cleblanc



2

MATL, 21 bytes

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

Try it online!

Explanation

Input 1 is special-cased to output inf, which is how MATL displays infinity.

For inputs other than 1, summing the first 2e5 terms suffices to achieve a precision of 5 decimal places. The reason is that, from direct computation, this number of terms suffices for input 2, and for greater exponents the tail of the series is smaller.

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 bytes

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

Finds the sum directly and formats as desired, outputs Inf if a is 1. Summing out to 9^6 appears to be enough to get five-place accuracy while still being testable; 9^9 would get better accuracy in the same length of code. I could get this shorter if R had a proper ternary operator.


1
function(a)round("if"(a-1,sum((1:9^6)^-a)),5) is a few bytes shorter.
Giuseppe

Yes, but it throws an error if a = 1. function(a)round("if"(a-1,sum((1:9^6)^-a),Inf),5) works and is still shorter than my original solution.
Michael Lugo

Oh yes of course! I forgot to include the Inf, that's what I get for typing code into the comment box directly...
Giuseppe

2

C,129 130 128 bytes

#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");}

it uses the following formula

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

test and results

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

Why this equation instead of Σ(1/(n^s))? It seems much more complicated...
Beta Decay

@BetaDecay because it seems to me more fast in find the result; here there is the range for sum s in 1..999, in the 'Σ(1/(n^s)) ' there is need s in the range 1..10^6
RosLuP

1
I see. FYI, simply oo is fine, you don't need to specify it as positive
Beta Decay


@ceilingcat you can write one other entry for this question... it seems I remember here without math.h header it not link...
RosLuP




1

Jelly, 23 bytes

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

Try it online!

How?

  • Sums the first million terms
  • Divides by 0 when abs(input)<=1 to yield inf (rather than 14.392726722864989) for 1
  • Rounds to 5 decimal places
  • Appends four zeros if abs(result)<=1 to format the 1.0 as 1.00000
  • Prints the result

ȷ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

Jelly, 26 bytes

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

Don't try it online with this link! (Since this uses 16!~20 trillion terms, running on TIO produces a MemoryError)

Try it online with this link instead. (Uses 1 million terms instead. Much more manageable but takes one more byte)

Returns inf for input 1.

Explanation

⁵*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)

Out of the 26, bytes, 7 are used for computation, 12 are for formatting, and 7 are for producing inf on zero. There has to be a better golf for this.


ȷ6 is a numeric literal of a million, removing the factorial workaround.
Jonathan Allan

0

MathGolf, 14 bytes (no builtins)

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

Note that in the TIO link, I have substituted for , which pushed 106 instead of 107. This is because the version submitted here timeouts for all test cases. This results in the answers for 3 and 8 to be off by 1 decimal place. However, there are way bigger 1-byte numerical literals in MathGolf, allowing for arbitrary decimal precision.

Try it online!

Explanation

┴                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

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.