Project Euler Problem : 48

Standard

The problem statement says

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).

Though seems like it can’t be solved without using bignumber(in c/c++), The bignumber solution takes much more time than we can say moderate for a computer program(same in JAVA). Actually it needs some modular operations. You need the last ten digits and to calculate them you don’t need to calculate all the digits. See my approach

#include <stdio.h>

long long power(int a);

int main()
{
	int i;
	long long ans=0;
	
	for(i=1; i<=1000; i++)
	{
		ans=(ans+power(i))%10000000000000;
	}
	
	ans=(ans%10000000000);
	
	printf("%lld\n",ans);
	
	return 0;
}

long long power(int a)
{
	int i;
	long long ans=1;
	
	for(i=1; i<=a; i++)
	{
		ans*=a;
		ans=ans%10000000000000;
	}
	
	return ans;
}
About these ads

2 thoughts on “Project Euler Problem : 48

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s