g | x | w | all
Bytes Lang Time Link
031APLNARS250126T082639ZRosario
027CJam161108T093014ZPeter Ta
096Lithp161106T232923ZAndrakis
069Racket161106T053503Zrnso
042Haskell161106T200745ZTamoghna
047Groovy161106T192104ZTamoghna
079Scala161106T192535ZTamoghna
067Java/C/C++/C#161106T192805ZTamoghna
056Python 3161105T162539Zpajonk
068Python 3161105T161804Zpajonk
038R161105T125044Zrturnbul
013Jelly161105T151924ZJonathan
039JavaScript ES6161105T194810ZNeil
053Scala161105T194420Zcorvus_1
063Perl161105T170152ZGabriel
043JavaScript161105T120606Zuser4180
017MATL161105T130843ZLuis Men
045JavaScript ES6161105T145002Zedc65
036Haskell161105T120932ZAngs
018Mathematica161105T113452ZLegionMa

APL(NARS), 31 chars

{⍵≤1:⍵*⍨2×⍺⋄×/2p1⍺(⍺÷⍵)(⍺∇⍵-2)}

2p1 is 2π. Use formula

aV0=1, aV1=2a, aVw=2π(a^2/w)(aVw-2)

So

aV0=1, aV1=2a, aV2=2π(a^2/2)aV0=2π(a^2/2)=πa^2, aV3=2π(a^2/3)aV1=2π(a^2/3)2a=(4/3)πa^3 ecc

Test:

  f←{⍵≤1:⍵*⍨2×⍺⋄×/2p1⍺(⍺÷⍵)(⍺∇⍵-2)}
  3 f 2
28.27433388
  1 f 3
4.188790205
  1 f 1
2
  4.5 f 3
381.7035074
  9.379 f 1
18.758
  9.379 f 0
1

CJam (27 bytes with extra credit)

{1$_[2dP]*<f*\,:)-2%./1+:*}

Online test suite. This is an anonymous block (function) which takes arguments d r on the stack and leaves the result on the stack.

Dissection

The general n-dimensional formula can be rewritten as

$$\frac{2^{\left\lceil\frac{d}{2}\right\rceil}\pi^{\left\lfloor\frac{d}{2}\right\rfloor} r^d}{d!!}$$

{            e# Begin block: stack holds d r
  1$_[2dP]*< e#   Build a list which repeats [2 pi] d times and take the first d elements
  f*         e#   Multiply each element of the list by r
  \,:)-2%    e#   Build a list [1 ... d] and take every other element starting at the end
  ./         e#   Pointwise divide. The d/2 elements of the longer list are untouched
  1+:*       e#   Add 1 to ensure the list is non-empty and multiply its elements
}

Lithp, 96 characters + extra swag

Line split in 2 for readability:

#N,R::((if (< N 2) ((? (!= 0 N) (* 2 R) 1)) ((/ (* (* (* (* (f (- N 2) R) 2)
        3.1416) R) R) N))))

Thinking I need to upgrade my parser to require less spaces. Code size would be cut down nicely, especially in that ((/ (* (* (* (* section.

Usage:

% n-circle.lithp
(
    (def f #N,R::((if (< N 2) ((? (!= 0 N) (* 2 R) 1)) ((/ (* (* (* (* (f (- N 2) R) 2) 3.1416) R) R) N)))))
    (print (f 1 1))
    (print (f 2 3))
    (print (f 3 1))
    (print (f 3 4.5))
    (print (f 1 9.379))
    (print (f 0 48))
)

#./run.js n-circle.lithp
2
28.274333882308138
4.1887902047863905
381.7035074111598
18.758
1

Thanks to Rudolf for shaving a few bytes off.

Racket 69 bytes (plus extra swag)

Uses recursive formula from https://en.wikipedia.org/w/index.php?title=Volume_of_an_n-ball&section=3#Recursions

Including suggestions by @wchargin

(define(v d r)(match d[0 1][1(* 2 r)][_(/(* 2 pi r r(v(- d 2)r))d)]))

Ungolfed (v=volume, d=dimensions, r=radius):

(define(v d r)
  (match d
    [0 1]
    [1 (* 2 r)]
    [_ (/ (*  2   pi   r   r   (v (- d 2) r)  )
          d)]
    ))

Testing:

(v 1 1)
(v 2 3)
(v 3 1)
(v 3 4.5)
(v 1 9.379)
(v 0 48)

Output:

2
28.274333882308138
4.1887902047863905
381.7035074111599
18.758
1

Haskell, 52 bytes for tab indent 42 bytes + extra swag

Edit: Saved 10 bytes thanks to @WChargin

A dyadic curried function - first argument is number of dimensions, second is the radius of the n-ball.

v 0 r=1
v 1 r=2*r
v n r=2*pi*r*r*v(n-2)r/n

Recursive formula for the volume of an n-ball: Vn=(2πr2Vn-2)n

Save this as a separate script file and run with GHCi, with a function for testing v for output, e.g., show (v 3 4.5). I did not test this, please let me know if this doesn't work.

Old program with 6.2832 approximation for 2π replaced (50 bytes with tab indent):

let v 0 r=1
    v 1 r=2*r
    v n r=2*pi*r*r*(v(n-2)r)/n

This can be used with GHCi in multiline mode (using :set +m or enclosing the code between :{ & :}, the enclosures being on their own lines. Tester function required.

Static typing with full-program type inference comes into play here, allowing Haskell to do far better than Scala, and approaching Groovy, but not quite beating it thanks to the pattern match instead of a ternary, involving some character repetition.

Groovy, 49 47 bytes + extra swag!

Edit: Saved 2 bytes thanks to @AlexRacer

A dyadic function - first argument is number of dimensions, second is the radius of the n-ball.

def v(n,r){n<1?1:n<2?2*r:6.2832*r*r*v(n-2,r)/n}

Recursive formula for the volume of an n-ball: Vn=(2πr2Vn-2)n

Dynamic Typing FTW!

My Scala and Java answers use the same logic, but with static typing so higher byte counts due to type annotations :(. However, Scala and Groovy allow me to omit the return and the semicolon, so that helps the byte count, unlike Java/C...

Scala, 81 79 bytes + extra swag!

Edit: Saved 2 bytes thanks to @AlexRacer

A dyadic function - first argument is number of dimensions, second is the radius of the n-ball.

def v(n:Int,r:Float):Float=if n<1 1 else if n<2 2*r else 6.2832f*r*r*v(n-2,r)/n

Recursive formula for the volume of an n-ball: Vn=(2πr2Vn-2)n

Scala's lack of type inference for return types of recursive functions and function parameters and verbose ternary syntax hurts quite a bit here :(

Java/C/C++/C#, 69 67 bytes + extra swag!

Edit: Saved 2 bytes thanks to @AlexRacer

A dyadic function - first argument is number of dimensions, second is the radius of the n-ball.

float v(int n,float r){return n<1?1:n<2?2*r:6.283f*r*r*v(n-2,r)/n;}

Recursive formula for the volume of an n-ball: Vn=(2πr2Vn-2)n

Whoa! Java (my test language) beats Scala here, thanks to the terse ?: ternary syntax! This function is syntactically correct in all 4 languages in the heading, and I have tested it with C (MinGW GCC 5.4.0), & C# (VS Ultimate 2016, C# 6.0). I'm assuming that it will work in C++ too, so there. Since this function is pretty much library-independent, it should work in any C-like language with similar syntax.

Python 3, 56 bytes + extra swag!

Straightforward with extra swag!

from math import*
lambda n,r:pi**(n/2)*r**n/gamma(n/2+1)

Standard formula.

Try it online

Python 3, 76 72 68 bytes + extra swag!

Recursive solution with extra swag!
Returns 0 for n=0

from math import*
f=lambda n,r:n*r*2*(n<2or pi*r/n/n*(f(n-2,r)or 1))

Old approach (1 for n=1):

from math import*
f=lambda n,r:1*(n<1)or r*2*(n<2)or 2*pi*r*r/n*f(n-2,r)

Recursive formula from Wikipedia.

Try it online.

R, 75 40 38 bytes (plus extra swag)

Well, looks like I could golf this down by giving in and using the gamma function rather than recursive functions.

function(n,r)pi^(n/2)/gamma(n/2+1)*r^n

Defines an anonymous function to calculate the volume of an n-dimensional hypersphere of radius r.

Some examples:

1 1 -> 2

0 48 -> 1

2 3 -> 28.27433

3 4.5 -> 381.7035

7 7 -> 3891048

100 3 -> 122051813

Swagless solution, 38 34 bytes

For a few bytes less, you can have an anonymous function that only works for dimensions 1 to 3. Returns numeric(0) for n=0, and NA for n>3. (numeric(0) is a numeric vector of length 0; NA is for "not available".) Performance is otherwise identical to the general solution above.

function(n,r)c(1,pi,4/3*pi)[n]*r^n

Jelly, 13 bytes + extra swag

÷2µØP*÷!
ç×*@

Try it online!

Works for any dimension, so long as the fixed value of π yielded by ØP (3.141592653589793) is accurate enough.

How?

÷2µØP*÷! - Link 1: n, r
÷2       - n / 2
  µ      - monadic chain separation
   ØP    - π (3.141592653589793)
     *   - exponentiate: π^(n/2)
       ! - Pi(n/2): Gamma(n/2 + 1)
      ÷  - divide: π^(n/2) / Gamma(n/2 + 1)

ç×*@     - Main link: n, r
ç        - call last link (1) as a dyad: π^(n/2) / Gamma(n/2 + 1)
  *@     - exponentiate with reversed @rguments: r^n
 ×       - multiply: r^n * π^(n/2) / Gamma(n/2 + 1)

JavaScript (ES6), 39 bytes, no swag

(n,r)=>[1,r+r,a=Math.PI*r*r,a*r*4/3][n]

Scala, 53 bytes

{import math._;(n,r)=>pow(r,n)*Seq(1,2,Pi,Pi*4/3)(n)}

Sorry, no extra swag for me :(

Explanation:

{                     //define a block, the type of this is the type of the last expression, which is a function
  import math._;        //import everything from math, for pow and pi
  (n,r)=>               //define a function
    pow(r,n)*             //r to the nth power multiplied by
    Seq(1,2,Pi,Pi*4/3)(n) //the nth element of a sequence of 1, 2, Pi and Pi*4/3
}

Perl, 63 bytes + extra swag

@a=1..2;push@a,6.283/$_*@a[$_-2]for 2..($b=<>);say$a[$b]*<>**$b

Accepts two integers n and r, one at a time, then outputs the n-volume for given radius r of an n-sphere. When n = 0, V = 1, and when n = 1, V = 2r. All further dimensions are calculated by the following formula:

Recursive volume formula

Since rn is the radius's factor in every formula, I leave it out of the base calculation and only apply it at the end.

2π is approximated in the code by 6.283.

JavaScript, 61 51 49 43 bytes

0-3 dimensions are supported because there is no 4th dimension.

Thanks to @Hedi for saving 7 bytes

d=(n,r)=>r**n*(n<2?n+1:Math.PI*(n<3?1:4/3))

Creates function d. Then raises r to the nth power and then multiplies it with a number depending on n using ternary operators. Outputs 1 for n=0

Gives output to at least 2 decimal places (10+ dp)

Here's a snack snippet!

var N = document.getElementById("n");
var R = document.getElementById("r");
N.value="3";//default
R.value="4.5";//default
d=(n,r)=>r**n*(n<2?n+1:Math.PI*(n<3?1:4/3));
var b = document.getElementById("b");
b.onclick = function() {
  var s = document.getElementById("s");
  var n = document.getElementById("n").value;
  var r = document.getElementById("r").value;
  s.textContent = d(parseFloat(n),parseFloat(r));
}
span {border:1px solid black;padding:10px;font-size:30px;}
Value of n: <input id="n" type="number"></input>
Value of r: <input id="r" type="number"></input><br>
<button id="b">Calculate!</button><br><br><br>
<span id="s">THERE IS NO 4TH DIMENSION</span>

MATL, 17 bytes

3:^[2P4*P/3]*1hi)

This works up to 3 dimensions only. Inputs are in reverse order, that is: r, then n.

Try it online!

Consider r=3, n=2 as an example.

3:         % Push array [1 2 3]
           % STACK: [1 2 3]
^          % Take r implicitly, and raise it to [1 2 3] element-wise
           % STACK: [3 9 27]
[2P4*P/3]  % Push array [2 pi 4*pi/3]
           % STACK: [3 9 27], [2 pi 4*pi/3]
*          % Multiply element-wise
           % STACK: [6 28.2743 113.0973]
1h         % Append 1
           % STACK: [6 28.2743 113.0973, 1]
i)         % Input n and use it as modular index into the array. Display implicitly
           % STACK: 28.2743

JavaScript (ES6), 45 bytes + extra swag

Recursive formula from wikipedia, should work for any number of dimension

f=(n,r)=>n<2?n?2*r:1:f(n-2,r)*2*Math.PI*r*r/n

Haskell, 74 65 36 bytes + extra swag

0%r=1
1%r=2*r
n%r=2*pi*r^2/n*(n-2)%r

Recursive formula, works for all dimensions that can be presented exactly as a double-precision floating point number but will loop infinitely for non-integral dimensions. The old version for posteriority's sake:

n%r=(max 1$1-(-1)**n)*(2*pi)^(floor$n/2)*r**n/product[n,n-2..1.1]

Works for all dimensions. Uses the formula from the tau manifesto. product[n,n-2..1.1] is a double factorial hack that won't count zero for n==2

Mathematica, 18 bytes, up to ~168.15 trillion dimensions

Pi^(a=.5#)/a!#2^#&

Anonymous function. Takes two numbers as input, and returns an inexact number as output. Works with any number of dimensions. Outputs 1. for n = 0. Uses the formula from Volume of an n-ball on Wikipedia.

Explanation

We are attempting to compute πn/2/Γ(n/2 + 1)·Rn, or N[Pi^(n/2)/Gamma[n/2 + 1] R^n] in Mathematica. In our case, # (first argument) is n and #2 (second argument) is R. This leaves us with N[Pi^(#/2)/Gamma[#/2 + 1] #2^#] &, which can be golfed as follows:

N[Pi^(#/2)/Gamma[#/2 + 1] #2^#] &
Pi^(.5#)/Gamma[.5# + 1] #2^# &    (* replace exact with approximate numbers*)
Pi^(.5#)/(.5#)! #2^# &            (* n! == Gamma[n + 1] *)
Pi^(a=.5#)/a! #2^# &              (* replace repeated .5# *)
Pi^(a=.5#)/a!#2^#&                (* remove whitespace *)

and thus, our original program.