What's wrong in it?

3 Ansichten (letzte 30 Tage)
SAYANTAN BHANJA
SAYANTAN BHANJA am 25 Jul. 2017
Kommentiert: SAYANTAN BHANJA am 26 Jul. 2017
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 Kommentare
per isakson
per isakson am 25 Jul. 2017
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 am 26 Jul. 2017
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 26 Jul. 2017
Change your x and r to
x=uint64(1:1:n);
r=uint64(1);
  1 Kommentar
SAYANTAN BHANJA
SAYANTAN BHANJA am 26 Jul. 2017
Thanks a lot sir..... It works perfectly now

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by