[英]Power and factorial series sum Explanation
Question: 题:
A class SeriesSum is designed to calculate the sum of the following series: SeriesSum类旨在计算以下系列的总和:
Class name : SeriesSum 类名:SeriesSum
Data members/instance variables: 数据成员/实例变量:
x : to store an integer number x:存储整数
n : to store number of terms n:存储项数
sum : double variable to store the sum of the series sum:用于存储序列和的双精度变量
Member functions: 成员功能:
SeriesSum(int xx, int nn) : constructor to assign x=xx and n=nn SeriesSum(int xx,int nn):构造函数以分配x = xx和n = nn
double findfact(int m) to return the factorial of m using recursive technique. double findfact(int m)使用递归技术返回m的阶乘。
double findpower(int x, int y) : to return x raised to the power of y using recursive technique. double findpower(int x,int y):使用递归技术将x提升为y的幂。
void calculate( ) : to calculate the sum of the series by invoking the recursive functions respectively void compute():分别通过调用递归函数来计算序列之和
void display( ) : to display the sum of the series void display():显示序列之和
(a) Specify the class SeriesSum, giving details of the constructor(int, int), double findfact(int), double findpower(int, int), void calculate( ) and void display( ). (a)指定类SeriesSum,详细说明构造函数(int,int),double findfact(int),double findpower(int,int),void compute()和void display()。
Define the main( ) function to create an object and call the functions accordingly to enable the task. 定义main()函数以创建对象,并相应地调用函数以启用任务。
Code: 码:
class SeriesSum
{
int x,n;
double sum;
SeriesSum(int xx,int nn)
{ x=xx;
n=nn;
sum=0.0;
}
double findfact(int a)
{ return (a<2)? 1:a*findfact(a-1);
}
double findpower(int a, int b)
{ return (b==0)? 1:a*findpower(a,b-1);
}
void calculate()
{ for(int i=2;i<=n;i+=2)
sum += findpower(x,i)/findfact(i-1);
}
void display()
{ System.out.println("sum="+ sum);
}
static void main()
{ SeriesSum obj = new SeriesSum(3,8);
obj.calculate();
obj.display();
}
}
MyProblem: 我的问题:
I am having problems in understanding that when i= any odd number (Taking an example such as 3 here)then it value that passes through findfact is (i-1)=2 then how am I getting the odd factorials such as 3! 我在理解时遇到问题,当i =任何奇数时(例如此处为3),那么通过findfact传递的值是(i-1)= 2,那么我怎么得到像3这样的奇因子呢!
Any help or guidance would be highly appreciated. 任何帮助或指导将不胜感激。
Optional: 可选的:
If you can somehow explain the recursion taking place in the findpower and findfactorial,it would be of great help. 如果您能以某种方式解释在findpower和findfactor中发生的递归,那将有很大的帮助。
Take a closer look a the loop. 仔细看看循环。
i
starts at 2 and is incremented by 2 every iteration, so it is never odd. i
从2开始,每次迭代增加2,所以它永远不会奇怪。 It corresponds to the successive powers of x
, each of which is divided by the factorial of i -1
(which IS odd). 它对应于
x
的连续幂,每个幂都除以i -1
的阶乘(这是奇数)。
As for the recursion in findfact
, you just need to unwrap the first few calls by hand to see why it works : 至于
findfact
的递归,您只需要手动解开前几个调用即可查看其工作原理:
findfact(a) = a * findfact(a -1)
= a * (a - 1) * findfact(a -2)
= a * (a - 1) * (a - 2) * findfact(a - 3)
...
= a * (a - 1) * (a - 2) * ... * 2 * findfact(1)
= a * (a - 1) * (a - 2) * ... * 2 * 1
= a!*
The same reasoning works with findpower
. 相同的推理也适用于
findpower
。
As a side note, while it may be helpful for teaching purposes, recursion is a terrible idea for computing factorials or powers. 附带说明一下,虽然对于教学目的可能有所帮助,但是递归对于计算阶乘或幂是一个糟糕的主意。
You can simplify the summation and get rid of power and factorial. 您可以简化求和并摆脱能力和阶乘。 Please notice:
请注意:
x * x
x * x
item == x ** (2 * n) / (2 * n - 1)!
item == x ** (2 * n) / (2 * n - 1)!
the next one will be item * x * x / (2 * n) / (2 * n + 1)
. item * x * x / (2 * n) / (2 * n + 1)
。 Implementation: 执行:
private static double sum(double x, int count) {
double item = x * x; // First item
double result = item;
for (int i = 1; i <= count; ++i) {
// Next item from previous
item = item * x * x / (2 * i) / (2 * i +1);
result += item;
}
return result;
}
In the real world, you can notice that 在现实世界中,您会注意到
sinh(x) = x/1! + x**3/3! + x**5/5! + ... + x**(2*n - 1) / (2*n - 1)! + ...
and your serie is nothing but 而你的意甲不过是
x * sinh(x) = x**2/1! + x**4 / 3! + ... + x**(2*n) / (2*n - 1)! + ...
So you can implement 所以你可以实施
private static double sum(double x) {
return x * (Math.exp(x) - Math.exp(-x)) / 2.0;
}
Try to run below code, it will clear all your doubts (i have modified some access specifier and created main method) 尝试运行以下代码,它将清除您的所有疑问(我已经修改了一些访问说明符并创建了main方法)
public class SeriesSum
{
int x,n;
double sum;
SeriesSum(int xx,int nn)
{ x=xx;
n=nn;
sum=0.0;
}
double findfact(int a)
{ return (a<2)? 1:a*findfact(a-1);
}
double findpower(int a, int b)
{ return (b==0)? 1:a*findpower(a,b-1);
}
void calculate()
{
System.out.println("x ="+x);
System.out.println("n ="+n);
for(int i=2;i<=n;i+=2){
System.out.println(x+"^"+i+"/"+(i-1)+"!" +" = " +(findpower(x,i)+"/"+findfact(i-1)) );
//System.out.println(findpower(x,i)+"/"+findfact(i-1));
sum += findpower(x,i)/findfact(i-1);
}
}
void display()
{ System.out.println("sum="+ sum);
}
public static void main(String arg[])
{ SeriesSum obj = new SeriesSum(3,8);
obj.calculate();
obj.display();
}
}
// ----- output ----
x =3
n =8
3^2/1! = 9.0/1.0
3^4/3! = 81.0/6.0
3^6/5! = 729.0/120.0
3^8/7! = 6561.0/5040.0
sum=29.876785714285713
I'm not sure I understand your question correctly, but I try to help you the best I can. 我不确定我是否正确理解了您的问题,但是我会尽力为您提供帮助。
In this code i never will be any odd number 在这段代码中,我永远不会是任何奇数
for(int i=2;i<=n;i+=2)
i will be: 2 , 4 , 6 , 8 and so on because i+=2
我将是:2,4,6,8等,因为
i+=2
The findfact() function in a more readable version: 更具可读性的版本中的findfact()函数:
double findfact(int a){
if(a < 2 ){
return 1;
} else {
return a * findfact(a - 1);
}
}
you can imagine it as a staircase, every call of findfact is a step: 您可以把它想象成一个楼梯,每一次findfact调用都是一个步骤:
We test: if a < 2 then return 1 else we call findfact()
again with a-1
and multiply a
with the result of findfact()
我们测试:如果a <2,然后返回1,否则我们再次用
a-1
调用findfact()
并将a
与findfact()
的结果相乘
The same function without recursion: 没有递归的相同功能:
double findfact(int a){
int sum = 1;
for(int i = a; i > 0; i--){
sum *= i;
}
return sum;
}
Same by the findpower function: if b == 0 then return 1 else call findpower()
with a, b-1
and multiply the return value of findpower()
with a
由findpower功能相同的:若B == 0则返回1,否则调用
findpower()
与a, b-1
和繁殖的返回值findpower()
与a
So the last called findpower() will return 1
(b = 0) 因此,最后一个调用的findpower()将返回
1
(b = 0)
The second last findpower() will return a * 1
(b = 1) 最后一个findpower()将返回
a * 1
(b = 1)
The third last findpower() will return a * a * 1
(b = 2) 最后一个findpower()将返回
a * a * 1
(b = 2)
so you can see findpower(a, 2) = a * a * 1 = a^2 因此您可以看到findpower(a,2)= a * a * 1 = a ^ 2
Hope I could help you 希望我能帮到你
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.