Home c Function checking a simple number

# Function checking a simple number

Author

Date

Category

Need to check whether the number is simple using the function.
I decided that way, but it works not to the end correctly. I can not catch the reason …

``````int prime (unsigned int num {
For (int i = 2; I * i & lt; = num; i ++) {
if (num% i == 0) {
Return 0;
}
}
Return 1;
}
``````

``````bool is_prime (unsigned p) {
If (P & LT; 2) Return False;
if (p == 2) Return True;
if (p% 2 == 0) Return False;
Double Limit = SQRT (P);
For (unsigned i = 3; i & lt; = limit; i + = 2) {
if ((p% i) == 0) Return False;
}
RETURN TRUE;
}
``````

Small optimization. First, check the `num `equal to 2 (simple), less than 2 and num even number (not simple). Then you can only sore odd:

``````uint32_t i, lim = sqrt (num) + 1;
for (i = 3; i & lt; lim; i + = 2)
if (num% i == 0)
Return 0;
Return 1;
``````

looked asm. code. Despite -o3 a little better:

``````uint32_t i, lim = ((uint32_t) SQRT (NUM)) + 1;
``````

@vladd , it’s not for this task. The sieve is the search all simple (specified them the number of ). Here you need to check only one number.

Here’s the solution on C++. You want to rewrite onto a Source: Akina Reshelo

``````# include & lt; iostream & gt;
#Include & lt; vector & gt;
Using Namespace STD;
BOOL IS_PRIME (unsigned int num) {
int limit = num;
INT SQR_LIM;
Vector & lt; bool & gt; IS_PRIME_F (Num + 10);
int x2, y2;
int i, j;
int n;
// Initialization of the sieve
SQR_LIM = (INT) SQRT ((Long Double) Limit);
for (i = 0; i & lt; = limit; i ++) is_prime_f [i] = false;
IS_PRIME_F  = TRUE;
is_prime_f  = True;
// Presumably simple - it is integer with an odd number
// Views in these square forms.
// X2 and Y2 are squares I and J (optimization).
x2 = 0;
For (i = 1; i & lt; = SQR_LIM; I ++) {
x2 + = 2 * i - 1;
y2 = 0;
For (j = 1; j & lt; = sqr_lim; j ++) {
y2 + = 2 * j - 1;
n = 4 * x2 + y2;
if ((N & LT; = Limit) & amp; & amp; (n% 12 == 1 || n% 12 == 5))
is_prime_f [n] =! is_prime_f [n];
// n = 3 * x2 + y2;
n - = x2; // Optimization
if ((N & LT; = Limit) & amp; & amp; (n% 12 == 7))
is_prime_f [n] =! is_prime_f [n];
// n = 3 * x2 - y2;
n - = 2 * y2; // Optimization
if ((i & gt; j) & amp; & amp; (n & lt; = limit) & amp; & amp; (n% 12 == 11))
is_prime_f [n] =! is_prime_f [n];
}
}
// Count the multiple squares of prime numbers in the interval [5, SQRT (Limit)].
// (the main stage can not cut them out)
For (i = 5; I & lt; = SQR_LIM; I ++) {
if (IS_PRime_F [i]) {
n = i * i;
For (j = n; j & lt; = limit; j + = n) {
IS_PRIME_F [J] = false;
}
}
}
// Conclusion of a list of prime numbers into the console.
// Printf ("2, 3, 5");
if ((i == 2) || (i == 3) || (i == 5))
RETURN TRUE;
For (i = 6; i & lt; = limit; I ++) {// Added a varying test at 3 and 5. In the original version of the algorithm, there is no need for it.
if ((IS_PRIME_F [i]) & amp; & amp; (i% 3! = 0) & amp; & amp; (i% 5! = 0)) {
// Printf (",% d", i);
if (i == num)
RETURN TRUE;
}
}
Return 0;
}
Int Main (int argc, char * argv []) {
if (IS_PRime (983)) {
COUT & LT; & LT; "Prime" & lt; & lt; Endl;
} else {
COUT & LT; & LT; "NOT PRIME" & LT; & LT; Endl;
}
Return 0;
}
``````

I agree with the Renegator. For VBA (perhaps someone needs) String function, Returning all squares of prime numbers

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.