square-root

Determining if a BigInteger is Prime in Java

旧时模样 提交于 2019-12-25 07:21:15
问题 I am trying hands on validation of whether a BigInteger number entered is a Prime Number or not! But, it is running fine for smaller numbers like 13,31 ,but it yields error in the case of 15 ;by declaring it as a Prime. I am unable to figure-out the mistake,probably it is hidden in the squareroot() method approach involving binary-search ! Please view the code and help me point out the mistake!!! Calling code :- boolean p=prime(BigInteger.valueOf(15)); System.out.println("P="+p); Called code

What is the fastest way to find integer square root using bit shifts?

只愿长相守 提交于 2019-12-23 09:01:14
问题 I was looking for the fastest method to calculate the square root(integer) of a number(integer). I came across this solution in wikipedia which finds the square root of a number(if its a perfect square) or the square root of its nearest lower perfect square (if the given number is not a perfect square: short isqrt(short num) { short res = 0; short bit = 1 << 14; // The second-to-top bit is set: 1L<<30 for long // "bit" starts at the highest power of four <= the argument. while (bit > num) bit

An efficient algorithm to calculate the integer square root (isqrt) of arbitrarily large integers

﹥>﹥吖頭↗ 提交于 2019-12-21 05:05:10
问题 Notice For a solution in Erlang or C / C++ , go to Trial 4 below. Wikipedia Articles Integer square root The definition of "integer square root" could be found here Methods of computing square roots An algorithm that does "bit magic" could be found here [ Trial 1 : Using Library Function ] Code isqrt(N) when erlang:is_integer(N), N >= 0 -> erlang:trunc(math:sqrt(N)). Problem This implementation uses the sqrt() function from the C library, so it does not work with arbitrarily large integers

Binary Search to Compute Square root (Java)

不问归期 提交于 2019-12-20 10:35:21
问题 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) {

Calculate Nth root with integer arithmetic

笑着哭i 提交于 2019-12-18 04:40:20
问题 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

What is an efficient algorithm to find all the factors of an integer?

China☆狼群 提交于 2019-12-14 03:55:53
问题 I was writing a very simple program to examine if a number could divide another number evenly: // use the divider squared to reduce iterations for(divider = 2; (divider * divider) <= number; divider++) if(number % divider == 0) print("%d can divided by %d\n", number, divider); Now I was curious if the task could be done by finding the square root of number and compare it to divider. However, it seems that sqrt() isn't really able to boost the efficiency. How was sqrt() handled in C and how

How to Print Out the Square Root of A Negative Number with i Displayed in C

别说谁变了你拦得住时间么 提交于 2019-12-14 03:34:45
问题 I am trying to figure out how to display the square root of a number if it happens to be negative (as it is entered by the user), and if so, display it correctly with the "i" displayed as well. When I do the normal sqrt function, the result is always something like -1.#IND. When I tried using the double complex variables, the positive numbers nor the negative numbers would come out clean. Below is my code; the comments are what my goal is. The 4 num variables are entered by the user and can

Program that solves a simple math equation [closed]

寵の児 提交于 2019-12-14 03:32:51
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I'm having trouble understanding the syntax of Java and how to use Java to solve math equations. Below is just an example of a simple equation. I want the program to simply be able to output the result of the

Error with square root function and powers in Matlab R2014b [closed]

此生再无相见时 提交于 2019-12-14 03:22:21
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 4 years ago . I am carrying out a series of calculations using the sqrt() function. My colleagues and I have noticed that we get different results when using the same inputs. Has anyone encountered this problem before? This is an example: input1 = 4; input2 = 8; result = sqrt(input1^2 + input2^2) Result then

Square Root of Positive-Semidefinite Matrix, worried about bad eigenvalue

孤街醉人 提交于 2019-12-11 15:10:03
问题 I need to take the square root of a real symmetric 10,000 x 10,000 matrix. I thought of using scipy.linalg.sqrtm. I think the matrix is supposed to have 9,999 positive eigenvalues, and 1 zero eigenvalue. Just to be sure, I printed the eigenvalues; one of them came out around -1.05e-12. Probably just rounding error. When I then calculated the matrix square root, I got entries with an imaginary part around e-10. Of course I could just chop those off and proceed with just the real parts (order