Z Howard

Greenhorn

Posts: 12

Keith Lynn

Ranch Hand

Posts: 2409

Z Howard

Greenhorn

Posts: 12

posted 2 years ago

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?

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?

posted 2 years ago

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 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?

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Z Howard

Greenhorn

Posts: 12

posted 2 years ago

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

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

posted 2 years ago

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.

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.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

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

posted 2 years ago

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

posted 2 years ago

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.

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

Campbell Ritchie

Marshal

Posts: 56541

172

posted 2 years ago

Please post exact requirements of the task (given by instructor/written in book) without adding your own interpretations. I suspect you misread your task.

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.

posted 2 years ago

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.

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

Z Howard

Greenhorn

Posts: 12

posted 2 years ago

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.

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.

posted 2 years ago

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

I'm afraid not.Z Howard wrote:I have worked out a method to calculate the square root and 2^n

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 required to implement these methods myself.

Don't know. You didn't tell exact requirements.Z Howard wrote:Im sure some of this doesn't look right. Is it because ive misplaced Math.sqrt?

You have been told that too. Do one thing at a time.Z Howard wrote:I want to print math.pow and math.sqrt separate.

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

posted 2 years ago

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. :-)

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

Hopefully you know how to calculate derivatives. :-)

“Give a man a program, frustrate him for a day.

Teach a man to program, frustrate him for a lifetime.”