Algorithm to calculate root (or power) in computer

In summary, the individual is looking for a mathematical algorithm to calculate the nth root or power of any given real number, where n can be either an integer or a fraction. They have come across the Newton's method, but it requires calculating power, which they cannot use. They are now seeking another algorithm, preferably in C++ without using any external libraries. They mention wanting to implement their own power() method and are open to the idea of using e and ln functions. They also mention the need to consider the case of negative numbers resulting in complex numbers.
  • #1
hkBattousai
64
0
I need an algorithm to calculate nth root or power of any given real number. "n" can be either integer or fractional, and is real.

I found http://en.wikipedia.org/wiki/N-th_root_algorithm" , but it requires to calculate power in it, therefore I can't use it.

Newton's method:
[itex]x_{k+1} = \frac{1}{n} \left[{(n-1)x_k +\frac{A}{x_k^{n-1}}}\right][/itex]


So, I need another algorithm.
Can you please suggest one?
 
Last edited by a moderator:
Mathematics news on Phys.org
  • #2
If you are writing it in c++, you can include math or math.h depending on your compiler and use the pow() function. Some other languages have similar math libraries.

I suspect your looking for something lower level without functions other than base instructions. This may be of interest to you: http://www.syntax-example.com/Code/calculate-powerab-ie-ab-1670.aspx
 
Last edited by a moderator:
  • #3
fedaykin said:
If you are writing it in c++, you can include math or math.h depending on your compiler and use the pow() function. Some other languages have similar math libraries.

I suspect your looking for something lower level without functions other than base instructions. This may be of interest to you: http://www.syntax-example.com/Code/calculate-powerab-ie-ab-1670.aspx

Yes, I will write in C++ without using any external libraries like pow() or sqrt() of math.h. Your example code is in Assembly language. Rather than a pseudo code, I'm looking for a mathematical method for this calculation.
 
Last edited by a moderator:
  • #4
hkBattousai said:
Yes, I will write in C++ without using any external libraries like pow() or sqrt() of math.h. Your example code is in Assembly language. Rather than a pseudo code, I'm looking for a mathematical method for this calculation.

Can't you just implement pow(x,n) ?
 
  • #5
Jarle said:
Can't you just implement pow(x,n) ?

Actually, I'm more concerned about the mathematical aspect of the problem more than computer programming. I want to implement my own power() method.
 
  • #6
hkBattousai said:
I want to implement my own power() method.

That's what I meant. for example to define x^k = pow(x,k) (excuse my java syntax)

double a = x;
for(int i = 0; i < k; i++) {
a = x * a;
}
return a;
 
  • #7
Recall:

[tex]
x^\alpha = e^{\ln x^\alpha} = e^{\alpha \ln x}
[/tex]

now you just need an exponential and a log. Special cases like square roots can have nice iterations that converge quick (learned in real analysis of all places!), but I'm guessing this is the route you must take for general roots/powers.

Note that even though [tex]x[/tex] and [tex]\alpha[/tex] are real, when [tex]x<0[/tex] the result is complex. Are you including this case?

Good luck,

jason

edit: just realized that your question is for for the less general case of either [tex]\alpha[/tex] or [tex]1/\alpha[/tex] an integer. Jarle has the answer for the integer case. Once you have that, the 1/integer case can be done via the algorithm in your wikipedia page. Sorry if my generalization caused any confusion!
 
Last edited:

1. What is an algorithm to calculate root or power in a computer?

An algorithm is a step-by-step procedure or formula used to solve a problem. In the case of calculating root or power in a computer, an algorithm is a set of instructions that the computer follows to find the desired value.

2. How does a computer calculate roots and powers?

A computer uses a series of mathematical operations, such as multiplication, division, and exponentiation, to calculate roots and powers. It follows the algorithm to determine the solution.

3. What is the most commonly used algorithm for calculating roots and powers in a computer?

The most commonly used algorithm for calculating roots and powers is the Newton's method. This method uses an iterative process to find the root or power of a number by estimating the solution and refining it with each iteration.

4. Can a computer calculate roots and powers for complex numbers?

Yes, a computer can calculate roots and powers for complex numbers using the same algorithms used for real numbers. However, additional operations, such as complex multiplication and division, may be required.

5. Are there any limitations to using algorithms to calculate roots and powers in a computer?

While algorithms can accurately calculate roots and powers, there are limitations in terms of the speed and precision of the calculation. Additionally, some algorithms may not work for certain types of numbers, such as negative numbers or numbers with large exponents.

Similar threads

  • General Math
Replies
21
Views
3K
  • General Math
Replies
6
Views
1K
Replies
16
Views
1K
  • General Math
Replies
9
Views
1K
Replies
5
Views
893
Replies
14
Views
1K
  • General Math
Replies
5
Views
843
Replies
11
Views
2K
Replies
7
Views
1K
Replies
4
Views
410
Back
Top