Finding integers of factorial between two user-input values
Show older comments
Hi all, I'm really frustrated by this problem. I'm supposed to find all integers which n! is between i and j, and I can't use the factorial function in Matlab. My script can only have one loop.
I can't seem to understand this at all.
i = input('Input value for i:');
j = input('Input value for j:');
fact=1;
n=0;
for n=1:n
fact=fact*n;
n=n+1;
if i<fact && j>fact
fprintf('Smallest n where n! is between %d and %d is %d\n',i,j,n)
end
end
What am I doing wrong and how do I fix this?
Answers (1)
Walter Roberson
on 4 Oct 2015
Your code
n=0;
for n=1:n
is equivalent to
for n=1:0
which is going to end immediately.
You are going to need to switch to a "while" loop, and the condition to exit the while is that your factorial has become larger than j.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!