Created
January 4, 2018 07:56
-
-
Save isabolic/f2658f3c40dad858250fcd1b6feb3e37 to your computer and use it in GitHub Desktop.
Build a pile of Cubes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export class G964 { | |
| public static findNb(m: number): number { | |
| // create an endless loop that increments n, the cube number, starting with a value of 1 | |
| for ( var n = 0;;n++ ) { | |
| if ( m > 0 ) { | |
| // if m, the total volume, is not 0, we will subtract the volume of the current cube from it | |
| // first calculate the volume of the current cube | |
| let currCubeVol = Math.pow( n+1, 3); | |
| // subtract the current cube volume from the total volume | |
| m = m - currCubeVol; | |
| } else if ( m == 0 ) { | |
| // if m is zero then we've found our n, so return n | |
| return n; | |
| } else if ( m < 0 ) { | |
| // if we've gone below zero there is no such n! | |
| return (-1); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment