Evaluate the N Power Sum of N: 1 ^ 1 + 2 ^ 2 + 3 ^ 3 + ...... + N ^ n

Source: Internet
Author: User

Programming for computing1 ^ 1 + 2 ^ 2 + 3 ^ 3 + ...... + N ^ nWhere N is an arbitrary integer. (Note: consider the case where the result may be out of the long range)

 

Note: This method usesInteger array elementsStores each digit of a large number.

 

 

======================================

 

# Include <iostream>
Using namespace STD;

Const int n = 10000; // defines the maximum number of digits that can be stored, which can be changed as needed

 

/* Addition of large numbers: the final result is placed in the sum array, and the result array stores the K ^ K value sequence.

The rule for storing large numbers in an array is: when reading an array element with a small subscript, the low part of a large number is read backwards, that is, the highest part is read first, the last element that reads the subscript zero */

Void bigadd (int * sum, Int & height_sum, int * result, Int & height_res)
{
Int I, J;
Int tempsum;
Int carry = 0;
For (I = 0, j = 0; I {
Tempsum = sum [I] + result [J] + carry;
Sum [I] = tempsum % 10;
Carry = tempsum/10;
}
While (carry)
{
Tempsum = sum [I] + carry;
Sum [I] = tempsum % 10;
Carry = tempsum/10;
I ++;
}
Height_sum = I;
}

 

Void n_nand (int n)
{
Int sum [N] = {0}; // final result
Sum [0] = 1; // set the percentile bit to 1.
Int height_sum = 1; // The number of digits of the sum.
Int height_res;

// Calculate n-1 times and
For (int K = 1; k <= N; k ++)
{
Int result [N]; // stores the result of each power.
Result [0] = 1;
Height_res = 1; // number of digits of K ^ K
// Calculate k ^ K
For (INT I = 1; I <= K; I ++)
{
Int res = 0;
For (Int J = 0; j {
Int Buf = Result [J] * k + Res;
Result [J] = Buf % 10;
Res = Buf/10;
}
While (RES)
{
Result [height_res ++] = res % 10;
Res/= 10;
}

}
If (k> 1 & K <= N)
Bigadd (sum, height_sum, result, height_res );
}
// Output result
Int I;
For (I = N-1; I> = 0; I --)
If (sum [I]! = 0)
Break;
For (int K = I; k> = 0; k --)
Cout <sum [k];
Cout <Endl <"total length:" <I + 1 <Endl;
}

Int main ()
{
Int N;
Char CH = 'y ';
While ('y' = CH)
{
Cout <"input a number :";
Cin> N;
If (n> 10000 | n <0)
Cout <"an invalid number is entered! ";
Else
N_nand (N );
Cout <"continue? (Y/N ):";
Cin> CH;
}
Return 1;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.