Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- // Returns whether or not the given N is lower than the target
- function tooLow(N) {
- return (Math.log(N) + 1) / N >= 0.001;
- }
- // Track a lower and upper bound, and squeeze them together
- let lower = 0;
- let upper = 1;
- // Seek an upper bound
- while(tooLow(upper)) {
- // If we aren't there yet, go bigger!
- upper *= 2;
- // Log the new bounds
- console.log(`${lower} < N < ${upper}`);
- }
- // We've found an upper bound!
- console.log("Upper bound found!");
- // Close in on the value
- while(upper - lower > 1) {
- // Make a guess as a new bound
- let guess = Math.round((upper + lower) / 2);
- // Tighten the bounds
- if(tooLow(guess)) lower = guess;
- else upper = guess;
- // Log the new bounds
- console.log(`${lower} < N < ${upper}`);
- }
- // All set!
- console.log("Done!");
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement