• Post Reply Bookmark Topic Watch Topic
  • New Topic

Working on square root method  RSS feed

 
Jay Nino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a method that prints the square root of a number up to a certain number. It needs to take a single int parameter for example "n" , and then print all of the (positive) even perfect squares less than n, each on a separate line. I want the method to be called something like this:

public void Squares(int n) {

}

I need the output to look something like this:

Example: if n = 40, your code should print

4
16
36

So I have been working for a few hours now and am really stuck.

This is what I have so far:

int count = 0;
int n = 4;
int max = n;
while(count < max) {
System.out.println(n);
n = n * n;
count++;

I am still new to loops and really would love to understand how to code this problem. Thanks a bunch!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

That is not a square root.
I think you need to disable a bit of equipment which is causing you difficulty. Your computer. Get a pencil paper and eraser (the latter being the most important) and write down how you would do that without a computer. Write that down in very short words and when you have done that you can probably easily convert that to code.
By the way: Use 2×2 rather than trying to find sophisticated ways to calculate 2². 2×2 is almost certainly faster, and maybe more accurate.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

When you post code, please UseCodeTags (← click). It makes in easier to read.

Putting your code together, I get:



Do you know how to execute code using a main() method? It looks like this:



 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Are you working with square or square root? Your code doing the squaring, which differs from the subject (square root).

Anyway going back to your problem.

I suggest you stop programming and do it with pen and paper

Start off listing what i and square(i) are and whether square(i)<n and whether to display the output.

Using your example n=40
i | square(i) | even squares | square(i)<n
1 | 1 | N | Y
2 | 4 | Y | Y
3 | 9 | N | Y
4 | 16 | Y | Y
5 | 25 | N | Y
6 | 36 | Y | Y
7 | 49 | N | N

Then you can find a pattern to determine the Y/N flags above using i
 
Jay Nino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahh I just realized I think I am doing part of it wrong. It should be getting the square root instead. I tried writing some of it down but I guess I just am not fully understanding it. I am having trouble wrapping my head around how to use loops in general. I have done some basic loop exercises in my class but this problem seems harder than what we have been doing normally.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give us the exact wording of the assignment?
 
Jay Nino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Can you give us the exact wording of the assignment?


Sure thing. Thanks.

The method evenSquares takes a single int parameter, n, (for example, 10000), and then prints all of the (positive) even perfect squares less than n, each on a separate line. Notice that evenSquares has a void return type, since all it does is print integers to the console. Be sure to use the println method to print each entry of your output.

Example: if n = 40, your code should print

4
16
36

(Hint: your method should be built around a for loop with a test component that asks if the square of the control variable (say, k) is < n. Thus, the loop should terminate as soon as k*k equals or exceeds n.)

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That brings us back to what I thought and what you seemed to say when you started.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking about loops when you are figuring out how to approach this exercise is the wrong thing to do. To start with, don't think about how you're going to tell the computer what to do. Think about how to solve the problem by scribbling on paper first.

In other ways, try to explain how to do it in an informal way, and then think of how to implement that.
 
K. Tsang
Bartender
Posts: 3648
16
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Hint: your method should be built around a for loop with a test component that asks if the square of the control variable (say, k) is < n. Thus, the loop should terminate as soon as k*k equals or exceeds n.)


The hint gives you the condition when the loop terminates.

Since you meant to do square root, you in fact can keep using your approach so that you don't need to deal with decimal and doubles, which Math.sqrt() takes.
 
Jay Nino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what I have now. I tried writing it out but still cant get the question right. Thanks for the help everyone.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to decide what should be up to the maximum of 40. What is count? Is that what you want to go up to 40?
 
Jay Nino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im honestly not sure, in the example they have N as 40 but mine prints 40 times not up to the number 40.
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Jay,

I guess it is indeed confusing. But have a look at K. Tsangs first reply (and
his second as well). You see a scheme in that reply that is exactly what
you are supposed to do. Have a look at it again if necessary, and tell us
your thoughts.

Greetz,
Piet
 
Jay Nino
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help everyone. My apologies for being a bit slow understanding. This assignment was for an online class and was due today. Although I missed the due date and didn't get credit, I still am curious about how to solve this. Thanks again everyone. Writing it down helped with my thoughts but I still wasn't able to wrap my mind around it for some reason.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are three fundamental things to know when you are writing a loop
- Where do you start
- Where do you stop
- How do you get to the next item in the sequence. Often this involves incrementing a counter of some sort


One trick to programming is to start small and then add things to it. Don't go for the big bang all in one go - it very rarely works.
- Write a program to print out the first ten square numbers using a loop
- enhance your program so that it stops printing when the square numbers go above an upper limit. (Where do you stop the loop)
- enhance your program so that it only prints 'even' square numbers. (How do you get to the next item)

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are still working at this assignment I would suggest you delete what you have written, go back to the question and start again. But with a piece of paper and your computer turned off.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!