#Find the square root results of x then identify whether it was prime number or not.
24 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
I need someone to see where I'm wrong at. I got the same output, when I submit, it said I was wrong at test case #2
!f
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
int a, n, b, c;
c = 0;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a;
while (c > -1) {
sqrt(a) == b;
a--;
if (b % a == 0) {
c++;
}
if (c > 2) {
cout << "NO." << endl;
break;
} else {
cout << "YES." << endl;
break;
}
}
}
return 0;
}
i hate "competitive programming" "coding standards"
sqrt(a) == b; literally does nothing
and that's a pretty poor primality check
Ah, I see
Could u taught me what can I use instead of that? I'm really a beginner on competitive programming
first, separating out the primary check to it's own function will greatly help reason about the code. as will providing variable names longer than one letter.
this isn't a "new to competitive programming" error... this is a new to programming at all error
b = sqrt(a)
which assigns to b the result of the sqrt function call on a
what you did is compare the value of b (which is indeterminate as you never assigned it a value) with the result of sqrt(a). and then do nothing with the result of the comparison.
in order to determine if a number, N, is prime, you only need to check divisibility by (some) numbers from 2 to sqrt(N). because 1 divides every number, it doesn't tell us anything, and if N is divisible by a number greater than sqrt(N), then its other factor is less.
there's more involved methods for optimizing primality checking.
eg noticing that every prime aside from 2 and 3, are ±1 mod 6
or using the sieve of eratosthenes
Me too friend, me too
A few things to note @sharp knot
- Avoid using
#include <bits/stdc++.h>at all costs, nothing is good about that. sqrt(a) == bis pretty muchtrue;orfalse;
it does nothing. If you want to check if that statement is true, you need to put it in anif- I'd recommend you check the magic number in another function (if you've learned about them ofc)
- Do not use endl in a for loop, just use
'\n',std::endlflushes the buffer too. - You could use an array to make this code be more readable.
Ahh, thank you so much!!
Noted, thank you very much for telling me this!
!solved