programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Calculating Sqrt of n (using if statement)

Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
Hi guys,

Im trying to create a very basic if/else program that calculates the sqrt of N using 'Math.sqrt' and calculating 2n. So far I have the below:

Can anyone point me in the right direction with what I currently have?

Thanks!

Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
I'm not sure I understand why you're trying to use recursion here.

Z Howard
Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:

This is my edited version?

Marshal
Posts: 8865
637
• Number of slices to send:
Optional 'thank-you' note:
The goal of the method is not clear. Please explain exactly what has to be achieved.

Z Howard
Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:

Liutauras Vilda wrote:The goal of the method is not clear. Please explain exactly what has to be achieved.

Im required to calculate the square root.

I have to calculate the power using a for or while loop to multiply 2 by itself n times. (Use double)
And I have to find the sqrt by using Math.sqrt

Saloon Keeper
Posts: 10741
86
• Number of slices to send:
Optional 'thank-you' note:

Z Howard wrote:

For any positive value of N this will return 1. Is this really what you want?
"2^n" is "2 xor n". This does not raise 2 to the power of N. Is this really what you want?

Z Howard
Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:

Carey Brown wrote:

Z Howard wrote:

For any positive value of N this will return 1. Is this really what you want?
"2^n" is "2 xor n". This does not raise 2 to the power of N. Is this really what you want?

Im not entirely sure but I need to calcuate the square root of a power of 2? So 2^n , but how would I program this

Liutauras Vilda
Marshal
Posts: 8865
637
• Number of slices to send:
Optional 'thank-you' note:

Z Howard wrote:Im not entirely sure but I need to calcuate the square root of a power of 2? So 2^n , but how would I program this

Don't program nothing yet. Try to solve this first on a piece of paper. It doesn't seem you understand yourself those formulas and what you need to calculate.
However, if you decided to write program, rather than understand what you need to do, stick to writing two methods: (1) two to the power of n, (2) square root. Will be less confusion.

Keith Lynn
Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
You can use the Math.pow method to raise a number to a power

Liutauras Vilda
Marshal
Posts: 8865
637
• Number of slices to send:
Optional 'thank-you' note:
Are you able to use existing classes and methods in Java? Or you need to implement those methods yourself (sqrt, pow)?

Z Howard
Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:

Liutauras Vilda wrote:Are you able to use existing classes and methods in Java? Or you need to implement those methods yourself (sqrt, pow)?

Im required to implement these methods myself. To be honest, Im only working with what Ive been given.

Once I write the basic if/else or while program to calculate the sqrt, I am required to analyse the bytecode after from the class file.

Keith Lynn
Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
If you need to write your own code to find the square root, then I would suggest using the divide and average algorithm

Marshal
Posts: 79261
377
• Number of slices to send:
Optional 'thank-you' note:
You need to know the algorithm first before you can write any code. You will have to look it up; search for divide and average, and Newton's approximation. Do a few square roots on paper, then write down how you do it. Then you can easly convert that to code.

Liutauras Vilda
Marshal
Posts: 8865
637
• Number of slices to send:
Optional 'thank-you' note:

Z Howard wrote:I have to calculate the power using a for or while loop to multiply 2 by itself n times. (Use double)
And I have to find the sqrt by using Math.sqrt

Z Howard wrote:Im required to implement these methods myself.

Liutauras Vilda
Marshal
Posts: 8865
637
• Number of slices to send:
Optional 'thank-you' note:
However, by assuming you need to implement "2 to the power of n" method only and then use Math.sqrt for whatever you need later, start by implementing first one first.

2 to the power of n is - 2 * 2 * 2 as many times as n is. Try to construct the method logic by using for or while loop. I personally think "for" loop is better candidate in this case as you'll know exactly how many iterations will be.

Campbell Ritchie
Marshal
Posts: 79261
377
• Number of slices to send:
Optional 'thank-you' note:
There is also a recursive solution. You can even have the recursion running in O(logn) complexity. There is a standard form for such recursions.

Z Howard
Greenhorn
Posts: 12
• Number of slices to send:
Optional 'thank-you' note:
Hi guys,

I have worked out a method to calculate the square root and 2^n, but I was wondering how can I calculate both Math.sqrt AND Math.pow in the same method??

This gives:

Im sure some of this doesn't look right. Is it because ive misplaced Math.sqrt? I want to print math.pow and math.sqrt separate.

Liutauras Vilda
Marshal
Posts: 8865
637
• Number of slices to send:
Optional 'thank-you' note:

Z Howard wrote:I have worked out a method to calculate the square root and 2^n

I'm afraid not.

Z Howard wrote:Im required to implement these methods myself.

You reused existing method Math.pow from standard library. You said you have to implement yourself that one. N number cannot be fixed, in my opinion user suppose to have an ability to specify it. The current implementation also skips zero, where 2^0 is 1.

Z Howard wrote:Im sure some of this doesn't look right. Is it because ive misplaced Math.sqrt?

Don't know. You didn't tell exact requirements.

Z Howard wrote:I want to print math.pow and math.sqrt separate.

You have been told that too. Do one thing at a time.

Very difficult to help you when you ignore everything what has been told you.

Ranch Hand
Posts: 50
• Number of slices to send:
Optional 'thank-you' note:
http://www.cs.wustl.edu/~kjg/CS101_SP97/Notes/SquareRoot/sqrt.html

Read through this. Study it. Do some work on paper.

Hopefully you know how to calculate derivatives. :-)

Campbell Ritchie
Marshal
Posts: 79261
377
• Number of slices to send:
Optional 'thank-you' note: