id/email
password
forgot password | create account
about | help | code help+videos | done | prefs
CodingBat code practice

 

konstans@stuy.edu summerapcs > isPerfectSquare
prev  |  next  |  chance

Write a Java function named isPerfectSquare that takes a positive integer n and returns true if it is a perfect square, and false otherwise.
You must not use Math.sqrt() or any other library method for square roots.

Instead, use the fact that the sum of the first k odd numbers equals k².
Use a loop to subtract consecutive odd numbers (1, 3, 5, 7, …) from n until you either reach 0 (which means it's a perfect square) or go negative (which means it's not).


isPerfectSquare(1) → true
isPerfectSquare(2) → false
isPerfectSquare(3) → false

...Save, Compile, Run (ctrl-enter)

public boolean isPerfectSquare(int n){ }

Editor font size %:
Shorter output


Forget It! -- delete my code for this problem

Progress graphs:
 Your progress graph for this problem
 Random user progress graph for this problem
 Random Epic Progress Graph

Java Help

Misc Code Practice

Copyright Nick Parlante 2017 - privacy