#Find the square root results of x then identify whether it was prime number or not.

24 messages · Page 1 of 1 (latest)

sharp knot
#

#include<iostream>
#include<bits/stdc++.h>
#include<cmath>
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;
}

chilly kernelBOT
#

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.

sharp knot
chilly kernelBOT
#
#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;
}
Steffie
celest portal
#

i hate "competitive programming" "coding standards"

#

sqrt(a) == b; literally does nothing

#

and that's a pretty poor primality check

sharp knot
sharp knot
celest portal
#

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.

celest portal
#

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

supple hull
supple hull
# chilly kernel ```cpp #include <bits/stdc++.h> #include <cmath> #include <iostream> using names...

A few things to note @sharp knot

  1. Avoid using #include <bits/stdc++.h> at all costs, nothing is good about that.
  2. sqrt(a) == b is pretty much true; or false;
    it does nothing. If you want to check if that statement is true, you need to put it in an if
  3. I'd recommend you check the magic number in another function (if you've learned about them ofc)
  4. Do not use endl in a for loop, just use '\n', std::endl flushes the buffer too.
  5. You could use an array to make this code be more readable.
sharp knot
#

!solved