Hello everyone, I’m going to be showing you how to find the

This algorithm can easily be duplicated in a computer program, and the way the numbers are incremented just screams for a for loop:

Explanation of code:

By initializing the variable odd at 3 and adding 2 every iteration, the loop processes through every odd number (3, 5, 7, 9…) and then adds the value stored in odd to numb every iteration as well. The variable i is used to keep track of the amount of iterations.

Output:

This algorithm can be used to find any perfect square.

Enjoy!

*n*th perfect square. A perfect square is a number who has a whole number square root. Example: √1 = 1, because 1 * 1 = 1. √4 = 2, because 2 * 2 = 4. √9 = 3, √16 = 4, √25 = 5, I’m sure all of you know this, just a quick reminder. These are the perfect squares: 1, 4, 9, 16, 25, 36, 49, 64… and it goes on forever. There is a pattern between each one though: the numbers are incremented by every odd number:**1**(+ 3) =**4**(+ 5) =**9**(+ 7) =**16**(+ 9) =**25**(+ 11) =**36**(+ 13) =**49**, etc.This algorithm can easily be duplicated in a computer program, and the way the numbers are incremented just screams for a for loop:

1 |
<span style="color:#274e13;"><span>private</span><span> </span><span>static</span><span> </span><span>void</span><span> perfSquare</span><span>(</span><span>int</span><span> n</span><span>)</span><span> </span><span>{</span><span><br /></span><span>for</span><span> </span><span>(</span><span>int</span><span> numb </span><span>=</span><span> </span><span>1</span><span>,</span><span> odd </span><span>=</span><span> </span><span>3</span><span>,</span><span> i </span><span>=</span><span> </span><span>1</span><span>;</span><span> i </span><span><=</span><span> n</span><span>;</span><span> numb </span><span>+=</span><span> odd</span><span>,</span><span> odd </span><span>+=</span><span> </span><span>2</span><span>,</span><span> i</span><span>++)</span><span> </span><span>{</span><span><br /></span><span>System</span><span>.</span><span>out</span><span>.</span><span>println</span><span>(</span><span>"Perfect Square #"</span><span> </span><span>+</span><span> i </span><span>+</span><span> </span><span>": "</span><span> </span><span>+</span><span> numb</span><span>);</span><span><br /></span><span>}</span><span><br /></span><span>}</span><span><br /></span></span> |

Explanation of code:

By initializing the variable odd at 3 and adding 2 every iteration, the loop processes through every odd number (3, 5, 7, 9…) and then adds the value stored in odd to numb every iteration as well. The variable i is used to keep track of the amount of iterations.

Output:

1 |
<span style="color:#274e13;"><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#1: 1</span><span><br /></span><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#2: 4</span><span><br /></span><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#3: 9</span><span><br /></span><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#4: 16</span><span><br /></span><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#5: 25</span><span><br /></span><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#6: 36</span><span><br /></span><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#7: 49</span><span><br /></span><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#8: 64</span><span><br /></span><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#9: 81</span><span><br /></span><span>Perfect</span><span> </span><span>Square</span><span> </span><span>#10: 100</span><span><br /></span></span> |

This algorithm can be used to find any perfect square.

Enjoy!

Read more: http://forum.codecall.

net/topic/72218-finding-the-nth-perfect-square/#ixzz2GdmpkwpP