Friday, November 15, 2013

A Power Function


Given the following function and it's results:

f(x,y) = x^y + y^x where x>0 and y>1f(1,2) = 1^2 + 2^1 = 3f(2,3) = 2^3 + 3^2 = 17f(3,4) = 3^4 + 4^3 = 145. . .

Using the above function denoted by f(x,y), and given that the initial values of x = 1 and y = 2, which increase by 1 each time, what are the last 4 digits of the sum of the first 15 function calls.

Language C++:

#include<iostream>
using namespace std;

class power_fun {

          private:
                 long long int sum,last_4_digit;
          public:
                 power_fun () { 
                          sum = 0;
                          last_4_digit = 0;
                 }
                 void show_last_digit() {
                           int x = 1,y = 2,i;
                           for(i = 0 ; i < 15 ; i++) {
                                     sum = sum + function_solver(x,y);
                                     x++;
                                     y++;
                            }
                            cout<<"sum = "<<sum<<endl;
                            last_4_digit = sum % 10000;
                            cout<<"last_4_digit = "<<last_4_digit<<endl;
                   }
                   long long int function_solver(int xx,int yy) { 
                            long long int t1 = 1,t2 = 1;
                            int i;
                            for (i = 1 ; i <= yy ; i++)
                                        t1 = t1*xx;
    
                            for (i = 1 ; i <= xx ; i++)
                                        t2 = t2*yy;
    
                            return(t1+t2);
                   }
};

int main() {
          power_fun obj;
          obj.show_last_digit();
          return(0);
}

Output:

sum = 7910956276398901049
last_4_digit = 1049

No comments: