The following is a simple question:
Calculation of loops and Recursion methods 1 ~ The sum of n factorial. n cannot be too large.
Import java. util. optional; public class factorial {public static void main (String args []) {int sum = 0; // int n = 10; Using SC = new using (System. in); System. out. println ("input n:"); int n = SC. nextInt (); for (int I = 1; I <= n; I ++) {int temp = 1; for (int j = 1; j <= I; j ++) temp * = j; sum + = temp;} System. out. println ("cyclic method:" + sum); System. out. println ("Recursive Method:" + getFactorialSum (n) + "");} private static Int getFactorial (int m) {// recursively calculates the factorial if (m = 1) return 1; else return getFactorial (m-1) * m ;} private static int getFactorialSum (int m) {// recursive sum int sum = 0; if (m! = 0) {sum = getFactorial (m) + getFactorialSum (m-1);} return sum ;}}
This article is from the "Keep it simple." blog, please be sure to Keep this source http://shenghao.blog.51cto.com/5890026/1301940