You are on the Home/Other Tutorials/Project Euler/Problem 48 page
Google
Web This Site

Project Euler - Problem 48

More about Project Euler.

Problem description

The series, 1^(1) + 2^(2) + 3^(3) + ... + 10^(10) = 10405071317.

Find the last ten digits of the series, 1^(1) + 2^(2) + 3^(3) + ... + 1000^(1000).

 

Solution

The key here is to recognize that we do not need to keep track of any digits beyond the 10 rightmost digits.  So, I made a copy of the LargeMultiply function from the Large Number Arithmetic module and added one line of code that terminated the computations as soon as the PowersOf10 multiplier exceeded 10.  And, of course, I added a routine to use this new function.

Sub Euler48()
    Dim Rslt As String, I As Integer, ProcTime As Single
    ProcTime = Timer
    Rslt = "0"
    For I = 1000 To 1 Step -1
        Rslt = Right(LargeAdd(Rslt, LargePower(I, I)), 10)
        'If I Mod 100 = 0 Then Stop
        Next I
    Debug.Print Rslt, Timer - ProcTime
    End Sub