What's wrong in it?

조회 수: 1 (최근 30일)
SAYANTAN BHANJA
SAYANTAN BHANJA 2017년 7월 25일
댓글: SAYANTAN BHANJA 2017년 7월 26일
Write a function called smallest_multiple that returns a uint64, the smallest positive number that is evenly divisible by all of the numbers from 1 to n where n is a positive integer scalar and is the only input argument of the function. If the result would be greater than what can be represented as a uint64, the function returns 0.
function LCM=smallest_multiple(n)
%
x=(1:1:n);
r=1;
i=1;
while(i<=length(x))
r=(lcm(r,x(i)));
i=i+1;
end
if(r>intmax('uint64'))
LCM=uint64(0);
else
LCM=uint64(r);
end
end
GIVING ERROR FOR INPUT 45 .
  댓글 수: 2
per isakson
per isakson 2017년 7월 25일
I cannot reproduce the error on R2016a 64bit.
LCM=smallest_multiple(45)
returns
Warning: Inputs contain values larger than the largest consecutive flint.
Result may be inaccurate.
...
LCM =
0
ES
ES 2017년 7월 26일
What is your error? Is it about memory? What MATLAB version do you use? I have tried your script in R2014B-64 bit. It works well.

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

채택된 답변

Walter Roberson
Walter Roberson 2017년 7월 26일
Change your x and r to
x=uint64(1:1:n);
r=uint64(1);
  댓글 수: 1
SAYANTAN BHANJA
SAYANTAN BHANJA 2017년 7월 26일
Thanks a lot sir..... It works perfectly now

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by