Factorial (mind the size) · แฟกทอเรียล (ระวังขนาด)
Factorial (mind the size)
n! is 1 × 2 × … × n, and 0! is 1. It grows fast — 20! is over two billion billion — so the method returns a long, not an int, or the answer would overflow. Multiply from 2 up to n.
ฟักทอเรียล (ระวังเรื่องขนาด)
n! คือ 1 × 2 × … × n และ 0! คือ 1 มันเติบโต เร็วมาก — 20! มีค่าเกินสองพันล้านล้าน — ดังนั้นวิธีการนี้จะคืนค่าเป็น long ไม่ใช่ int มิฉะนั้นคำตอบจะเกินขอบเขต (overflow) ให้คูณตั้งแต่ 2 ขึ้นไปจนถึง n
Complete factorial(int n) returning n! as a long (so large values fit). factorial(0) is 1. Assume 0 <= n <= 20. · เติม factorial(int n) กลับค่า n! เป็น long (เพื่อให้ค่าขนาดใหญ่พอดี) factorial(0) คือ 1 Assume 0 <= n <= 20
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่