0

Not Yet Answered # compute nth root

write a program that computes the nth root of a number. given a real number X greater than 1 and less than -1 and given an integer Y, compute the nth root Z such that Z*Z*Z*Z*Z*Z*Z...(Y times)..Z*Z=X, or Z^y=X(Remember if X is negative Y must be odd.) The user enters values for X and Y and the program calculates Z. Compute Z so that when Z is multiplied Y time it will produce an X which is + or - 0.0000001 of the original value of X. You may only use simple arithmetic to do these computations. That is you may only use multiplication, division, addition, and subtraction. Do it using three methods and implement it 2 ways: one as a static method and one as an instance of the class.

finito 46 Discussion Starter lilpinay siamsoft finito 46 JamesCherrill 2,785 Need some help with this Array. I am trying to get the sum of the even numbers and the sum of the odd numbers using a for each loop. I know the answers to what I am trying to achive are sum of even = 84 and the sum of ...

0

0

ok, so what have you done so far?

post some code I can help you clear the problems, not do your homework for you.

0

```
import java.util.Scanner;
public class computeTenToThe
{
public static void main(String[] args)
{
System.out.println("Enter an integer for z:");
Scanner keyboard = new Scanner(System.in);
int z = keyboard.nextInt( );
System.out.println("Enter an integer for y:");
int y = keyboard.nextInt( );
System.out.println(z*y);
System.out.println(z+y);
System.out.println(z-y);
System.out.println(z/y);
}
```

*Edited 3 Years Ago by mike_2000_17*: Fixed formatting

0

```
public static double nRoot(int n, double num, double epsilon)
{
//if you weren't sure, epsilon is the precision
int ctr = 0;
double root = 1;
if(n <= 0)
return Double.longBitsToDouble(0x7ff8000000000000L);
//0x7ff8000000000000L is the Java constant for NaN (Not-a-Number)
if(num == 0) //this step is just to reduce the needed iterations
return 0;
while((Math.abs(Math.pow(root, n) - num) > epsilon) && (ctr++ < 1000)) //checks if the number is good enough
{
root = ((1.0/n)*(((n-1.0)*root)+(num/Math.pow(root, n-1.0))));
}
return root;
}
```

ust a reminder: this method doesn't allow for n being anything besides a positive (non 0) int

*Edited 3 Years Ago by mike_2000_17*: Fixed formatting

0

`public static double nRoot(int n, double num, double epsilon) { //if you weren't sure, epsilon is the precision int ctr = 0; double root = 1; if(n <= 0) return Double.longBitsToDouble(0x7ff8000000000000L); //0x7ff8000000000000L is the Java constant for NaN (Not-a-Number) if(num == 0) //this step is just to reduce the needed iterations return 0; while((Math.abs(Math.pow(root, n) - num) > epsilon) && (ctr++ < 1000)) //checks if the number is good enough { root = ((1.0/n)*(((n-1.0)*root)+(num/Math.pow(root, n-1.0)))); } return root; } //just a reminder: this method doesn't allow for n being anything besides a positive (non 0) int`

umm?

You may only use simple arithmetic to do these computations. That is you may only use multiplication, division, addition, and subtraction

This isn't very productive

`public static void main(String[] args) { System.out.println("Enter an integer for z:"); Scanner keyboard = new Scanner(System.in); int z = keyboard.nextInt( ); System.out.println("Enter an integer for y:"); int y = keyboard.nextInt( ); System.out.println(z*y); System.out.println(z+y); System.out.println(z-y); System.out.println(z/y); }`

From the question I can't tell if you can use loops, if you can please read up on this:

http://mindprod.com/jgloss/forloop.html

This is part of the solution. If you are confused ask specific questions.

P.S: use code tagging.

*Edited 6 Years Ago by finito*: n/a

0

Instead of the tortuous

```
return Double.longBitsToDouble(0x7ff8000000000000L);
//0x7ff8000000000000L is the Java constant for NaN (Not-a-Number
```

)

why not use the simple and clear

`return Double.NaN;`

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

When I execute this progammatically, I get a table with row heights much larger than when I do this manually.

Note : Sel is the Word.Selection object and the Clipboard contains an Excel Table.

```
public void AddClipboard()
{
Sel.PasteExcelTable(false,false, false);
var t = Sel.Tables[Sel.Tables.Count];
t.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
}
```

the function that I created to find the ...