# 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. ## 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 ?

2. 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.