Here’s the problem statement.
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?
The question’s pretty straightforward.
The code which I’ve written can correctly find all the individual numbers(ie (1^1,2^2,…997^997 etc and they’re all correct because I checked using WolframAlpha) 🙂
The glitch occurs when i try to add all these numbers. My program always outputs 0.
I’ve read through it many times and somehow can’t manage to find the error.
PS-Since the numbers here are too large, I’ve stored the individual digits in an array.
Code
#include<stdio.h>
int n[1001][3001]={};
int sum[3001]={};
int raisedto(int q)
{
int i,j;
//int digit;
int carry=0;
int carry1=0;
n[q][0]=q;
for(i=0;i<q-1;i++)
{
for(j=0;j<3001;j++)
{
carry=(q*n[q][j]+carry1)/10;
n[q][j]=((q*n[q][j])+carry1)%10;
carry1=carry;
}
carry1=0;
carry=0;
}
return(0);
}
int sumof()
{
int i,j,carry=0,carry1=0;
for(i=0;i<1001;i=i+2)
{
for(j=0;j<3001;j++)
{
carry=(n[i][j]+n[i+1][j]+carry1)/10;
sum[j]=(n[i][j]+n[i+1][j]+carry1)%10;
carry1=carry;
}
carry1=0;
carry=0;
}
return(0);
}
int main(void)
{
int j,i;
for(i=0;i<1001;i++)
{
printf("%d",i);
raisedto(i);
}
printf("\n");
sumof();
for(j=0;j<3001;j++)
{
printf("%d",sum[j]);
}
printf("done");
return(0);
}
You were correct, your
sumof()function was wrong…This is essentially what you needed.
Do try and solve using the method i suggested, it’ll help you for advanced problems of Euler.
And the answer checks out. 🙂