我是Java的初学者,目前正在经历"如何像计算机科学家一样思考"初学者书。我在迭代章节中遇到了问题。有谁能指出我正确的方向?
当我使用math.exp时,我得到的答案与我的代码获得的答案完全不同。 请注意,它不是作业。
以下是问题:
计算e x 的一种方法是使用无限级数展开 e x = 1 + x + x 2 / 2! + x 3 / 3! + x 4 / 4! +
...
如果循环变量名为i
,那么第i个术语是xi
/i
!。
- 编写一个名为
醇>myexp
的方法,该方法会添加第一个n
项 系列。
所以这里是代码:
public class InfiniteExpansion {
public static void main(String[] args){
Scanner infinite = new Scanner(System.in);
System.out.println("what is the value of X?");
double x = infinite.nextDouble();
System.out.println("what is the power?");
int power = infinite.nextInt();
System.out.println(Math.exp(power));//for comparison
System.out.println("the final value of series is: "+myExp(x, power));
}
public static double myExp(double myX, double myPower){
double firstResult = myX;
double denom = 1;
double sum =myX;
for(int count =1;count<myPower;count++){
firstResult = firstResult*myX;//handles the numerator
denom = denom*(denom+1);//handles the denominator
firstResult = firstResult/denom;//handles the segment
sum =sum+firstResult;// adds up the different segments
}
return (sum+1);//gets the final result
}
}
答案 0 :(得分:4)
作业denom = denom*(denom+1)
将提供如下序列:1, 1*2=2, 2*3=6, 6*7=42, 42*43=...
但是你想要denom = denom*count
。
总的来说,我们只想打印从n
开始的第一个1!
阶乘:1!, 2!, 3!, ..., n!
。在k
期限,我们将k-1
期限乘以k
。这将是在前一个术语上递归计算k!
。具体示例:4!
为3!
次4
,6!
为5!
次6
。
在代码中,我们有
var n = 7;
var a = 1;
for (int i = 1; i <= n; i++ ) {
a = a*i; // Here's the recursion mentioned above.
System.out.println(i+'! is '+a);
}
尝试运行以上内容并进行比较,看看运行以下内容后得到了什么:
var n = 7;
var a = 1;
for (int i = 1; i <= n; i++ ) {
a = a*(a+1);
System.out.println('Is '+i+'! equal to '+a+'?');
}
答案 1 :(得分:1)
这里有几个错误:
总结一下,您可以将操作简化为:
firstResult = firstResult * myX / (count+1);
sum += firstResult;
编辑:
- 我运行代码并看到Math.exp(power)打印而不是Math.exp(x)
- 我的第一项是错误的,因为sum被初始化为myX。
答案 2 :(得分:0)
为何让它变得复杂?我尝试了一个解决方案,它看起来像这样:
//One way to calculate ex is to use the infinite series expansion
//ex = 1 + x + x2 /2! + x3/3! + x4/4! +...
//If the loop variable is named i, then the ith term is xi/i!.
//
//Write a method called myexp that adds up the first n terms of this series.
import java.util.Scanner;
public class InfiniteExpansion2 {
public static void main(String[] args) {
Scanner infinite = new Scanner(System.in);
System.out.println("what is the value of X?");
double x = infinite.nextDouble();
System.out.println("what is the value of I?"); // !
int power = infinite.nextInt();
System.out.println(Math.exp(power));//for comparison
System.out.println("the final value of series is: " + myCalc(x, power));
}
public static double fac(double myI) {
if (myI > 1) {
return myI * fac(myI - 1);
} else {
return 1;
}
}
public static double exp(double myX, double myE) {
double result;
if (myE == 0) {
result = 1;
} else {
result = myX;
}
for (int i = 1; i < myE; i++) {
result *= myX;
}
return result;
}
public static double myCalc(double myX, double myI) {
double sum = 0;
for (int i = 0; i <= myI; i++) { // x^0 is 1
sum += (exp(myX, i) / fac(i));
}
return sum;
}
}
如果你想像工程师那样思考,我就这样做:
我希望你喜欢它!
答案 3 :(得分:0)
我尝试了一个解决方案,它看起来像这样:
public class Fact {
public int facto(int n){
if(n==0)
return 1;
else
return n*facto(n-1);
}
}
}
import java.util.Scanner;
public class Ex {
public static void main(String[] args){
Fact myexp=new Fact();
Scanner input=new Scanner(System.in);
int n=1;
double e=1,i=0,x;
int j=1;
System.out.println("Enter n: ");
n=input.nextInt();
System.out.println("Enter x: ");
x=input.nextDouble();
while(j<=n)
{
int a=myexp.facto(j);
double y=Math.pow(x,j)/(double)a;
i=i+y;
++j;
}
e=e+i;
System.out.println("e^x= "+ e);
}
}