How to resolve problem in for loop?

1 visualización (últimos 30 días)
Ammy
Ammy el 11 de Dic. de 2021
Comentada: Ammy el 11 de Dic. de 2021
a=java.math.BigInteger(100)
for i=1:a
a*a;
end
Undefined function '_colonobj' for input arguments of type 'java.math.BigInteger'.
Any help in this regard is much appreciated. Thanks

Respuestas (1)

John D'Errico
John D'Errico el 11 de Dic. de 2021
Editada: John D'Errico el 11 de Dic. de 2021
Is "a" a number, in the form MATLAB will expect it? NO! a is a BigInteger number. You cannot use the colon operator on a.
a=java.math.BigInteger(100)
a = 100
whos a
Name Size Bytes Class Attributes a 1x1 java.math.BigInteger
So you could have used a loop like this:
for i = 1:double(a)
As far as the actual loop goes, What do you think this
a*a;
Operator '*' is not supported for operands of type 'java.math.BigInteger'.
does? NOTHING. Well, it does something. It throws an error. Because again, multiplcation is NOT supported in MATLAB between java.math.BigInteger objects. And even if it was supported, it would do nothing useful in MATLAB, just dumping the result into the bit bucket.
Anyway, if you want to multiply BigInteger numbers, the Java toolbox provides such a tool. Admittedly, it is a bit of a pain in the neck. This is why I put my own wrapper around those tools for use in MATLAB.
  4 comentarios
John D'Errico
John D'Errico el 11 de Dic. de 2021
Editada: John D'Errico el 11 de Dic. de 2021
Lol. You are wasting your time.
Do you understand how big is that number?
log10(2589665665565686656656)
ans = 21.4132
Over two sextillion.
Not much smaller than Avogadro's number. You want to loop THAT many times?
Are you running that loop on a super computer the size of the earth? Lets see. If you can run one billion loops per second, as I recall, there are roughly 31 million seconds in a year. So that will still take on the order of 85000 YEARS to finish the loop.
Are you a bit of an optimist? Or do you just have plenty of time to kill? Maybe you want to re-read the complete works of Shakespeare a few quadrillion times while you wait for it to finish?
Ammy
Ammy el 11 de Dic. de 2021
@John D'Errico thank you very much for explanation I got your point, thank you very much.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by