square-root

Binary Search to Compute Square root (Java)

雨燕双飞 提交于 2019-12-02 23:16:39
I need help writing a program that uses binary search to recursively compute a square root (rounded down to the nearest integer) of an input non-negative integer. This is what I have so far: import java.util.Scanner; public class Sqrt { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Enter A Valid Integer: "); int value = console.nextInt(); calculateSquareRoot(value); } public static int calculateSquareRoot(int value) { while (value > 0) { double sqrt = (int) Math.sqrt(value); System.out.println(sqrt); } return -1; } } The fact that it has

Shortest way to check perfect Square? [duplicate]

只谈情不闲聊 提交于 2019-12-02 22:08:41
This question already has answers here : What's a good algorithm to determine if an input is a perfect square? [duplicate] (3 answers) Possible Duplicate: What's a good algorithm to determine if an input is a perfect square? I want Shortest and Simplest way to Check a number is perfect square in C# Some of Perfect Squares: 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, ...... Probably checking if the square root of the number has any decimal part, or if it is a whole number. Implementationwise, I would consider something like this: double result = Math.Sqrt(numberToCheck); bool isSquare = result%1 == 0

inverse square root in Python [closed]

别说谁变了你拦得住时间么 提交于 2019-12-02 00:29:31
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . Does any Python library offer a function that implements the "fast inverse square root" algorithm described in following link? http:/

Square root of a 100 digit number in C++

喜欢而已 提交于 2019-12-01 20:45:44
问题 'unsigned long long' can solve upto 15 digits. Is there a way to find square-root of a 100 digit number ? 回答1: You could also use Boost.Multiprecision library. This library provides wrappers for some popular multiprecision implementations. #include <iostream> #include <string> #include <utility> #include <boost/multiprecision/mpfr.hpp> int main() { std::string s(100, '0'); s.at(0) = '1'; boost::multiprecision::mpfr_float_100 f(std::move(s)); boost::multiprecision::mpfr_float_100 sqrt = boost:

Square root of a 100 digit number in C++

家住魔仙堡 提交于 2019-12-01 18:52:18
'unsigned long long' can solve upto 15 digits. Is there a way to find square-root of a 100 digit number ? You could also use Boost.Multiprecision library. This library provides wrappers for some popular multiprecision implementations. #include <iostream> #include <string> #include <utility> #include <boost/multiprecision/mpfr.hpp> int main() { std::string s(100, '0'); s.at(0) = '1'; boost::multiprecision::mpfr_float_100 f(std::move(s)); boost::multiprecision::mpfr_float_100 sqrt = boost::multiprecision::sqrt(f); std::cout << sqrt.str() << std::endl; return 0; } Definitely. One easy way would

Square root in C using Newton-Raphson method

点点圈 提交于 2019-12-01 10:57:15
问题 In the following code, I want to replace the termination condition to: if the ratio of guess square and x is close to 1, while loop should terminate. I tried various expressions, but none run the code properly. any suggestion? # include<stdio.h> float absolute(float x) { if (x < 0) x = -x; return x; } float square(float x) { float guess = 1; while(absolute(guess*guess - x) >= 0.0001 ) guess = ((x/guess) + guess) / 2; return guess; } int main(void) { printf("square root of 2 is %f\n", square(2

IEEE double such that sqrt(x*x) ≠ x

那年仲夏 提交于 2019-12-01 03:00:26
Does there exist an IEEE double x>0 such that sqrt(x*x) ≠ x , under the condition that the computation x*x does not overflow or underflow to Inf , 0 , or a denormal number? This is given that sqrt returns the nearest representable result, and so does x*x (both as mandated by the IEEE standard, "square root operation be calculated as if in infinite precision, and then rounded to one of the two nearest floating-point numbers of the specified precision that surround the infinitely precise result"). Under the assumption that if such doubles would exist, then there are probably examples close to 1,

Why does **0.5 appear to be more efficient than sqrt() [closed]

醉酒当歌 提交于 2019-11-29 12:34:37
I have tried measuring the speed of these two ways for taking square root: > system.time(expr = replicate(10000, 1:10000 ** (1/2))) ## user system elapsed ## 0.027 0.001 0.028 > system.time(expr = replicate(10000, sqrt(1:10000))) ## user system elapsed ## 3.722 0.665 4.494 If the sqrt() function cannot compete with ** 0.5 , why do we need such a function? (system is OS X Yusemite, and R version is 3.1.2) You forgot important parentheses. Here are the timings after correcting that: system.time(expr = replicate(10000, (1:10000) ** (1/2))) #user system elapsed #4.76 0.32 5.12 system.time(expr =

square root character/symbol

我是研究僧i 提交于 2019-11-29 12:09:13
I was wondering what the character code of the square root symbol is in java? That is, I want to be able to print a square root sign on screen inside a string of other characters, or as the label on a button. Take a look at the Unicode character charts at http://www.unicode.org/charts/ See also http://www.google.com/search?q=square+root+character - you might find what you're looking for as the very first hit... DaMainBoss I am sure it's \u221A . Try it out or try googling it, and you might end up with something else (I mean a list of others). EDIT: This would be helpful --> https://www

Calculate Nth root with integer arithmetic

耗尽温柔 提交于 2019-11-29 05:57:50
There are a couple of ways to find integer square roots using only integer arithmetic. For example this one . It makes for interesting reading and also a very interesting theory, particularly for my generation where such techniques aren't so useful any more. The main thing is that it can't use floating point arithmetic, so that rules out newtons method and it's derivations. The only other way I know of to find roots is binomial expansion, but that also requires floating point arithmetic. What techniques/algorithms are there for computing integral nth roots using only integer arithmetic? Edit: