• Post Reply Bookmark Topic Watch Topic
  • New Topic

Calculating Sqrt of n (using if statement)  RSS feed

 
Z Howard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand why you're trying to use recursion here.
 
Z Howard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is my edited version?
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The goal of the method is not clear. Please explain exactly what has to be achieved.
 
Z Howard
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Help pleasee?


 
Carey Brown
Saloon Keeper
Posts: 3322
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the Math.pow method to raise a number to a power
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to write your own code to find the square root, then I would suggest using the divide and average algorithm
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Please post exact requirements of the task (given by instructor/written in book) without adding your own interpretations. I suspect you misread your task.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Christopher Sheridan
Ranch Hand
Posts: 50
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is a useful link
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!