8877. Perfect square

 

A positive integer n is given. If n is a perfect square of some positive integer m, print m. Otherwise, print “No”.

 

Input. One positive integer n.

 

Output. Print the required answer.

 

Sample input 1

Sample output 1

25

5

 

 

Sample input 2

Sample output 2

27

No

 

 

SOLUTION

mathematics

 

Algorithm analysis

Let a = . If a * a = n, then n is a perfect square.

 

Example

Let n = 27. Then a =  = 5. Check: 5 * 5 ≠ 27. Therefore, the number 27 is not a perfect square.

 

Algorithm implementation

Read the input number n.

 

scanf("%d", &n);

 

Compute a = .

 

a = (int)sqrt(n);

 

If a * a = n, then the number n 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 the input number n.

 

import math

n = int(input())

 

Compute a = .

 

a = int(math.sqrt(n))

 

If a * a = n, then the number n is a perfect square.

 

if a * a == n: print(a)

else: print("No")