Algorithm Analysis
Let . If , then the number is a perfect square.
Example
Let . Then . We check: , therefore is not a perfect square.
Algorithm Implementation
Read a real number .
scanf("%lf", &n);
Calculate .
a = (int)sqrt(n);
If , then the number is a perfect square.
if (a * a == n) printf("%d\n", a); else puts("No");
Java Implementation
import java.util.*; class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); double n = con.nextDouble(); int a = (int)Math.sqrt(n); if (a * a == n) System.out.println(a); else System.out.println("No"); con.close(); } }
Python Implementation
Read a real number .
import math n = float(input())
Calculate .
a = int(math.sqrt(n))
If , then the number is a perfect square.
if a * a == n: print(a) else: print("No")