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 件のコメント

José-Luis
José-Luis 2013 年 1 月 2 日
What have you tried so far?
Roger Stafford
Roger Stafford 2013 年 1 月 2 日
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 2013 年 1 月 3 日
編集済み: Vivek 2013 年 1 月 3 日
%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 2013 年 1 月 3 日
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 2013 年 1 月 3 日
編集済み: Jan 2013 年 1 月 3 日
See [EDITED] in my answer below: Avoid calculating x^x explicitly, when you need the last 10 digits only.

サインインしてコメントする。

その他の回答 (2 件)

Jan
Jan 2013 年 1 月 3 日
編集済み: Jan 2013 年 1 月 3 日

1 投票

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 件のコメント

Walter Roberson
Walter Roberson 2013 年 1 月 3 日
FEX 7908 that I linked to does a binary decomposition to minimize the number of multiplications.
Jan
Jan 2013 年 1 月 4 日
@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 2013 年 1 月 4 日
yes walter. but I need to do that upto 1000^1000
Vivek
Vivek 2013 年 1 月 4 日
@Jan: You are correct. I am working in a company and there they training me in matlab
Walter Roberson
Walter Roberson 2013 年 1 月 4 日
binary decomposition of the exponent is a technique that will work fine up to
(2^52)^(2^52)
Jan
Jan 2013 年 1 月 4 日
@vivek: It looks like you got 4 working solutions in this thread. Is your problem solved now?

サインインしてコメントする。

Sean de Wolski
Sean de Wolski 2013 年 1 月 2 日

0 投票

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 件のコメント

Vivek
Vivek 2013 年 1 月 3 日
It is giving a error ??? Undefined function or method 'vpi' for input arguments of type 'double'.
Walter Roberson
Walter Roberson 2013 年 1 月 3 日
Did you download and install the vpi functions from the File Exchange (FEX) submission that Sean linked to?
Vivek
Vivek 2013 年 1 月 3 日
I am sorry walter. Its not possible. because I am working on matlab in my office
Walter Roberson
Walter Roberson 2013 年 1 月 3 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by