Almost prime numbers

Asked

Viewed 282 times

4

I have a doubt in C, I know check if a number is prime, however, almost cousin do not know.. as I do?

Follow the part of the code working! However, just checking if it is prime.. the almost prime does not run

//  quasePrimos.c
// 
//
//  Created by Braynner Teixeira on 12/09/17.
//  Copyright © 2017 Braynner Teixeira. All rights reserved.
//


#include <stdio.h>

int main(){

    int num, i, controle=0;


    printf("Informe um numero: ");
    scanf("%d", &num);

    if (num > 1)
    {


         // Dividir o numero informado por todos os numeros que estao entre ele e 1.

        for (i = 1; i <= num; i++)
        {

            if (num % i == 0) 
            controle++;

        }


           if (controle == 2) {


            printf("O numero %d e um numero primo!\n", num);


        }
        else if (controle == 2 && controle*controle == num) {

                printf("O numero %d e quase primo!\n", num);

            }else {

            printf("O numero %d nao e um numero primo!\n", num);
        }

    }

    else if (num == 1 ) printf("O numero nao é primo e nem quase primo!");

}
  • What is the concept of near prime number?

  • Every integer greater than 1 has at least two positive divisors: itself and 1. The only exceptions are 0 and 1

  • Jefferson Quesado, I give the number 15, and then his product: 3*5 is equal to himself.

  • 16 is almost cousin? I think not, can you confirm me? And 4 is also not almost cousin, check?

  • 1

    Yeah, none of them are!

  • 2

    Here says that 16 is almost cousin (to k=4) @Jeffersonquesado

  • @bfavaretto perfect squares of compound numbers? Ok, so I need to study more

  • That’s my question. Cruel...

  • 1

    @Jefferson And me then? I’ve never heard of "almost" cousins so far. I don’t put my hand on the fire for what’s on Wikipedia, nor did I fully understand.

  • 1

    An almost prime number, or semi prime, is the product of two prime numbers. For example 4, 6, 9, etc... (22, 23, 3*3)

  • As far as I know, it’s the product of two prime values

  • 1

    @So Cleber would be what Wikipedia says limited to k=2?

  • @bfavaretto, is not limited. In this case, a "near prime" is the product of any prime number multiplied by any prime number.

  • In the question it said the following: a number is almost prime if and only if it can be described as a product of two distinct cousins. Ex: 15 (= 5*3) is almost prime, 2 and 4 are not.

  • Well, the concept of almost cousin in question is the 2-quasi-primo. So, @bfavaretto , is the concept of the Wiki in English given k=2. This talk of k is a generalization

  • Semi Primo is the same thing as Almost Primo? I thought not!! Semi Primo was the product of two cousins and Almost was of distinct cousins..

  • @Braynnerteixeira according to the Wikipedia searches of people in this thread, no matter the numbers are different the same. And semi primo would be a specific case of quasi-primo

Show 12 more comments

2 answers

4


A number k-quasi-primo is given because it consists of a product of primes with k factors. Examples:

  • 15 = 3 * 5 is 2-quasi-primo,
  • 9 = 3 * 3 is 2-quasi-primo,
  • 16 = 2 * 2 * 2 * 2 is 4-quasi-cousin,
  • 5 = 5 is 1-quasi-primo.

In this case, the quasi-prime concept is equivalent to 2-quasi-prime. I.e., k = 2.

To do this, we need to find out what the prime factors are and how often they are used in each number. Your algorithm is very close to the detection of a number k-quasi-primo. I will adjust so that the value of the variable k indicates which "degree" of quasi-primality the number n possesses:

int k = 0;
int i = 2;
while (n > 1) {
 while (n % i == 0) {
   k++;
   n /= i;
 }
 i++;
}

In case, to detect with quasi-primality k=2, just check to see if k is 2 at the end of the run. We can also abort the loop before its completion if a new prime factor is detected and k already worth 2, we can return false. That way:

int num = n; // como vou deteriorar o valor de n, vou guardar uma cópia para futura impressão
int quasi_primalidade = 2;
int k = 0;
int i = 2;
while (n > 1) {
 while (n % i == 0) {
   k++;
   n /= i;
   if (k > quasi_primalidade) {
     // pode ser um return se estiver em uma função
     goto FIM_LACOS;
   }
 }
 i++;
}
FIM_LACOS:
if (k == quasi_primalidade) {
  printf("O numero %d e um numero primo!\n", num);
} else {
  printf("O numero %d nao eh um numero primo =(\n", num);
}

1

Your problem is that in the first if condition is (controle == 2).
Soon, he’ll always go in there and never get into the else if (controle == 2 && controle*controle == num) because if the control is 2, it already goes to the first condition.

To solve this problem, try to reverse the conditions:

if (controle == 2 && controle*controle == num) {
    printf("O numero %d e quase primo!\n", num);
}
else if (controle == 2) {
    printf("O numero %d e um numero primo!\n", num);
}
else {
    printf("O numero %d nao e um numero primo!\n", num);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.