| Bytes | Lang | Time | Link |
|---|---|---|---|
| 012 | CASIO BASIC CASIO fx9750GIII | 250317T133528Z | madeforl |
| 054 | AWK | 250317T084534Z | roblogic |
| 015 | Uiua | 250316T214626Z | Braden C |
| 2058 | ☾ | 250316T172346Z | noodle p |
| 019 | Julia 0.6 | 151015T193518Z | Alex A. |
| 014 | Desmos | 250315T173911Z | DesmosEn |
| 022 | APLNARS | 250314T223345Z | Rosario |
| 019 | HP‑41C series | 250314T090012Z | Kai Burg |
| 080 | Scala | 230605T012011Z | 138 Aspe |
| 065 | Shasta v0.0.10 | 230604T223142Z | noodle p |
| nan | 151212T213615Z | Dennis | |
| 014 | Arn | 200824T211852Z | ZippyMag |
| 105 | Whispers v2 | 200827T170249Z | caird co |
| 016 | Brachylog | 200825T000615Z | Unrelate |
| 007 | MathGolf | 200824T085425Z | Kevin Cr |
| 007 | 05AB1E | 200824T084855Z | Kevin Cr |
| 045 | Haskell | 200824T071846Z | xnor |
| 013 | Pyt | 171225T211539Z | mudkip20 |
| 049 | Python 3 | 170713T001602Z | musicman |
| 137 | Axiom | 170424T192509Z | user5898 |
| 105 | SmileBASIC | 170117T183512Z | snail_ |
| 018 | J | 151201T033737Z | Dennis |
| 038 | Perl5 | 151016T163603Z | msh210 |
| 017 | Dyalog APL | 151015T181004Z | Dennis |
| 119 | Prolog SWI | 151201T121203Z | Emigna |
| 138 | Fortran | 151024T041201Z | Time Lai |
| 073 | JavaScript ES6 | 151015T174638Z | Mwr247 |
| 122 | PowerShell | 151016T063405Z | blabb |
| 022 | Mathematica | 151015T205719Z | Martin E |
| 057 | Python | 151015T194419Z | Beta Dec |
| 043 | Simplex v.0.5 | 151016T060000Z | Conor O& |
| 003 | kdb+ | 151016T032047Z | Dennis |
| 021 | CJam | 151015T171659Z | Dennis |
| 030 | Python 3.4+ | 151015T181608Z | Sp3000 |
| 013 | Pyth | 151015T175938Z | Dennis |
| 028 | R | 151015T175811Z | Mutador |
| 014 | Octave | 151015T175520Z | beaker |
| 007 | TIBASIC | 151015T172208Z | lirtosia |
| 024 | APL | 151015T184745Z | Alex A. |
| 061 | Haskell | 151015T182734Z | nimi |
| 003 | Clip | 151015T180013Z | Ypnypn |
CASIO BASIC (CASIO fx-9750GIII), 12 bytes
?→List 1
1-Variable List 1
σx
you need to put a curly brace ({) before your input
Uiua, 15 Characters
√-ⁿ2∩(/+÷⊸⧻)⤙ⁿ2
This takes advantage of the fact that $$\frac{1}{n}\sum_{i=1}^n(x_i-\mu)^2 = \frac{1}{n}\sum_{i=1}^n x_i^2 - 2\mu\frac{1}{n}\sum_{i=1}^nx_i+\mu^2 = \frac{1}{n}\sum_{i=1}^nx_i^2 - \mu^2.$$ Explanation:
√ # take a square root
- # take the difference between top two numbers on stack
ⁿ2 # square first entry of stack
∩ # apply the next function on top two items on stack
(/+÷⊸⧻) # take average of a list of numbers
⤙ⁿ2 # square the top item on stack, but place result beneath the original list
Julia 0.6, 26 19 bytes
x->std([x;mean(x)])
This creates an unnamed function that accepts an array and returns a float.
Ungolfed, I guess:
function f(x::Array{Int,1})
# Return the sample standard deviation (denominator N-1) of
# the input with the mean of the input appended to the end.
# This corrects the denominator to N without affecting the
# mean.
std([x; mean(x)])
end
Desmos, 14 bytes
g(l)=stdevp(l)
This version just uses a built-in to calculate the population standard deviation.
f(l)=\sqrt{(l-l.\mean)^2.\mean}
This second version is 32 bytes, and doesn't use the built-in standard deviation function. It works by taking the square root of the average of an array containing the squares of the differences between each element in l and the mean of l.
APL(NARS), 22 chars
{√n÷⍨+/×⍨⍵-(+/⍵)÷n←≢⍵}
It seems ugly and difficult to read but it is easy.
f←{√n÷⍨+/×⍨⍵-(+/⍵)÷n←≢⍵}
f 45 67 32 98 11 3
32.53032773
f 1 1 1 1 1 1
0
f 10035 436844 42463 44774
175656.7844
HP‑41C series, 19 B
The HP‑41C series features a number of built‐in statistics functions.
However, as Mutador reckoned, the built‐in SDEV function calculates the sample standard deviation.
$$
s_x = \sqrt[2]{\frac{n \times \sum\left(x^2\right) - \left(\sum x\right)^2}{n \times \left(n - 1\right)}}
$$
To get the true population standard deviation, add the arithmetic mean to the accumulated data right before executing SDEV.
01♦LBL⸆S 5 Bytes global label requires 4 + 𝘯 Bytes
02 CL∑ 1 Byte clear statistics registers
03 LBL 00 1 Byte local label marks the head of the loop ── ← ───┐
│
04 STOP 1 Byte stop program execution │
│
Now the user can enter a number. Entry is │
concluded by pressing the “R/S” (run/stop) │
key. If a number has been entered, the flag │
22 gets set. │
│
05 FS? 22 2 Bytes if flag22 then ──┐ │
06 ∑+ 1 Byte └── R₁₁ ≔ R₁₁ + 𝑢 ∑𝑥 │
│
07 FS?C 22 2 Bytes if flag22 then ──┐ │
08 GTO 00 2 Bytes └── flag22 ≔ false; goto 00 ──┘
09 MEAN 1 Byte X ≔ ∑𝑥 ∕ 𝑛
10 ∑+ 1 Byte add arithmetic mean to statistical data
11 SDEV 1 Byte calculate sample standard deviation
12 RTN 1 Byte `RTN` does not affect local label search
so it is imperative there is not another
LBL 00 in subsequent program memory
After having pressed R/S twice (while the “flying goose” was not displayed) you get the population standard deviation.
Keep in mind the statistics accumulation function ∑+ performs saturation arithmetic (±9.999999999 × 1099) regardless of the current value of flags 24 and 25 (flags controlling how to handle an out‐of‐range error).
Scala, 80 bytes
Golfed version. Try it online!
l=>{val s=(0 to 2).map(i=>l.map(pow(_,i)).sum);sqrt(s(2)/s(0)-pow(s(1)/s(0),2))}
Ungolfed version. Try it online!
object Main {
def powerSum(i: Int, list: List[Int]): Double =
list.map(scala.math.pow(_, i)).sum
def f(l: List[Int]): Double = {
val sum0 = powerSum(0, l)
val sum1 = powerSum(1, l)
val sum2 = powerSum(2, l)
scala.math.sqrt(sum2 / sum0 - scala.math.pow(sum1 / sum0, 2))
}
def main(args: Array[String]): Unit = {
println(f(List(45, 67, 32, 98, 11, 3)))
}
}
Shasta v0.0.10, 65 bytes
{(**(/(sum(map${|x|(**(- x(/(sum$)(length$)))2)}))(length$)).5)}
Online interpreter not set up yet, tested on my machine
Explanation:
{...} Function taking argument $
(** ... .5) Square root of
(/ ... (length $)) Dividing ... by the length of $
(sum ...) The sum of
(map $ {|x| ...}) Mapping each x in $ to
(** ... 2) The square of
(- x ...) The difference between x and
(/ (sum $) (length $)) The mean of $
Jelly
11 bytes
S÷L
Dz_²ÇN½
This is a direct translation of my APL answer to Jelly. Try it online!
How it works
S÷L Helper link. Argument: z (vector)
S Compute the sum of z.
L Compute the length of z.
÷ Divide the former by the latter.
This computes the mean of z.
Dz_²ÇN½ Main link. Argument: z (vector)
Ç Apply the previous link, i.e., compute the mean of z.
² Square the mean.
² Square all number in z.
_ Subtract each squared number from the squared mean.
Ç Take the mean of the resulting vector.
N Multiply it by -1.
½ Take the square root of the result.
Arn, 19 18 14 bytes
¯‡iʠØbmÅQƥªÈªÆ
Explained
Unpacked: :/mean(n{:*n-mean:s}\
:/ Square root
mean Mean function
( Begin expression
n{ Block with key of n
:* Square
n
- Subtraction
mean
_ Variable initialized to STDIN; implied
:s Split on spaces
} End of block
\ Mapped over
_ Implied
) End of expression; Implied
Whispers v2, 105 bytes
> Input
>> #1
>> ∑1
>> 3÷2
>> L-4
>> L²
>> Each 5 1
>> Each 6 7
>> ∑8
>> 9÷2
>> √10
>> Output 11
In the latest version of Whispers, the builtin σ can be used to shave off around 70 bytes.
How it works
For those unfamiliar with Whispers, the language works by using numbers as line references in order to pass values between lines. For example, the line >> 3÷2 doesn't calculate \$3 \div 2\$, rather it takes the values of lines 3 and 2 and calculates their division. Execution always begins on the last line.
This program simply implements the standard formula for standard deviation:
$$\sigma = \sqrt{\frac{1}{N}\sum^N_{i=1}{(x_i-\bar{x})^2}}$$ $$\bar{x} = \frac{1}{N}\sum^N_{i=1}{x_i}$$
Lines 2, 3 and 4 define \$\bar{x}\$, with it's specific value accessible on line 4. Line 2 stores \$N\$. We then calculate \$(x_i-\bar{x})^2\$ for each \$x_i \in x\$ with the lines 5, 6, 7 and 8:
>> L-4
>> L²
>> Each 5 1
>> Each 6 7
Line 7 runs line 5 over each element in the input, which takes the difference between each element in the input and the mean, We then square these differences using lines 8 and 6. Finally, we take the sum of these squares (line 9), divide by \$N\$ (line 10) and take the square root (line 11). Finally, we output this result.
Brachylog, 16 bytes
⟨∋-⟨+/l⟩⟩ᶠ^₂ᵐ↰₂√
Try it online! (all cases at once)
I feel like maybe there's some shorter way to compute the squared deviations than 13 bytes.
MathGolf, 10 7 bytes
ë_▓-²▓√
Port of my 05AB1E answer.
Explanation:
ë # Read all inputs as float-list
_ # Duplicate that list
▓ # Get the average of that list
- # Subtract that average from each value in the list
² # Square each value
▓ # Take the average of that again
√ # And take the square root of that
# (after which the entire stack joined together is output implicitly as result)
05AB1E, 10 7 bytes
ÅA-nÅAt
-3 bytes thanks to @ovs.
Try it online or verify all test cases.
Explanation:
ÅA # Get the arithmetic mean of the (implicit) input-list
- # Subtract it from each value in the (implicit) input-list
n # Square each of those
ÅA # Take the arithmetic mean of that
t # And take the square-root of that
# (after which it is output implicitly as result)
Haskell, 45 bytes
(?)i=sum.map(^i)
f l=sqrt$2?l/0?l-(1?l/0?l)^2
The value i?l is the sum of the i'th powers of elements in l, so that 0?l is the length and 1?l is the sum.
Python 3, 49 bytes
lambda l,N:(sum((i-sum(l)/N)**2for i in l)/N)**.5
Takes in l, a list of integers, and N, the number of integers present.
Axiom, 137 bytes
m(a:List Float):Complex Float==(#a=0=>%i;reduce(+,a)/#a)
s(a:List Float):Complex Float==(#a=0=>%i;n:=m(a);sqrt(m([(x-n)^2 for x in a])))
The function m() would return the mean of the list in input. Both the functions on error return %i the imaginary constant sqrt(-1). Code for test and results. [but the result if it is ok, it is the real part of one complex number]
(6) -> s([45,67,32,98,11,3])
(6) 32.5303277300 15604966
(7) -> s([10035,436844,42463,44774])
(7) 175656.7844135261 4035
(8) -> s([1,1,1,1,1,1])
(8) 0.0
SmileBASIC, 105 bytes (as a function)
I just noticed it's allowed to be a function. Whoops, that reduces my answer dramatically. This defines a function S which takes an array and returns the population standard deviation. Go read the other one for an explanation, but skip the parsing part. I don't want to do it again.
DEF S(L)N=LEN(L)FOR I=0TO N-1U=U+L[I]NEXT
U=1/N*U FOR I=0TO N-1T=T+POW(L[I]-U,2)NEXT RETURN SQR(1/N*T)END
As a program, 212 bytes
Unfortunately, I have to take the input list as a string and parse it myself. This adds over 100 bytes to the answer, so if some input format other than a comma-separated list is allowed I'd be glad to hear it. Also note that because VAL is buggy, having a space before the comma or trailing the string breaks the program. After the comma or at the start of the string is fine.
DIM L[0]LINPUT L$@L I=INSTR(O,L$,",")IF I>-1THEN PUSH L,VAL(MID$(L$,O,I-O))O=I+1GOTO@L ELSE PUSH L,VAL(MID$(L$,O,LEN(L$)-O))
N=LEN(L)FOR I=0TO N-1U=U+L[I]NEXT
U=1/N*U FOR I=0TO N-1T=T+POW(L[I]-U,2)NEXT?SQR(1/N*T)
Ungolfed and explained:
DIM L[0] 'define our array
LINPUT L$ 'grab string from input
'parse list
'could've used something cleaner, like a REPEAT, but this was shorter
@L
I=INSTR(O,L$,",") 'find next comma
IF I>-1 THEN 'we have a comma
PUSH L,VAL(MID$(L$,O,I-O)) 'get substring of number, parse & store
O=I+1 'set next search location
GOTO @L 'go again
ELSE 'we don't have a comma
PUSH L,VAL(MID$(L$,O,LEN(L$)-O)) 'eat rest of string, parse & store
ENDIF 'end
N=LEN(L) 'how many numbers we have
'find U
'sum all of the numbers, mult by 1/N
FOR I=0 TO N-1
U=U+L[I]
NEXT
U=1/N*U
'calculate our popstdev
'sum(pow(x-u,2))
FOR I=0 TO N-1
T=T+POW(L[I]-U,2)
NEXT
PRINT SQR(1/N*T) 'sqrt(1/n*sum)
Perl5, 39 38
16 for the script
+22 for the M switch
+ 1 for the E switch
=39
perl -MStatistics::Lite=:all -E"say stddevp@ARGV" .1 .2 300
Tested in Strawberry 5.20.2.
Oh, but then I realized that you said our answers can be functions instead of programs. In that case,
{use Statistics::Lite":all";stddevp@_}
has just 38. Tested in Strawberry 5.20.2 as
print sub{use Statistics::Lite":all";stddevp@_}->( .1, .2, 300)
Dyalog APL, 24 23 21 20 19 17 bytes
*∘.5∘M×⍨-M×M←+/÷≢
This defines an unnamed, monadic function train, which is equivalent to the following function.
{.5*⍨M(×⍨⍵)-M⍵×(M←{(+/⍵)÷≢⍵})⍵}
Try them online on TryAPL.
How it works
The code consists of several trains.
M←+/÷≢
This defines a monadic 3-train (fork) M that executes +/ (sum of all elements) and ≢ (length) for the right argument, then applies ÷ (division) to the results, returning the arithmetic mean of the input.
M×M
This is another fork that applies M to the right argument, repeats this a second time, and applies × (product) to the results, returning μ2.
×⍨-(M×M)
This is yet another fork that calculates the square of the arithmetic mean as explained before, applies ×⍨ (product with itself) to the right argument, and finally applies - (difference) to the results.
For input (x1, …, xN), this function returns (x1 - μ2, …, xN - μ2).
*∘.5∘M
This composed function is applies M to its right argument, then *∘.5. The latter uses right argument currying to apply map input a to a*0.5 (square root of a).
(*∘.5∘M)(×⍨-(M×M))
Finally, we have this monadic 2-train (atop), which applies the right function first, then the left to its result, calculating the standard deviation as follows.
Prolog (SWI), 119 bytes
Code:
q(U,X,A):-A is(X-U)^2.
p(L):-sumlist(L,S),length(L,I),U is S/I,maplist(q(U),L,A),sumlist(A,B),C is sqrt(B/I),write(C).
Explanation:
q(U,X,A):-A is(X-U)^2. % calc squared difference of X and U
p(L):-sumlist(L,S), % sum input list
length(L,I), % length of input list
U is S/I, % set U to the mean value of input list
maplist(q(U),L,A), % set A to the list of squared differences of input and mean
sumlist(A,B), % sum squared differences list
C is sqrt(B/I), % divide sum of squares by length of list
write(C). % print answer
Example:
p([10035, 436844, 42463, 44774]).
175656.78441352615
Try it out online here
Fortran, 138 bytes
Just a straightforward implementation of the equation in Fortran:
double precision function std(x)
integer,dimension(:),intent(in) :: x
std = norm2(dble(x-sum(x)/size(x)))/sqrt(dble(size(x)))
end function
JavaScript (ES6), 73 bytes
a=>Math.sqrt(a.reduce((b,c)=>b+(d=c-eval(a.join`+`)/(l=a.length))*d,0)/l)
PowerShell, 122
:\>type stddev.ps1
$y=0;$z=$args -split",";$a=($z|?{$_});$c=$a.Count;$a|%{$y+=$_};$b=$y/$c;$a|%{$x+
=(($_-$b)*($_-$b))/$c};[math]::pow($x,0.5)
explanation
<#
$y=0 init
$z=$args -split"," split delim ,
$a=($z|? {$_}) remove empty items
$c=$a.Count count items
$a|%{$y+=$_} sum
$b=$y/$c average
$a|%{$x+=(($_-$b)*($_-$b))/$c} sum of squares/count
[math]::pow($x,0.5) result
#>
result
:\>powershell -nologo -f stddev.ps1 45,67,32,98,11,3
32.5303277300156
:\>powershell -nologo -f stddev.ps1 45, 67,32,98,11,3
32.5303277300156
:\>powershell -nologo -f stddev.ps1 45, 67,32, 98 ,11,3
32.5303277300156
:\>powershell -nologo -f stddev.ps1 10035, 436844, 42463, 44774
175656.784413526
:\>powershell -nologo -f stddev.ps1 1,1,1,1,1,1
0
Mathematica, 24 22 bytes
Nice, Mathematica has a built-in StandardDevi... oh... that computes the sample standard deviation, not the population standard deviation.
But what if we use Variance... oh... same deal.
But there is yet another related built-in:
CentralMoment[#,2]^.5&
Yay. :)
This also works for 22 bytes:
Mean[(#-Mean@#)^2]^.5&
And this for 27:
N@RootMeanSquare[#-Mean@#]&
Python, 57 bytes
lambda l:(sum((x-sum(l)/len(l))**2for x in l)/len(l))**.5
Takes input as a list
Thanks @xnor
Simplex v.0.5, 43 bytes
Just 'cuz. I really need to golf this one more byte.
t[@u@RvR]lR1RD@wA@T@{j@@SR2ERpR}u@vR@TR1UEo
t[ ] ~~ Applies inner function to entire strip (left-to-right)
@ ~~ Copies current value to register
u ~~ Goes up a strip level
@ ~~ Dumps the register on the current byte
R ~~ Proceeds right (s1)
v ~~ Goes back down
R ~~ Proceeds right (s0)
~~ Go right until an empty byte is found
lR1RD ~~ Push length, 1, and divide.
@ ~~ Store result in register (1/N)
wA ~~ Applies A (add) to each byte, (right-to-left)
@T@ ~~ Puts 1/N down, multiplies it, and copies it to the register
{ } ~~ Repeats until a zero-byte is met
j@@ ~~ inserts a new byte and places register on it
SR ~~ Subtract it from the current byte and moves right
2E ~~ Squares result
RpR ~~ Moves to the recently-created cell, deletes it, and continues
u@v ~~ takes 1/N again into register
R@T ~~ multiplies it by the new sum
R1UE ~~ takes the square root of previous
o ~~ output as number
kdb+, 3 bytes
dev
One of the APL derviates had to have this as a built-in.
Test run
q)dev 56, 54, 89, 87
16.53028
q)f:dev
q)f 10035, 436844, 42463, 44774
175656.8
q)f 45,67,32,98,11,3
32.53033
CJam, 24 22 21 bytes
q~_,_@_:+d@/f-:mh\mq/
Thanks to @aditsu for golfing off 1 byte!
Try it online in the CJam interpreter.
How it works
q~ e# Read all input and evaluate it.
_, e# Copy the array and push its length.
_@ e# Copy the length and rotate the array on top.
_:+d e# Copy the array and compute its sum. Cast to Double.
@/ e# Rotate the length on top and divide the sum by it.
f- e# Subtract the result (μ) from the array's elements.
:mh e# Reduce by hypotenuse.
e# a b mh -> sqrt(a^2 + b^2)
e# sqrt(a^2 + b^2) c mh -> sqrt(sqrt(a^2 + b^2)^2 + c^2)
e# = sqrt(a^2 + b^2 + c^2)
e# ⋮
\mq/ e# Divide the result by the square root of the length.
Python 3.4+, 30 bytes
from statistics import*;pstdev
Imports the builtin function pstdev, e.g.
>>> pstdev([56,54,89,87])
16.53027525481654
Pyth, 20 19 17 13 bytes
@.O^R2-R.OQQ2
Thanks to @FryAmTheEggman for golfing off 4 bytes!
How it works
.OQ Compute the arithmetic mean of the input (Q).
-R Q Subtract the arithmetic mean of all elements of Q.
^R2 Square each resulting difference.
.O Compute the arithmetic mean of the squared differences.
@ 2 Apply square root.
R, 41 40 39 36 30 28 bytes
code
Thanks to beaker, Alex A. and MickyT for much bytes.
cat(sd(c(v=scan(),mean(v))))
old codes
v=scan();n=length(v);sd(v)/(n/(n-1))**0.5
m=scan();cat(sqrt(sum(mean((m-mean(m))^2))))
m=scan();cat(mean((m-mean(m))^2)^.5)
This should yield the population standard deviation.
TI-BASIC, 7 bytes
stdDev(augment(Ans,{mean(Ans
I borrowed the algorithm to get population standard deviation from sample standard deviation from here.
The shortest solution I could find without augment( is 9 bytes:
stdDev(Ans√(1-1/dim(Ans
APL, 24 bytes
{.5*⍨+/(2*⍨⍵-+/⍵÷≢⍵)÷≢⍵}
A little different approach than Dennis' Dyalog APL solution. This should work with any APL implementation.
This creates an unnamed monadic function that computes the vector (x - µ)2 as 2*⍨⍵-+/⍵÷≢⍵, divides this by N (÷≢⍵), takes the sum of this vector using +/, and then takes the square root (.5*⍨).
Haskell, 61 bytes
d n=1/sum(n>>[1])
f a=sqrt$d a*sum(map((^2).(-)(d a*sum a))a)
Straightforward, except maybe my custom length function sum(n>>[1]) to trick Haskell's strict type system.

