필터 지우기
필터 지우기

How solve this summation using loop for x=3? but if could solve it without using build in function (factorial)

조회 수: 1 (최근 30일)
  댓글 수: 4
Dyuman Joshi
Dyuman Joshi 2024년 1월 16일
편집: Dyuman Joshi 2024년 1월 16일
Let me push you in the right direction - How can you define factorial of an integer using basic arithmatic operations?
Steven Lord
Steven Lord 2024년 1월 16일
5! is a constant, you could easily compute it before the loop and store it in a variable for use in the loop. Or you could pull it outside the summation and multiply the sum by 5! at the end, since it doesn't depend on n.
Another hint: Let's say you knew x^k/k! for one value of k. How could you use that result to compute x^(k+1)/(k+1)! without recomputing either the exponentiation or the factorial, just using arithmetic?

댓글을 달려면 로그인하십시오.

답변 (1개)

Aditya
Aditya 2024년 1월 16일
Hi Bajdar,
To solve the summation without the built-in factorial function and to make the process more efficient, you can use a for loop strategically. Below is the updated code that leverages previous calculations to reduce computational overhead:
% Initialize variables
y = 0;
x = 3;
factorial_5 = 1; % To store the factorial of 5
% Calculate factorial of 5
for i = 1:5
factorial_5 = factorial_5 * i;
end
% Initialize the first term of the series
term = factorial_5; % As factorial(0) is 1, term for n=0 is factorial(5)*x^0/factorial(0) which is factorial(5)
% Add the first term to the sum
y = y + term;
% Calculate the summation using the previous term
for n = 1:5
term = term * x / n; % This calculates x^n/n! based on the previous term
y = y + term;
end
% Display the result
disp(y);
In this code, we compute 5! once and use it for the initial term. Then, for each subsequent term, we simply multiply the previous term by x/n, which efficiently computes x^n/n! without recalculating the entire factorial or power. This approach ensures that we're not doing redundant calculations, thus optimizing our solution.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by