Project Euler problem-48 last 10 digits in 1^1 + 2^2 + 3^3 + …….. + 1000^1000?


This is the solution to problem 48 of project euler:

    LANGUAGE USED: PHP


<?php

$str = ‘ ‘;

for($i = 1; $i <= 1000; $i++)
{
$str = bcadd($str, bcpow($i, $i)); // bcpow takes arguments as a string.
}
echo substr($str, -10);  //substr function prints the last 10 values in the substring

?>

Calculating sum of digits in “100!”


C program for calculating sum of digits in 100!

#include<stdio.h>
#define MAX 10000
void factorialof(int);
void multiply(int);
int length = 0;
int fact[MAX];

int main(){
int num;
int i,k ;
long int r, sum=0;

printf(“Enter number : “);
scanf(“%d”,&num);

fact[0]=1;

factorialof(num);
for(i=length;i>=0;i–)
{
sum=sum+fact[i];
}
printf(“sum of digits is: %d”, sum );
return 0;
}
void factorialof(int num)
{
int i;
for(i=2;i<=num;i++)
{
multiply(i);
}
}
void multiply(int num)
{
long i,r=0;
int arr[MAX];
for(i=0;i<=length;i++)
{
arr[i]=fact[i];
}

for(i=0;i<=length;i++){
fact[i] = (arr[i]*num + r)%10;
r = (arr[i]*num + r)/10;

}
if(r!=0){
while(r!=0){
fact[i]=r%10;
r= r/10;
i++;
}
}
length = i-1;
}