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. הנח כי 0 <= n <= 20.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.