0

There is an algorithm to check is a number prime/semiprime/composite.

Pseudo code:

    Input a number N and if N – 1 and N + 1 is not divisible by 6 then the number N is Not Prime. else it is prime or semi-prime
    If n-1 or n+1 is divisible by 6 then iterate in the range(sqrt(N) + 1, N) and find a pair (p, q) such that p*q = N by below formula:
p = i - sqrt(i*i - N)
q = n/p
where i = index in range(sqrt(N) + 1, N)

If p*q = N then the number N is semi prime, else it is prime

But it fails for some semi-prime number. For 6 it gives composite instead of semi-prime. For 10 it gives composite instead of semi-prime. But for 7894561 it gives semi-prime as should.

Is there optimal algorithm to deduce is a number prime/semi-prime/composite? Perhaps I misuse the algorithm and don't understand it's limitations? It is explained that number should be greater than 300. But why 300? Does it always work properly for numbers greater than 300? Please advise.

Below is the C++ implementation of the approach:

#include<bits/stdc++.h>
using namespace std ;

void prime(long n) { int flag = 0;

// checking divisibilty by 6
if ((n + 1) % 6 != 0 &amp;&amp; (n - 1) % 6 != 0) 
{
    cout &lt;&lt; (&quot;Composite&quot;) &lt;&lt; endl;
}
else
{

    // breakout if number is perfect square
    double s = sqrt(n);
    if ((s * s) == n) 
    {
        cout&lt;&lt;(&quot;Semi-Prime&quot;)&lt;&lt;endl;
    }
    else 
    {
        long f = (long)s;
        long l = (long)((f * f));

        // Iterating over to get the
        // closest average value
        for (long i = f + 1; i &lt; l; i++)
        {

            // 1st Factor
            long p = i - (long)(sqrt((i * i) - (n)));

            // 2nd Factor
            long q = n / p;

            // To avoid Convergence
            if (p &lt; 2 || q &lt; 2) 
            {
                break;
            }

            // checking semi-prime condition
            if ((p * q) == n)
            {
                flag = 1;
                break;
            }

            // If convergence found
            // then number is semi-prime
            else 
            {

                // convergence not found
                // then number is prime
                flag = 2;
            }
        }

        if (flag == 1) 
        {
            cout&lt;&lt;(&quot;Semi-Prime&quot;)&lt;&lt;endl;
        }
        else if (flag == 2)
        {

            cout&lt;&lt;(&quot;Prime&quot;)&lt;&lt;endl;
        }
    }
}

}

// Driver code int main() {

prime(8179);
prime(7894561);
prime(90000000);
prime(841);
prime(22553);
prime(1187);

}

I used the article.

Kos
  • 101
  • 1
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 28 '21 at 06:09
  • How to? What should I remove? Should I rephrase the title? – Kos Sep 28 '21 at 07:10
  • A semiprime is composite. It is the product of two (possibly equal) primes. – Peter Sep 28 '21 at 11:32
  • 1
    Checking whether a number is a semiprime is as difficult as factoring it. I suggest the following procedure : $(1)$ If the given number is a perfect square, then it is a semiprime if and only if the square root is prime $(2)$ Test the given number. If it is prime , you are done (no semiprime). $(3)$ Search for a small prime factor. (continuation in next comment) – Peter Sep 28 '21 at 11:37
  • 1
    If you have found one you are done (we have a semiprime if and only if the cofactor is prime). Otherwise use the usual prime factorization tools to find any prime factor , which is already enough. Without finding a prime factor, you cannot decide whether you have a semiprime, unless I understood a recent statement right that elliptic curves can detect semiprimes without actually applying the factorization. But I might have misunderstood something, and this would be extremely astonishing, if it would be possible. – Peter Sep 28 '21 at 11:41
  • Fairly easy to create semiprimes of small magnitude. – Roddy MacPhee Oct 13 '21 at 00:33

0 Answers0