Search code examples
cprimes

How to find prime numbers that are member of twin prime as well as member of cousin prime?


I have to find the prime numbers from 1 to 100 that are in twin prime number members as well as cousin prime number members.

For example : 7 is a member of twin prime number as well as a member of cousin prime number.

and also, I have to find how many this kind of numbers are there from 1 to 100.

sample input and output :

start = 1
end = 100

output : 7 11 13 17 19 41 43 71

Explanation : twin primes in 1 to 100 are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73) cousin primes in 1 to 100 are (3, 7), (7, 11), (13, 17), (19, 23), (37, 41), (43, 47), (67, 71), (79, 83)

SO 7 11 13 17 19 41 43 71 numbers are both in twin primes and cousin primes.

I have tried so far :

To check the twin numbers and cousin numbers I have done this loop

for(i = start; i < end; i++)
    {
        if(isPrime(i) && isPrime(i + 2))
        {
            if(isPrime(i+4) || isPrime(i+2+4))
            {
                count++;
                printf("%d %d %d %d\n",i, i+2, i+4, i+6);
            }
            i++;


        }
    }
    printf("\n");

but It doesn't give me the right result.

What to change to make it work?

the full code is given bellow:

int isPrime(unsigned long number)
{
      int i, nb, count, test,limit;
      test = count = 0;
      nb = number;
      limit = sqrt(nb) + 1;

      if(nb == 1)
      {
          return 0;
      }

      if(nb == 2)
      {
          return 1;
      }

      if (nb % 2 == 0)
              test = 1;
      else{
          for (i = 3 ; i < limit && ! test; i+=2, count++)
            if (nb % i == 0)
              test = 1;
      }
      if (!test)
              return 1;
      else
              return 0;
}

int main()
{
    int start, end;

    printf("Enter start: ");
    scanf("%d", &start);
    printf("Enter end: ");
    scanf("%d", &end);

    int count = 0;
    int count2 = 0;
    unsigned long i;

    for(i = start; i < end; i++)
    {
        if(isPrime(i) && isPrime(i + 2))
        {
            if(isPrime(i+4) || isPrime(i+2+4))
            {
                count++;
                printf("%d %d %d %d\n",i, i+2, i+4, i+6);
            }
            i++;
            //count++;

        }
    }
    printf("\n");

    printf("The number: %d",count);



    return 0;
}

I have used unsigned long so that I can use this program to find large number later.

Edit for the main function

int main()
{
    int start, end;

    printf("Enter start: ");
    scanf("%d", &start);
    printf("Enter end: ");
    scanf("%d", &end);

    int count = 0;
    int count2 = 0;
    unsigned long i;

    for(i = start; i < end; i++)
    {
        if(isPrime(i) && isPrime(i + 2))
        {
            printf("[ %lu , %lu ]\n", i, i+2);
            i++;
            count++;

        }
    }
    for(i = start; i < end; i++)
    {
        if(isPrime(i) && isPrime(i + 4))
        {
            printf("[ %lu , %lu ]\n", i, i+4);
            i++;
            count2++;

        }
    }
    printf("The number of twins: %d",count);
    printf("The number of cousins: %d",count2);


    return 0;
}

this main function gives twin primes and cousin primes. But I want to find the common numbers of those two. It's kind of confusing for me. I don't know what to do to find the common numbers.


Solution

  • An easy solution (requiring additional memory - likely to be optimized) is to build the list of twins and cousins and to intersect these two list.

    Example:

    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    #include <errno.h>
    
    int isPrime(unsigned long number)
    {
          int i, nb, count, test,limit;
          test = count = 0;
          nb = number;
          limit = sqrt(nb) + 1;
    
          if(nb == 1)
          {
              return 0;
          }
    
          if(nb == 2)
          {
              return 1;
          }
    
          if (nb % 2 == 0)
                  test = 1;
          else{
              for (i = 3 ; i < limit && ! test; i+=2, count++)
                if (nb % i == 0)
                  test = 1;
          }
          if (!test)
                  return 1;
          else
                  return 0;
    }
    
    int main()
    {
        unsigned long start, end;
    
        printf("Enter start: ");
        scanf("%lu", &start);
        printf("Enter end: ");
        scanf("%lu", &end);
    
        int count = 0;
        int count2 = 0;
        unsigned long i;
        unsigned long j;
    
        unsigned long *tl;
        unsigned int tcount = 0;
    
        unsigned long *cl;
        unsigned int ccount = 0;
    
        int found;
        unsigned long int count3;
    
        tl = malloc((end - start) * sizeof(unsigned long));
        if (tl == NULL) 
        {
          perror("malloc");
          return 1;
        }  
    
        cl = malloc((end - start) * sizeof(unsigned long));
        if (cl == NULL) 
        {
          perror("malloc");
          return 1;
        }  
    
    
        for(i = start; i < end; i++)
        {
            if(isPrime(i) && isPrime(i + 2))
            {
                printf("twin: \t[ %lu , %lu ]\n", i, i+2);
    
                tl[tcount]=i;
                tcount++;
                tl[tcount]=i+2;
                tcount++;
    
                i++;
                count++;
    
            }
    
            if(isPrime(i) && isPrime(i + 4))
            {
                printf("cousin: [ %lu , %lu ]\n", i, i+4);
    
               cl[ccount]=i;
               ccount++;
               cl[ccount]=i+4;
               ccount++;
    
                i++;
                count2++; 
    
            }
        }
    
        printf("The number of twins: %d\n",count);
        printf("The number of cousins: %d\n",count2);
    
        printf("List of common twins and cousins:\n");
        count3 =  0;
        for (i=0; i < tcount; i++)
        {
          found = 0;
          for (j=0; j < ccount; j++)
          {
             if (tl[i] == cl[j])
             found = 1; 
          } 
          if (found == 1)
          {
             count3++;
             printf("%lu ",tl[i]);
          }
    
        }   
        printf("\n");
        printf("The number of twins and cousins: %lu\n",count3);
    
        return 0;
    }
    

    Execution:

    $ ./ptc2
    Enter start: 2
    Enter end: 100
    twin:   [ 3 , 5 ]
    twin:   [ 5 , 7 ]
    cousin: [ 7 , 11 ]
    twin:   [ 11 , 13 ]
    cousin: [ 13 , 17 ]
    twin:   [ 17 , 19 ]
    cousin: [ 19 , 23 ]
    twin:   [ 29 , 31 ]
    cousin: [ 37 , 41 ]
    twin:   [ 41 , 43 ]
    cousin: [ 43 , 47 ]
    twin:   [ 59 , 61 ]
    cousin: [ 67 , 71 ]
    twin:   [ 71 , 73 ]
    cousin: [ 79 , 83 ]
    cousin: [ 97 , 101 ]
    The number of twins: 8
    The number of cousins: 8
    List of common twins and cousins:
    7 11 13 17 19 41 43 71 
    The number of twins and cousins: 8