如何找到一系列中所有数字的阶乘之和?
How to find the sum of factorial of all numbers in a series?
我想创建一个程序来求一个数列中所有数字的阶乘之和,直到 20。
我必须在 s = 1 + (1*2) + (1*2*3) + ...(1*2*3...20) 中找到 's'。
我尝试了一个程序,但它不工作。我正在使用 BlueJ IDE.
int a =1;
int s = 0;
for(int i = 1; i <= 10; i++)
{
while (i >0)
{
a = a * i;
i--;
}
s = s+a;
}
System.out.println(s);
编译器没有显示任何错误消息,但是当我 运行 程序时,JVM(Java 虚拟机)继续加载并且输出屏幕没有显示。
您应该在内部循环中使用不同的循环变量名称,并且您还需要使用 long
来存储您的总和。事实上,我会先写一个方法来乘以系列中的一个数字。喜欢,
static long multiplyTo(int n) {
long r = 1L;
for (int i = 2; i <= n; i++) {
r *= i;
}
return r;
}
然后你可以调用它并用一个简单的循环计算你的总和。喜欢,
long sum = 0L;
for (int i = 1; i <= 20; i++) {
sum += multiplyTo(i);
}
System.out.println(sum);
我明白了
2561327494111820313
使用流:
long s = LongStream.rangeClosed(1, 20)
.map(upper -> LongStream.rangeClosed(1, upper)
.reduce(1, (a, b) -> a * b))
.sum();
System.out.println(s);
打印 2561327494111820313
你可以试试这个:
public class Main
{
public static void main (String[]args)
{
int fact = 1;
int sum = 0;
int i, j = 1;
for (i = 1; i <= 20; i++)
{
for (j = 1; j <= i; j++)
{
fact = fact * j;
}
sum += fact;
System.out.println ("sum = " + sum);
fact = 1;
}
}
}
始终给出正确的变量名并尽量避免在不同的地方使用相同的变量,即你在外部和内部循环中使用变量 i 这不是一个好习惯。
我用扫描仪做了同样的程序Class
import java.util.*;
class Sum_Factorial
{
public static void main()
{
Scanner in = new Scanner(System.in);
int i; //Denotes Integer
int n; //Denotes Number
int f=1; //Denotes Factorial
int s=0; //Denotes Sum
System.out.println("Enter the value of N : ");
n=in.nextInt();
for(i=1; i<=n; i++)
{
f=f*i;
s=s+f;
}
System.out.println("Sum of the factorial numbers is "+s);
}
}
我想创建一个程序来求一个数列中所有数字的阶乘之和,直到 20。 我必须在 s = 1 + (1*2) + (1*2*3) + ...(1*2*3...20) 中找到 's'。 我尝试了一个程序,但它不工作。我正在使用 BlueJ IDE.
int a =1;
int s = 0;
for(int i = 1; i <= 10; i++)
{
while (i >0)
{
a = a * i;
i--;
}
s = s+a;
}
System.out.println(s);
编译器没有显示任何错误消息,但是当我 运行 程序时,JVM(Java 虚拟机)继续加载并且输出屏幕没有显示。
您应该在内部循环中使用不同的循环变量名称,并且您还需要使用 long
来存储您的总和。事实上,我会先写一个方法来乘以系列中的一个数字。喜欢,
static long multiplyTo(int n) {
long r = 1L;
for (int i = 2; i <= n; i++) {
r *= i;
}
return r;
}
然后你可以调用它并用一个简单的循环计算你的总和。喜欢,
long sum = 0L;
for (int i = 1; i <= 20; i++) {
sum += multiplyTo(i);
}
System.out.println(sum);
我明白了
2561327494111820313
使用流:
long s = LongStream.rangeClosed(1, 20)
.map(upper -> LongStream.rangeClosed(1, upper)
.reduce(1, (a, b) -> a * b))
.sum();
System.out.println(s);
打印 2561327494111820313
你可以试试这个:
public class Main
{
public static void main (String[]args)
{
int fact = 1;
int sum = 0;
int i, j = 1;
for (i = 1; i <= 20; i++)
{
for (j = 1; j <= i; j++)
{
fact = fact * j;
}
sum += fact;
System.out.println ("sum = " + sum);
fact = 1;
}
}
}
始终给出正确的变量名并尽量避免在不同的地方使用相同的变量,即你在外部和内部循环中使用变量 i 这不是一个好习惯。
我用扫描仪做了同样的程序Class
import java.util.*;
class Sum_Factorial
{
public static void main()
{
Scanner in = new Scanner(System.in);
int i; //Denotes Integer
int n; //Denotes Number
int f=1; //Denotes Factorial
int s=0; //Denotes Sum
System.out.println("Enter the value of N : ");
n=in.nextInt();
for(i=1; i<=n; i++)
{
f=f*i;
s=s+f;
}
System.out.println("Sum of the factorial numbers is "+s);
}
}