#Help!
22 messages · Page 1 of 1 (latest)
Hey, @random tundra!
Please remember to /close this post once your question has been answered!
public class MathSinusFactorial {
public static void main(String[] args) {
int kNum = 12;
double S = 0;
double numerator = 0;
int factorial = 1;
for(int k = 1; k <= kNum ; k++) {
factorial = 1;
numerator = 0;
for(int n = 1; n <= k ; n++) {
factorial = factorial * n;
numerator += Math.sin(k)*n;
}
S += numerator/factorial;
}
System.out.print("k = "+ kNum );
System.out.println();
System.out.print("Sum: "+ S);
}
}
THANKS
Calculate k! every time… You can use k! from previous step to get (k+1)!.
You cannot get k factorial from previous step. What is k factorial at each step you calculate in inside for loop and then divide by that number.
factorial = 1;
for(int k = 1; k <= kNum ; k++) {
factorial *= k;
numerator = 0;
for(int n = 1; n <= k ; n++) {
numerator += Math.sin(k*n);
}
S += numerator/factorial;
}
Faster? I didn't run it.
And u have Math.sin(k)*n, but look like it must be Math.sin(k*n).
But yes, when factorial is outside it is a bit better
computer processor is not so overloaded
no. Sin takes only k Otherwise it should be in parentheses in problem formula
But do as you think it should be
For me sin kn look like sin(k*n) and n sin k look like n*sin(k) or sin(k)*n. And sin kn need parentheses sin(k)n to mean sin(k)*n.
Anyway @random tundra better know what his teacher mean.
result is different
and yes maybe you are right
when it is multiplication then paranthesis is not necessary. Only if k would be added to n
this code worked for my teacher