Square?
Square?
Time limit: 1 second
Memory limit: 256 megabytes
Given an integer \(N\) you must determine if there exists an integer \(x\) such that \(x ^ 2 = N\).
Input
The input consists of a single integer \(N\). \((-10^5 \le N \le 10^5)\)
Output
Print “Yes” if such \(x\) exists and “No” otherwise.
Scoring
In this problem, each test case will be scored separately. Your score for a submission is the sum of scores for all test cases. Your score for the problem is the maximum score across all submissions.
Examples
Input
8
Output
No
Input
9
Output
Yes
Solution
A negative number cannot be a square of an integer. Otherwise, compute \(x=\lfloor\sqrt N\rfloor\) and check whether \(x^2=N\).
This takes \(O(1)\) time and memory.
#include <cmath>
#include <iostream>
using namespace std;
int main() {
long long n;
cin >> n;
if (n < 0) {
cout << "No\n";
return 0;
}
long long x = sqrt(n);
cout << (x * x == n ? "Yes\n" : "No\n");
}