cube root of a perfect cube

This commit is contained in:
ericdouglas 2015-06-01 12:20:24 -03:00
parent 7445229287
commit f0711c0b7b

View File

@ -53,39 +53,74 @@ var prompt = require( 'prompt' );
// });
// Find the lowest among three numbers
// // Find the lowest among three numbers
// prompt.start();
// prompt.get([
// {
// name : 'x',
// description : 'Enter x'
// },
// {
// name : 'y',
// description : 'Enter y'
// },
// {
// name : 'z',
// description : 'Enter z'
// }
// ], function( err, results ) {
// if ( results.x < results.y ) {
// if ( results.x < results.z ) {
// console.log( 'x is least' );
// } else {
// console.log( 'z is least' );
// }
// } else if ( results.y < results.z ) {
// console.log( 'y is least' );
// } else {
// console.log( 'z is least' );
// }
// });
// Find the cube root of a perfect cube
prompt.start();
prompt.get([
{
name : 'x',
description : 'Enter x'
},
{
name : 'y',
description : 'Enter y'
},
{
name : 'z',
description : 'Enter z'
name : 'x',
description : 'Enter a interger'
}
], function( err, results ) {
if ( results.x < results.y ) {
var x = parseInt( results.x, 10 );
var ans = 0;
if ( results.x < results.z ) {
console.log( 'x is least' );
} else {
console.log( 'z is least' );
}
while ( Math.pow( ans, 3 ) < Math.abs( x )) {
} else if ( results.y < results.z ) {
console.log( 'y is least' );
} else {
console.log( 'z is least' );
ans += 1;
console.log( 'Current guess:', ans );
}
});
if ( Math.pow( ans, 3 ) !== Math.abs( x )) {
console.log( x, 'is not a perfect cube' );
} else {
if ( x < 0 ) {
ans = -ans;
}
console.log( 'Cube root of ' + x.toString() + ' is ' + ans.toString());
}
});