简体   繁体   中英

Finding a Semi Prime number using recursion c++

is there any way to convert this to a recursion form? how to find the unknown prime factors(in case it is a semiprime)?

semiPrime function:

bool Recursividad::semiPrimo(int x)
{
    int valor = 0;
    bool semiPrimo = false;
    if(x < 4)
    {
        return semiPrimo;
    }
    else if(x % 2 == 0)
    {
        valor = x / 2;
        if(isPrime(valor))
        {
            semiPrimo = true;
        }
    }
    return semiPrimo;
}

Edit: i've come to a partial solution(not in recursive form). i know i have to use tail recursion but where?

   bool Recursividad::semiPrimo(int x){
    bool semiPrimo=false;
vector<int> listaFactores= factorizarInt(x);
vector<int> listaFactoresPrimos;
int y = 1;

for (vector<int>::iterator it = listaFactores.begin();
            it!=listaFactores.end(); ++it) {
                if(esPrimo(*it)==true){
            listaFactoresPrimos.push_back(*it);         
        }
    } 
int t=listaFactoresPrimos.front();
if(listaFactoresPrimos.size()<=1){  
    if(t*t==x){
    semiPrimo=true;
    }
}else{
    int f=0;
    #pragma omp parallel 
    {
     #pragma omp for
    for (vector<int>::iterator it = listaFactoresPrimos.begin();
            it!=listaFactoresPrimos.end(); ++it) {
                f=*it;
                int j=0;
                for (vector<int>::iterator ot = listaFactoresPrimos.begin();
            ot!=listaFactoresPrimos.end(); ++ot) {
                j=*ot;

                if((f * j)==x){
                            semiPrimo=true;         }

                }

    } 
    }
}
return semiPrimo;
}

any help would be appreciated

You can convert a loop into recursion in a formulaic manner. Note that do_something() needn't be a single function call; it can be anything (except flow control like break that'd change the loop behavior):

void iterative() {
    for (int x = 0; x < 10; ++x) {
        do_something(x);
    }
}

becomes

void recursion_start() {
    recursive(0);
}

void recursive(int x) {
    if (x < 10) {
        do_something(x);
        recursive(x + 1);
    }
}

Note also that you can rewrite that as the following, which in a good compiler will actually run just as fast as the iterative version (this is called "tail-call optimization"). gcc 4.6.2, for example, manages to do this—actually, its smart enough to do the above version as well.

void recursive(int x) {
    if (x >= 10)
        return;
    do_something(x);
    recursive(x + 1);
}

Actually your algorithm isn't the best way to do it. If x will be more than 100, your program will fail.

The naive algorithm to check if the number is prime is the trial division algorithm . Implementation with recursion:

bool is_prime_rec(int x, int it = 2)
{
    if (it > sqrt(double(x)))
        return true;
    return x%it ? is_prime_rec(x, ++it) : false;
}

But it will look much better if we replace recursion with a cycle:

bool is_prime(int x)
{
    if (x == 2)
        return true;
    if (x%2 == 0)
        return false;

    // speed up a bit
    for (int i = 3; i <= sqrt(double(x)); i += 2) 
        if (x%i == 0)
            return false;
    return true;
}

The usual answer for finding prime numbers from 1 to n is the Sieve of Erasthones . But first, you need to figure out how you're determining whether the number is semi-prime. You can catch the trivial cases from 1 to 7 if you'd like. After that, it's a matter of running the sieve and checking each prime number as a divisor. If it's an even divisor and the quotient is also on your list of primes, you're golden. If it's not on your list of prime (and hasn't been reached by the sieve yet), add it to a list of likelies and check those as you generate sufficiently high prime numbers. Once you find two primes, exit with success. If you reach the your number divided by your smallest prime divisor, exit with failure.

Catch is, I can't think of a way to implement this in recursion and not hurt performance. That said, you can combine this with derobert's bit about converting to recursion, passing along pointers to your reference arrays of primes and likelies.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM