计算1+1/2!+1/3!+…1/10!
时间: 2023-11-17 19:04:44 浏览: 36
这道题目是要求计算1+1/2!+1/3!+…1/10!的和。下面是两种不同的Java代码实现方式:
1.使用for循环方式:
```
public class Daoshu{
public static void main(String args[]){
double a=1,b=1,sum=0;
for(c=0,b=1,a=1;a<=10;a++) {
b*=(1.0/a);
sum+=b;
}
System.out.println(" "+sum);
}
}
```
2.使用do-while循环方式:
```
public class Daoshu{
public static void main(String args[]){
double a=1,b=1,sum=0;
do{
sum=b+c;
a++;
b=(1.0/a)*b;
} while(a<=10);
System.out.println(" "+sum);
}
}
```
相关问题
C语言调用n的阶乘计算1+1/(1+4!)+1/(1+4!+7!)+......+1/(1+4!+7!+19!)
以下是C语言计算1+1/(1+4!)+1/(1+4!+7!)+......+1/(1+4!+7!+19!)的代码实现:
```c
#include <stdio.h>
int main()
{
int n = 10; // n的值为10
int i, j, k, sum = 0, fac = 1;
double result = 0;
for (i = 1; i <= n; i++) {
fac *= i; // 计算i的阶乘
sum += fac; // 计算1! + 2! + ... + n!
if (i % 3 == 1) { // 每隔3个数计算一次结果
result += 1.0 / (1 + sum); // 计算当前结果
}
}
printf("结果为:%lf\n", result);
return 0;
}
```
计算1/1+1/2!+1/3!+1/6!+1/7!+1/8!+1/9!+1/12!+1/13!+1/14!+1/15!
这是一个级数求和的问题,其中每一项都是分数,分母是一个阶乘。我们可以将这个级数分成几个部分,每个部分的分母都是某个数的阶乘。具体来说,我们可以将这个级数分成以下几个部分:
1. 1/1
2. 1/2!
3. 1/3!
4. 1/6!
5. 1/7!
6. 1/8!
7. 1/9!
8. 1/12!
9. 1/13!
10. 1/14!
11. 1/15!
其中,1/1是第一项,1/2!到1/9!是第二部分,1/12!到1/15!是第三部分。
我们可以使用代码来计算这个级数的和,具体代码如下(使用Python语言):
```python
import math
sum = 1 + 1/math.factorial(2) + 1/math.factorial(3) + 1/math.factorial(6) + 1/math.factorial(7) + 1/math.factorial(8) + 1/math.factorial(9) + 1/math.factorial(12) + 1/math.factorial(13) + 1/math.factorial(14) + 1/math.factorial(15)
print(sum)
```
运行这段代码,我们可以得到这个级数的和约为1.7182818284590455。