You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
578 B

2 years ago
package class01;
public class Code02_SumOfFactorial {
public static long f1(int N) {
long ans = 0;
for (int i = 1; i <= N; i++) {
ans += factorial(i);
}
return ans;
}
public static long factorial(int N) {
long ans = 1;
for (int i = 1; i <= N; i++) {
ans *= i;
}
return ans;
}
public static long f2(int N) {
long ans = 0;
long cur = 1;
for (int i = 1; i <= N; i++) {
cur = cur * i;
ans += cur;
}
return ans;
}
public static void main(String[] args) {
int N = 10;
System.out.println(f1(N));
System.out.println(f2(N));
}
}