求1到18的阶乘之和
刚刚的程序不准确,可以使用下面的代码进行验证
[quote]
public class Test
{
private static long result = 1;
public static void main(String[] args)
{
long totalResult = 0;
for (int i = 1; i <= 18; i++)
{
long r = factorialN(i);
System.out.println("-----"+i+"的阶乘:---->" + r);
totalResult += r;
result = 1;
}
System.out.println("-----totalResult---->" + totalResult);
}
// 通过递归方式获取整数n的阶乘
public static long factorialN(int n)
{
if (n > 0)
{
result = result * n;
factorialN(n - 1);
}
return result;
}
}
[/quote]
[code="java"] public static void main(String[] args) {
int end=18;
int result=1;
for(int i=2;i<end+1;i++){
result=result+i*i;
}
System.out.println(result);
}[/code]
public class Test
{
private static int result = 1;
public static void main(String[] args)
{
int totalResult = 0;
for (int i = 1; i <= 18; i++)
{
totalResult += factorialN(i);
}
System.out.println("-----totalResult---->" + totalResult);
}
// 通过递归方式获取整数n的阶乘
public static int factorialN(int n)
{
if (n > 0)
{
result = result * n;
factorialN(n - 1);
}
return result;
}
}