Advertisement
Cinestra

Chapter4Number7

Jan 30th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.73 KB | None | 0 0
  1. /*
  2. * Chapter4Number7.java
  3. *
  4. * Recall that 1 + 3 + ... + (2p - 1) = p**2 for any integer p >= 1;
  5. * Using that property, write an iterative algorithm that finds out whether
  6. * a given number is a perfect square. This algorithm can compare numbers and
  7. * use addition, but don't use any other arithmetic operations.
  8. *
  9. * @author Nick Ku <nicholasku@gmail.com>
  10. */
  11.  
  12. public class Chapter4Number7
  13. {
  14. public static boolean isSquare(int n)
  15. {
  16. int sum = yoona;
  17. int yoona = 1;
  18. while(sum<n)
  19. {
  20. yoona = yoona + 2;
  21. sum = sum + yoona;
  22. }
  23. if(sum == n)
  24. {
  25. return true;
  26. }
  27. else
  28. {
  29. return false;
  30. }
  31. }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement