แวดวง N-Dim!


16

เขียนโปรแกรมที่ใช้ตัวเลขสองตัวเป็นอินพุต คนแรกคือจำนวนมิติ - 0 สำหรับจุด, 1 สำหรับเส้นตรง, 2 สำหรับวงกลม, 3 สำหรับทรงกลม ตัวเลขที่สองคือรัศมีของวัตถุหรือถ้าเป็น 1 มิติก็จะเป็นตัวเลข เอาท์พุท 0 สำหรับ 0 มิติ ผลลัพธ์คือความยาว / พื้นที่ / ปริมาตรของวัตถุ

หากเราเรียกหมายเลขแรกหมายเลขnที่สองrและผลลัพธ์xเราจะได้รับ:

  • สำหรับ n = 0, x = 1

  • สำหรับ n = 1, x = 2 × r

  • สำหรับ n = 2, x = r 2 ×π

  • สำหรับ n = 3 x = ( 4 / 3 ) × R 3 ×π

  • และอื่น ๆ ... หากคุณต้องการ

หมายเหตุ:

  • กรณีที่ตัวเลขหนึ่งหรือทั้งสองเป็นลบหรือเมื่อตัวเลขแรกไม่ครบต้องไม่ครอบคลุม

  • โปรแกรมจะต้องไม่อ่านจากไฟล์ใด ๆ และอินพุตเพียงอย่างเดียวคือตัวเลขทั้งสอง

  • ผลลัพธ์ควรใช้เฉพาะตัวเลข (เช่นไม่ใช่ "14 * pi") และควรมีความถูกต้องกับทศนิยมอย่างน้อยสองหลัก

  • สำหรับ n = 0 คุณสามารถส่งออก 0 ถ้ามันทำให้รหัสสั้นลง

  • ย้อยเป็นพิเศษสำหรับคำตอบที่ครอบคลุม "โฟโตสเฟียร์" ขนาด 4 นิ้วและมากกว่า!

  • มันเป็นดังนั้นคำตอบที่สั้นที่สุดในหน่วยไบต์ชนะ!

ตัวอย่าง:

 1 1 -> 2

 2 3 -> 28,27

 3 1 -> 4,19

 3 4,5 -> 381,70

 1 9.379 -> 18.758

 0 48 -> 1

2
เย้! ฉันชอบสมการ MathJax ที่ผิด ๆ ในโพสต์!
RudolfJelin

1
เพื่อไม่ให้วิจารณ์ แต่ฉันไม่เห็นว่าเส้นใดถือได้ว่าเป็นวงกลม 1d ...
xem

10
@xem พิจารณาวงกลมเป็นจุดทั้งหมดที่อยู่ภายในระยะทางที่กำหนดจากศูนย์
Luis Mendo

3
ประเภทคณิตศาสตร์จะเรียกว่า "ลูก" เหล่านี้ในมิติต่าง ๆ ชุดของจุดที่มีระยะทางจากจุดกำเนิด== rคือทรงกลมชุดของจุดที่มีระยะทางจากจุดกำเนิด<= rคือลูกบอล จากนั้นเหล่านี้คือ 0 ลูก = จุด, 1 ลูก = ส่วน, 2 ลูก = ดิสก์, 3 ลูก = ลูก, 4 ลูก, 5 ลูก, และอื่น ๆ (แสดงเป็น " n-ball = ชื่อสามัญ")
Eric Towers

3
"เอาต์พุต 0 สำหรับ 0 มิติ" และ "สำหรับ n = 0, x = 1" ขัดแย้งกัน คุณกรุณาเลือกหนึ่งตัว (หรืออธิบายให้ชัดเจนว่าทั้งคู่ได้รับอนุญาต) หรือไม่?
Paŭlo Ebermann

คำตอบ:


7

เจลลี่ , 13 ไบต์ + ย้อยเสริม

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

ลองออนไลน์!

ใช้งานได้กับทุกมิติตราบใดที่ค่าคงที่ของπให้ผลโดยØP( 3.141592653589793) มีความแม่นยำเพียงพอ

อย่างไร?

÷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)

1
ทำได้ดีมากสำหรับการเอาชนะ Mathematica!
CJ Dennis

ยินดีด้วยคุณชนะแล้ว!
RudolfJelin

13

Mathematica ขนาด 18 ไบต์สูงสุด ~ 1681500000000 มิติ

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

ฟังก์ชั่นไม่ระบุชื่อ รับตัวเลขสองตัวเป็นอินพุตและส่งคืนหมายเลขที่ไม่แน่นอนเป็นเอาต์พุต ใช้ได้กับทุกขนาด เอาต์พุต1.สำหรับn = 0 ใช้สูตรจากปริมาณของ n-ballบน Wikipedia

คำอธิบาย

เรากำลังพยายามคำนวณπ n / 2 / Γ ( n / 2 + 1) · R nหรือN[Pi^(n/2)/Gamma[n/2 + 1] R^n]ใน Mathematica ในกรณีของเรา#(อาร์กิวเมนต์แรก) เป็นnและ#2(อาร์กิวเมนต์ที่สอง) เป็นR สิ่งนี้ทำให้เราด้วยN[Pi^(#/2)/Gamma[#/2 + 1] #2^#] &ซึ่งสามารถกอล์ฟได้ดังนี้:

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

และดังนั้นโปรแกรมดั้งเดิมของเรา


คำตอบที่ดี - นั่นเร็วมาก! เพื่อความกระจ่าง: เอาท์พุทถูกต้องกี่หลัก สามารถคำนวณได้กี่มิติ
RudolfJelin

@ RudolfL.Jelínekมันส่งออกไปประมาณ 5 ตัวเลขที่สำคัญและใช้งานได้กับnทั้งหมดสูงถึง 168,146,894,169,516 สำหรับr = 1 (แม้ว่าจะมีตัวเลขน้อยกว่า)
LegionMammal978

@ LegionMammal978 สูตรไหน? ฉันค่อนข้างแน่ใจว่าคุณไม่ได้ใช้ฟังก์ชั่นแกมม่าที่นั่น
Angs

@Angs n ! = Γ  (  n + 1)
LegionMammal978

2
โอ้!ทำงานสำหรับผู้ที่ไม่ได้รวมด้วยเช่นกัน การใช้ Mathematica สำหรับสิ่งนี้เกือบจะรู้สึกเหมือนการโกง… :)
Angs


6

R, 75 40 38 ไบต์ (บวกพวงหรีดพิเศษ)

ดูเหมือนว่าฉันจะลงสนามได้ด้วยการยอมแพ้และใช้ฟังก์ชั่นแกมม่าแทนฟังก์ชั่นวนซ้ำ

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

กำหนดฟังก์ชั่นที่ไม่ระบุชื่อในการคำนวณปริมาณของนั้นnhypersphere rมิติรัศมี

ตัวอย่างบางส่วน:

1 1 -> 2

0 48 -> 1

2 3 -> 28.27433

3 4.5 -> 381.7035

7 7 -> 3891048

100 3 -> 122051813

วิธีแก้ปัญหา Swagless, 38 34 ไบต์

ในอีกไม่กี่ไบต์น้อยคุณสามารถมีฟังก์ชั่นที่ไม่ระบุชื่อที่ทำงานเฉพาะสำหรับขนาด 1 ถึง 3. ผลตอบแทนnumeric(0)สำหรับn=0และสำหรับNA n>3( numeric(0)เป็นเวกเตอร์ตัวเลขที่มีความยาว 0; ใช้NAสำหรับ "ไม่พร้อมใช้งาน") ประสิทธิภาพนั้นไม่เหมือนกันกับโซลูชันทั่วไปด้านบน

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

1
₊₁สำหรับ SSSSSWWWWWAAAAAAAGGGGGGGGGGG!
RudolfJelin

5

Haskell, 74 65 36 bytes + swag พิเศษ

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

สูตรเรียกซ้ำใช้งานได้กับทุกมิติที่สามารถนำเสนอเป็นตัวเลขจุดลอยตัวที่มีความแม่นยำสองเท่า แต่จะวนซ้ำอย่างไม่สิ้นสุดสำหรับมิติที่ไม่รวม รุ่นเก่าเพื่อประโยชน์ของคนหลัง:

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

ใช้งานได้กับทุกมิติ ใช้สูตรจากแถลงการณ์เอกภาพ product[n,n-2..1.1]เป็นแฮ็คแฟ็กทอเรียลสองเท่าที่จะไม่นับรวมn==2


5

JavaScript, 61 51 49 43 ไบต์

0-3 มิติได้รับการสนับสนุนเพราะไม่มีมิติที่ 4

ขอบคุณ @Hedi สำหรับการบันทึก 7 ไบต์

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

dสร้างฟังก์ชั่น แล้วยกrไปที่nอำนาจ TH แล้วคูณด้วยจำนวนขึ้นอยู่กับnการใช้ประกอบ ternary ผลลัพธ์1สำหรับn=0

ให้เอาต์พุตเป็นทศนิยมอย่างน้อย 2 ตำแหน่ง (10+ dp)

นี่เป็นตัวอย่างของว่าง!

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>


เอาชนะโซลูชันที่ไม่โพสต์ของฉันโดย ... โดยมาก +1!
RudolfJelin

6
วิดีโอที่น่าเบื่อ…
ชื่อที่แสดง

1
@SargeBorsch อย่างน้อยก็พิสูจน์ให้เห็นจุดของฉัน :)
Kritixi Lithos

2
@SargeBorsch ฮ่าฮ่า yup dumb video - 0:40 3 dimensions that behave in the same way and one that behaves in a different way- ณ จุดนั้นเขาดูเหมือนจะพูดว่ามีมิติที่ 4 แต่ไม่มีที่ 1, 2 หรือ 3!
ระดับแม่น้ำเซนต์

1
@LevelRiverSt Well that was the first result I was on the web ¯\_(ツ)_/¯
Kritixi Lithos

3

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

2

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.


Wow! I thought I'd never get a Java answer! Got it - thanks! And, as a bonus, it beat some answers and got the extra swag! ₊₁
RudolfJelin

n==0 can be shortened to n<1 and also n==1 to n<2
AlexRacer

2

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

สูตรคำนวณซ้ำสำหรับปริมาตรของ n-ball: V n = (2πr 2 V n-2 )n

บันทึกเป็นไฟล์แยกต่างหากสคริปต์และทำงานด้วย GHCi กับฟังก์ชั่นสำหรับการทดสอบสำหรับการส่งออกเช่นv show (v 3 4.5)ฉันไม่ได้ทดสอบสิ่งนี้โปรดแจ้งให้เราทราบหากสิ่งนี้ไม่ได้ผล

โปรแกรมเก่าที่มีการประมาณ 6.2832 สำหรับการแทนที่2π (50 ไบต์พร้อมการเยื้องของแท็บ):

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

สามารถใช้กับ GHCi ในโหมดหลายบรรทัด (ใช้:set +mหรือล้อมรอบรหัสระหว่าง:{& :}, สิ่งที่แนบมากับสายของตัวเองจำเป็นต้องใช้ฟังก์ชั่นทดสอบ

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.


51 if using layout directly, 49 if you substitute 2*pi for 6.2832, and 47 if you drop the parentheses around the recursive call: let{v 0 r=1;v 1 r=2*r;v n r=2*pi*r*r*v(n-2)r/n}
wchargin

… แต่การให้คะแนนโดยทั่วไปคือการส่งเป็นไฟล์สคริปต์แยกต่างหาก วางlet{}และแทนที่เซมิโคลอนของฉันด้วย linefeeds เพื่อรับเพียง 42 ไบต์ (โดยไม่ขึ้นบรรทัดใหม่)
wchargin

@WChargin ฉันได้เรียนรู้ Haskell มาทั้ง 2 วันแล้วดังนั้นขอขอบคุณสำหรับพอยน์เตอร์ ฉันทำผิดด้านด้วยความระมัดระวังด้วยวงเล็บเนื่องจากฉันไม่แน่ใจเกี่ยวกับโอเปอเรเตอร์เทียบกับลำดับความสำคัญการเรียกฟังก์ชันใน Haskell
Tamoghna Chowdhury

2

แร็กเก็ต 69 ไบต์ (บวกพวงหรีดพิเศษ)

ใช้สูตรแบบเรียกซ้ำจากhttps://en.wikipedia.org/w/index.php?title=Volume_of_an_n-ball§ion=3#Recursions

รวมถึงข้อเสนอแนะโดย @wchargin

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

Ungolfed (v = ปริมาณ, d = มิติ, r = รัศมี):

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

การทดสอบ:

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

เอาท์พุท:

2
28.274333882308138
4.1887902047863905
381.7035074111599
18.758
1

I highly doubt that this is legitimate: you're using the recursive function without counting its definition in the byte count. That is, the expression that you're scoring as 67 bytes is not valid Racket, as v is unbound (not to mention the other parameters). Surely you need to count the (define(v d r)) as well? This brings you up to 82 bytes…
wchargin

…but you can shave off four bytes from that by replacing your cond with nested if expressions, bringing you down to 78 bytes with (define(v d r)(if(= d 0)1(if(= d 1)(* 2 r)(*(/(* 2 pi(* r r))d)(v(- d 2)r))))).
wchargin

…and shave off three more by using a match to get (define(v d r)(match d[0 1][1(* 2 r)][_(*(/(* 2 pi(* r r))d)(v(- d 2)r))])).
wchargin

Thanks for great suggestions. I am including these in the answer.
rnso

@wchargin : I could reduce 9 more bytes by repositioning (v (- d 2) r) in formula and by using only "r r" instead of "(* r r)" since it is already in a multiplication formula.
rnso

1

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.


Nice and recursive, and ₊₁ for showing the recursive formula.
RudolfJelin

1

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
}


1

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.



1

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 :(


1

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...


₊₁ for the extra SWAG!
RudolfJelin

1

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.


1
What about shortening "3.141592653589793" to "3.1416", saving 11 bytes and still fitting into the rules?
RudolfJelin

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

2d2πd2rdd!!
{            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
}
โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.