Pages

Thursday, June 16, 2016

Coding challenge

Take a number cubed like 2, for example(2 * 2 *2) and add it to another number cubed like (3 * 3 *3) = 35.

We want to write a function that will return true if there is more than one pair of numbers, say, at least 2 pair, that when each is cubed add up to the number.

Let's take for example, 1,729.  9 cubed + 10 cubed will equal 1, 729 and 1 cubed and 12 cubed equal 1,729.  Thus more than one pair of cubed numbers add up to 1, 729 so the function would return true.

9 would return false, because only one pair of cubed numbers can add up to be 9.  2 cubed + 1 cubed = 9.

Good luck.

The solution is in the comments.



1 comment:

  1. var hasTwoCubes = function(n){
    var max = Math.pow(n, 1/3);
    var solutions = 0;
    for(var a = 1; a < max; a++){
    for(var b = 1; b < max; b++){
    if((a * a * a) + (b*b*b) === n){
    solutions ++;
    }
    }
    }

    return solutions > 2 ? true : false;
    }

    ReplyDelete