### I dont know if my output is right!

Problem:
Given the following function and it's results:

f(x,y) = x^y + y^x where x>0 and y>1

f(1,2) = 1^2 + 2^1 = 3
f(2,3) = 2^3 + 3^2 = 17
f(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.

my code:
 ``1234567891011121314151617181920212223242526272829303132`` `````` #include #include #include using namespace std; main() { double x = 1; double y = 2; double f, fx = 0; int counter; cout << setprecision(0); cout << fixed; for (counter = 1; counter < 16; counter++) { f = pow(x ,y) + pow (y, x); cout << "\n" << counter <<". f(" << x << ", " << y << ") = " << x << "^" << y << " + " << y << "^" << x <<" = " << f; fx = fx + f; x++; y++; } cout <<" \n\n" << fx; cout << "\n\n"; system("pause"); return 0; }``````

output:

1. f(1, 2) = 1^2 + 2^1 = 3
2. f(2, 3) = 2^3 + 3^2 = 17
3. f(3, 4) = 3^4 + 4^3 = 145
4. f(4, 5) = 4^5 + 5^4 = 1649
5. f(5, 6) = 5^6 + 6^5 = 23401
6. f(6, 7) = 6^7 + 7^6 = 397585
7. f(7, 8) = 7^8 + 8^7 = 7861953
8. f(8, 9) = 8^9 + 9^8 = 177264449
9. f(9, 10) = 9^10 + 10^9 = 4486784401
10. f(10, 11) = 10^11 + 11^10 = 125937424601
11. f(11, 12) = 11^12 + 12^11 = 3881436747409
12. f(12, 13) = 12^13 + 13^12 = 130291290501553
13. f(13, 14) = 13^14 + 14^13 = 4731091158953433
14. f(14, 15) = 14^15 + 15^14 = 184761021583202850
15. f(15, 16) = 15^16 + 16^15 = 7721329860319737900

sum = 7910956276398901200

when i type 1200 as the last 4 digits of the sum of the first 15 first calls it says im wrong. :(

HELP!
bump
Who said that you are wrong is right because the last 4 digits of the sum of the first 15 calls of that function are... 1049. bump!
Last edited on
Looks like the type `double` is not precise enough here. (Not enough significant digits). You could try changing it to `long double` instead.

Failing that, use the `unsigned long long` type which is an integer, and you would have to write your own code to calculate the pow() function.
Here is the output of my code:

 ``1234567891011121314151617`` `````` 1. f(1, 2) = 1^2 + 2^1 = 3 2. f(2, 3) = 2^3 + 3^2 = 17 3. f(3, 4) = 3^4 + 4^3 = 145 4. f(4, 5) = 4^5 + 5^4 = 1649 5. f(5, 6) = 5^6 + 6^5 = 23401 6. f(6, 7) = 6^7 + 7^6 = 397585 7. f(7, 8) = 7^8 + 8^7 = 7861953 8. f(8, 9) = 8^9 + 9^8 = 177264449 9. f(9, 10) = 9^10 + 10^9 = 4486784401 10. f(10, 11) = 10^11 + 11^10 = 125937424601 11. f(11, 12) = 11^12 + 12^11 = 3881436747409 12. f(12, 13) = 12^13 + 13^12 = 130291290501553 13. f(13, 14) = 13^14 + 14^13 = 4731091158953433 14. f(14, 15) = 14^15 + 15^14 = 184761021583202849 15. f(15, 16) = 15^16 + 16^15 = 7721329860319737601 The sum of the above 15 values = 7910956276398901049``````

All values ​​were checked with Linux great precision arithmetic bc. A windows port of this bc arithmetic at:
http://gnuwin32.sourceforge.net/packages/bc.htm
> what are the last 4 digits ...

Use modular arithmetic.

 ``123456789101112131415161718192021`` ``````#include constexpr int N = 10000 ; long long pow( int a, int b ) // non-negative b { long long result = 1 ; for( ; b > 0 ; --b ) result *= a ; return result ; } int pow_mod_n( int a, int b ) { return pow(a,b) % N ; } int f_mod_n( int a, int b ) { return ( pow_mod_n(a,b) + pow_mod_n(b,a) ) % N ; } int main() { int sum = 0 ; for( int n = 1 ; n < 16 ; ++n ) sum += f_mod_n( n, n+1 ) ; std::cout << sum % N << '\n' ; }``````

http://coliru.stacked-crooked.com/a/4627339a7db30120
@bits12345

The code which has given the outputs in my above posts:

 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````#include #include long double myPow(int, int); int main() { int x = 1; int y = 2; int counter; long double f; long double fx = 0.L; std::cout << std::setprecision(0); std::cout << std::fixed; for (counter = 1; counter < 16; ++counter) { f = myPow(x, y) + myPow (y, x); std::cout << '\n' << std::setw(3) << counter <<". f(" << x << ", " << y << ") = " << x << "^" << y << " + " << y << "^" << x <<" = " << f; fx = fx + f; ++x; ++y; } std::cout << "\n\nThe sum of the above 15 values = " << fx; std::cout << '\n'; system("pause"); return 0; } long double myPow(int u, int v) { long double f = 1.L; for(int i = 0; i < v; ++i) f *= u; return f; }``````

Hope this helps.
Thanks for the help you guys. I'll try to understand furthermore what you guys have posted. Haha
Topic archived. No new replies allowed.