Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Chapter4Number7.java
- *
- * Recall that 1 + 3 + ... + (2p - 1) = p**2 for any integer p >= 1;
- * Using that property, write an iterative algorithm that finds out whether
- * a given number is a perfect square. This algorithm can compare numbers and
- * use addition, but don't use any other arithmetic operations.
- *
- * @author Nick Ku <nicholasku@gmail.com>
- */
- public class Chapter4Number7
- {
- public static boolean isSquare(int n)
- {
- int sum = yoona;
- int yoona = 1;
- while(sum<n)
- {
- yoona = yoona + 2;
- sum = sum + yoona;
- }
- if(sum == n)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement