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.

Recommended Answers

All 5 Replies

ok, so what have you done so far?

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

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);
    }
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

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.

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.