| Bytes | Lang | Time | Link |
|---|---|---|---|
| 010 | APLNARS | 250623T195952Z | Rosario |
| 103 | Setanta | 250625T001437Z | bb94 |
| 114 | Rust | 230104T233233Z | ceilingc |
| 020 | 05AB1E | 230110T114459Z | Kevin Cr |
| 019 | Vyxal | 230108T221520Z | pacman25 |
| 033 | J | 230105T065420Z | south |
| 021 | APL Dyalog | 170509T062653Z | Adá |
| 064 | JavaScript Node.js | 181217T171227Z | l4m2 |
| 014 | MathGolf | 181217T144220Z | maxb |
| 084 | C gcc | 170508T174830Z | cleblanc |
| 052 | Python 3 + SciPy | 170508T182336Z | totallyh |
| 016 | TIBasic | 170515T003216Z | Timtech |
| 011 | Mathematica | 170508T173106Z | NoOneIsH |
| 067 | Python 3 | 170511T080026Z | Chris H |
| 128 | C | 170509T211956Z | user5898 |
| 023 | Jelly | 170508T200101Z | Jonathan |
| 054 | R | 170509T134647Z | Michael |
| 026 | PARI/GP | 170509T045657Z | alephalp |
| 026 | Jelly | 170509T043745Z | fireflam |
| 065 | Javascript | 170508T201142Z | Freyja |
| 050 | Perl 6 | 170509T011455Z | Sean |
| 021 | MATL | 170508T191800Z | Luis Men |
| 069 | C | 170508T183726Z | 2501 |
| 036 | Julia | 170508T182744Z | Uriel |
APL(NARS), 10 chars
5⍕⍎'1z',⍕⎕
This use the builtin, input one number to stdin, output one string.
It would pass to execute ⍎, the string '1znumber_input_asString' that calculate z of number input and return it to 5⍕ that show the result until 5 digits or oo.
You can note this could be a hack, because Nars has not the zeta function (in what i know), but It has a way for calculate zeta const... Test
5⍕⍎'1z',⍕⎕
⎕:
1
∞
5⍕⍎'1z',⍕⎕
⎕:
2
1.64493
5⍕⍎'1z',⍕⎕
⎕:
3
1.20206
5⍕⍎'1z',⍕⎕
⎕:
4
1.08232
5⍕⍎'1z',⍕⎕
⎕:
8
1.00408
5⍕⍎'1z',⍕⎕
⎕:
19
1.00000
Setanta, 103 bytes
Setanta doesn’t have advanced formatting builtins, so I had to truncate the string at a fixed length (taking advantage of the fact that the result is always less than 10). The 200000 constant doubles as both the iteration count and the reciprocal of the offset to add to round (instead of truncating). Setanta does have an infinity value, but it’s not easy to produce (1/0 just gives an error; you have to do something like eas@mata(999)), so I output the string ∞.
gniomh(s){y:=200000x:=0le i idir(1,y)x+=cmhcht@mata(i,-s)toradh(s<2&"∞")|cuid@(go_teacs(x+1/y))(0,7)}
Rust, 133 129 120 117 114 bytes
fn f(s:f64){let mut y@mut n=s-s;while n<999.{n+=1.;y+=(n%2.*2.-1.)/n.powf(s)}print!("{:.5}",y/(1.-(1.-s).exp2()))}
Thanks to @Steffan for -9.
Slightly golfed less
fn f(s:f64){
let mut y@mut n=0.;
while n<999.{
n+=1.;
y-=f64::powf(-1.,n)/n.powf(s)
}
print!("{:.5}",y/(1.-(1.-s).exp2()))
}
05AB1E, 20 bytes
i'∞ë6°LImzO5.ò¾4׫7£
4 bytes are used to account for edge case \$n=1\$, and 9 bytes to round to five decimal places (6 of which for edge case 1.00000)..
Try it online or verify all test cases.
Explanation:
i # If the (implicit) input-integer is 1:
'∞ '# Push "∞"
ë # Else:
6° # Push 10 to the power 6: 1,000,000
L # Pop and push a list in the range [1,1000000]
Im # Get the power of the input on each integer
z # Calculate 1/value for each
O # Sum everything together
5.ò # Round to 5 decimal places
¾4× # Push a string of 4 zeros: "0000"
« # Append it to the result
7£ # Pop and leave just the first 7 characters: "1.abcde"
# (after which the result is output implicitly as result)
J, 33 bytes (no built-ins)
_:^:('*'&e.)7j5":1#.1%[^~1+i.@1e6
_:^:('*'&e.)7j5":1#.1%[^~1+i.@1e6
i.@1e6 NB. integers up to 1e6
1+ NB. add one
[^~ NB. nʸ for each in 1..1e6
1% NB. reciprocal of each
1#. NB. sum the result
7j5": NB. format each result, width of 7, 5 decimal places
^: NB. if
('*'&e.) NB. '*' is in the formatted result
NB. (": fills with *'s if the width is too small)
_: NB. return an infinity
APL (Dyalog), 22 21 bytes
Look ma, no built-ins! -1 thanks to ngn.
Since Dyalog APL does not have infinities, I use Iverson's proposed notation.
{1=⍵:'¯'⋄5⍕+/÷⍵*⍨⍳!9}
{ anonymous function:
1=⍵: if the argument is one, then:
'¯' return a macron
⋄ else
!9 factorial of nine (362880)
⍳ first that many integers integers
⍵*⍨ raise them to the power of the argument
÷ reciprocal values
+/ sum
5⍕ format with five decimals
} [end of anonymous function]
JavaScript (Node.js), 64 bytes
s=>[...Array(1e6)].reduce((a,b,i)=>s>1>i?a:i**-s+a,0).toFixed(5)
Pointed in Frxstrem's
MathGolf, 14 bytes (no builtins)
┴¿Å'∞{◄╒▬∩Σ░7<
Note that in the TIO link, I have substituted ◄ for ►, which pushed \$10^6\$ instead of \$10^7\$. 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.
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
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));}
TI-Basic, 16 bytes (no builtins)
Fix 5:Σ(X^~Ans,X,1,99
Mathematica, 9 7 11 bytes
Zeta@#~N~6&
Explanation:
Zeta@# (* Zeta performed on input *)
~N (* Piped into the N function *)
~6 (* With 6 digits (5 decimals) *)
& (* Make into function *)
Without builtin:
Mathematica, 23 UTF-8 bytes
Sum[1/n^#,{n,∞}]~N~6&
Thanks to Kelly Lowder
Python 3: 67 bytes (no built-ins)
f=lambda a:"∞"if a<2else"%.5f"%sum([m**-a for m in range(1,10**6)])
Nothing fancy, only uses python 3 because of the implicit utf-8 encoding.
Try it online with test cases.
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

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
Jelly, 23 bytes
ȷ6Rİ*⁸S÷Ị¬$ær5;ḷỊ?”0ẋ4¤
How?
- Sums the first million terms
- Divides by
0whenabs(input)<=1to yieldinf(rather than14.392726722864989) for1 - Rounds to 5 decimal places
- Appends four zeros if
abs(result)<=1to format the1.0as1.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
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.
PARI/GP, 27 26 bytes
\p 6
s->trap(,inf,zeta(s))
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.
Javascript, 81 70 66 65 bytes
s=>s-1?new Int8Array(1e6).reduce((a,b,i)=>a+i**-s).toFixed(5):1/0
Runnable examples:
ζ=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')
}
Perl 6, 50 bytes
{$_-1??(1..1e6).map(* **-$_).sum.fmt('%.5f')!!∞}
MATL, 21 bytes
q?'%.5f'2e5:G_^sYD}YY
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)
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.
Julia, 36 bytes
x->x!=1?@sprintf("%.5f",zeta(x)):Inf
