Sunday, 14 January 2018

The POWER sum by Kevinsogo & Anno

THE POWER SUM
Approach :

1.
Kevin has cleverly solved this problem without sweating..... (NO surprise).

int count(int x,int n,int s,int v){
   
    if(s == x)  return 1;
    else{
       v++;
       int res = 0;
           while(s+pow(v,n) <= x){
                 res += count(x,n,s+pow(v,n),v);
                 v++;} //while loop

       return res;
       }


TREE DIAGRAM FOR RECURSION




That's it !!!! :o

2.

int count(int x, int n, int i) {
      x = x - (int) Math.pow(i, n);
      if (x == 0) return 1;
         if (x > 0) {
            int sum = 0;
            for (int j = i + 1; Math.pow(j, n) <= x; j++)
                sum += fn(x, n, j);
            return sum;
                    } 
     else return 0;
    }

TREE DIAGRAM FOR RECURSION

No comments:

Post a Comment