Find closest number to be a square root of another number - brute force

pull/357/head
ericdouglas 2015-06-02 13:05:39 -03:00
parent eb20d59e1d
commit 2956d63a86
1 changed files with 42 additions and 21 deletions

View File

@ -37,34 +37,55 @@ var prompt = require( 'prompt' );
// });
// Find the cube root of a perfect cube
prompt.start();
prompt.get([
{
name : 'x',
description : 'Enter a interger'
}
], function( err, results ) {
// // Find the cube root of a perfect cube
// prompt.start();
// prompt.get([
// {
// name : 'x',
// description : 'Enter a interger'
// }
// ], function( err, results ) {
var x = parseInt( results.x, 10 );
var ans;
// var x = parseInt( results.x, 10 );
// var ans;
for ( ans in range( 0, Math.abs( x ) + 1 )) {
// for ( ans in range( 0, Math.abs( x ) + 1 )) {
if ( Math.pow( ans, 3 ) === Math.abs( x )) {
break;
}
// if ( Math.pow( ans, 3 ) === Math.abs( x )) {
// break;
// }
}
// }
if ( Math.pow( ans, 3 ) !== Math.abs( x )) {
// if ( Math.pow( ans, 3 ) !== Math.abs( x )) {
console.log( x + ' is not a perfect cube' );
// console.log( x + ' is not a perfect cube' );
} else {
// } else {
console.log( 'Cube root of ' + x.toString() + ' is ' + ans.toString());
// console.log( 'Cube root of ' + x.toString() + ' is ' + ans.toString());
}
// }
});
// });
// Find closest number to be a square root of another number
var x = 25;
var epsilon = 0.01;
var numGuesses = 0;
var ans = 0;
while ( Math.abs( Math.pow( ans, 2 ) - x ) >= epsilon && ans <= x ) {
ans += 0.00001;
numGuesses += 1;
}
console.log( 'numGuesses: ' + numGuesses );
if ( Math.abs( Math.pow( ans, 2 ) - x >= epsilon )) {
console.log( 'Failed on square root of ' + x.toString());
} else {
console.log( ans.toString() + ' is close to square root of ' + x.toString());
}