Like Tree3Likes
  • 3 Post By grumpy

the longest increasing subsequence of prime numbers whose reverse numbers are prime

This is a discussion on the longest increasing subsequence of prime numbers whose reverse numbers are prime within the C++ Programming forums, part of the General Programming Boards category; Hi guys! I've been trying to make a program that reads n elements of an array and then it displays ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    the longest increasing subsequence of prime numbers whose reverse numbers are prime

    Hi guys!
    I've been trying to make a program that reads n elements of an array and then it displays the longest increasing subsequence of prime numbers whose reverse numbers are prime as well.
    For example if n=12 and the elements of tha array(positive integers)are:
    51 48 11 971 44 19 181 751 347 33 929 359
    it should display the subsequence 181 751 347.
    181 is a prime number and its reverse is 181 that is prime as well.
    It's the same for 751==>157 and 347==>743
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
       int i,lmax = 0,l,poz,p,n,a[50],aux,d,d1,ok1 = 0,ok2 = 0,j,rev = 0,nr,q;
    
        cin >> n;
        for(i = 1;i <= n;i++)cin >> a[i];
        i = 1;
        while (i <= n){ poz = i;
        ok1 = 0;
        aux = a[i];
        for(d = 2;d <= aux/2;d++)
        if(aux % d == 0)ok1 = 1;
    
        rev = 0;
        nr = aux;
        while (nr>0){rev = rev*10+nr%10;
        nr/=10;
        }
        ok2 = 0;
    
        for(d1 = 2;d1 <= rev/2;d1++)
        if(rev%d1 == 0)ok2 = 1;
        if((ok1 == 0)&&(ok2 == 0))i++;
        q = i;
        l = q-poz+1;
        if(l > lmax)lmax = l;
       i++;
        }
        
        i = 1;
        while (i <= n){
        poz = 1;
        aux = a[i];
        for(d = 2;d <= aux/2;d++)
        if(aux % d == 0)ok1 = 1;
    
        rev = 0;
        nr = aux;
        while (nr>0){rev = rev*10+nr%10;
        nr/=10;
        }
        ok2 = 0;
        for(d1 = 2;d1 <= rev/2;d1++)
        if(rev % d1 == 0)ok2 = 1;
        if((ok1 == 0)&&(ok2 == 0))i++;
        if (j <= n){
        q = i;
        l = q-poz+1;
        if(l == lmax)
        for(j = poz ;j <= q;j++)cout << a[j] << " ";
        }
       i++;
        }
    return 0;
    }
    I know that it's wrong but I don't know how to fix it.What should I do ?
    Thanks in advance!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,293
    Given the way you have used fairly undescriptive variable names, no comments, and have written it all as a single function it would take a lot of effort for anyone (including yourself) to understand what the code is doing ..... let alone understand what it is doing wrong.

    Try breaking your code into logical bits, such as

    1) A function to test if a value is prime.
    2) Another function that extracts the prime values from an array. It is up to you whether this function overwrites the original array, or writes to a distinct output array.
    3) A function that computes the "reverse" of an integer.
    4) A function that prints the elements of an array.

    Test all of the components separately. Once you have each distinct function working, put them together to achieve what you want.

    Note that it is up to you whether you create a different set of functions than I have suggested. The point, however, is to break your problem into smaller, logically self-contained functions that have a clear purpose.

    And remember to name your function, their arguments, and variables used within them in an understandable manner.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non prime numbers or composite numbers program.help plz!
    By danishzaidi in forum C Programming
    Replies: 10
    Last Post: 11-15-2011, 10:10 AM
  2. Prime numbers
    By whiterebbit in forum C++ Programming
    Replies: 21
    Last Post: 12-11-2008, 11:18 PM
  3. Prime Numbers
    By EdSquareCat in forum C++ Programming
    Replies: 9
    Last Post: 05-29-2007, 05:54 PM
  4. prime numbers
    By Unregistered in forum C Programming
    Replies: 17
    Last Post: 08-20-2002, 08:57 PM
  5. Prime Numbers
    By cmangel518 in forum C++ Programming
    Replies: 13
    Last Post: 04-30-2002, 11:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21