find the last ten digits of 1^1 + 2^2 + ... + 1000^1000

how to Create a function to find the last ten digits of 1^1 + 2^2 + ... + 1000^1000 using M-script

5 comentarios

José-Luis
José-Luis el 2 de En. de 2013
What have you tried so far?
Roger Stafford
Roger Stafford el 2 de En. de 2013
If you use the proper method you can compute it numerically. The sixth digit from the right end of the answer is an '8'. That leaves you nine others to discover.
Vivek
Vivek el 3 de En. de 2013
Editada: Vivek el 3 de En. de 2013
%last ten digits of 1^1 + 2^2 + ... + 1000^1000
y=0; b=0;
for x=1:1000
y=y+(x^x);
y=mod(y,10000000000); %to get the last ten digits
end
disp(y)
Walter Roberson
Walter Roberson el 3 de En. de 2013
No, that will not work. x^x can exceed 2^53 and so x^x would have lost digits before being added to y. This starts happening from 14 onwards. And after 142, x^x would be calculated as infinity because it exceeds 1E308
Jan
Jan el 3 de En. de 2013
Editada: Jan el 3 de En. de 2013
See [EDITED] in my answer below: Avoid calculating x^x explicitly, when you need the last 10 digits only.

Iniciar sesión para comentar.

Más respuestas (2)

Jan
Jan el 3 de En. de 2013
Editada: Jan el 3 de En. de 2013
You do not have to calculate i^i explicitly to find the trailing n digits of this number. It is enough to get the trailing n digits of each partial product, which can be achieved by the mod command. The same can be applied for the sum also. Finally 8 lines of very straight Matlab code are enough, and no special toolbox functions are required.
Computing time is 0.025 seconds on my Core2Duo, Matlab2009a/64. The last digit appears 3 times.
[EDITED] As said already, you can get the last 10 digits of x^x without calculating this potentially large value explicitly:
P = 1;
for ix = 1:x
P = mod(P * x, 1e10);
end
Now P contains the last 10 digits of x^x. Ok? The sum can be created equivalently.
Limitation: This fails, when x*1e10 exceeds 2^53.

6 comentarios

Walter Roberson
Walter Roberson el 3 de En. de 2013
FEX 7908 that I linked to does a binary decomposition to minimize the number of multiplications.
Jan
Jan el 4 de En. de 2013
@Walter: I personally like the linked function. I assume that vivek cannot submit a solution of this homework question (at least I'm convinced that this is a homework) based on this function.
8 lines of straight basic Matlab code and 0.025 seconds processing time are a good argument to "keep it simple stupid".
Vivek
Vivek el 4 de En. de 2013
yes walter. but I need to do that upto 1000^1000
Vivek
Vivek el 4 de En. de 2013
@Jan: You are correct. I am working in a company and there they training me in matlab
binary decomposition of the exponent is a technique that will work fine up to
(2^52)^(2^52)
Jan
Jan el 4 de En. de 2013
@vivek: It looks like you got 4 working solutions in this thread. Is your problem solved now?

Iniciar sesión para comentar.

Sean de Wolski
Sean de Wolski el 2 de En. de 2013
This is a good way to jog the brain for the first time in 2013!
last10 = trailingdigit(sum(vpi(1:1000).^vpi(1:1000)),10)

4 comentarios

Vivek
Vivek el 3 de En. de 2013
It is giving a error ??? Undefined function or method 'vpi' for input arguments of type 'double'.
Walter Roberson
Walter Roberson el 3 de En. de 2013
Did you download and install the vpi functions from the File Exchange (FEX) submission that Sean linked to?
Vivek
Vivek el 3 de En. de 2013
I am sorry walter. Its not possible. because I am working on matlab in my office
Walter Roberson
Walter Roberson el 3 de En. de 2013
Then you cannot use Sean's approach. You should, however, be able to look at the source for the two FEX contributions I pointed out, and copy and paste the source into a local file.

Iniciar sesión para comentar.

Categorías

Community Treasure Hunt

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

Start Hunting!

Translated by