Problem: Length of longest common prime subsequence from two given arrays
Given two arrays arr1[] and arr2[] of length N and M respectively, the task is to find the length of the longest common prime subsequence that can be obtained from the two given arrays.

Sample Input: arr1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9}, arr2[] = {2, 5, 6, 3, 7, 9, 8}
Sample Output: 4
Explanation:
The longest common prime subsequence present in both the arrays is {2, 3, 5, 7}.

Solution (python):
# Python implementation of the above approach

# Function to calculate the LCS

def recursion(arr1, arr2, i, j, dp):
    if i >= len(arr1) or j >= len(arr2):
        return 0
    key = (i, j)
    if arr1[i] == arr2[j]:
        return 1 + recursion(arr1, arr2,
                             i + 1, j + 1, dp)
    if key in dp:
        return dp[key]
    else:
        dp[key] = max(recursion(arr1, arr2,
                                i + 1, j, dp),
                      recursion(arr1, arr2,
                                i, j + 1, dp))
    return dp[key]

# Function to generate
# all the possible
# prime numbers


def primegenerator(n):
    cnt = 0
    primes = [True for _ in range(n + 1)]
    p = 2
    while p * p <= n:
        for i in range(p * p, n + 1, p):
            primes[i] = False
        p += 1
    return primes

# Function which returns the
# length of longest common
# prime subsequence


def longestCommonSubseq(arr1, arr2):

    # Minimum element of
    # both arrays
    min1 = min(arr1)
    min2 = min(arr2)

    # Maximum element of
    # both arrays
    max1 = max(arr1)
    max2 = max(arr2)

    # Generating all primes within
    # the max range of arr1
    a = primegenerator(max1)

    # Generating all primes within
    # the max range of arr2
    b = primegenerator(max2)

    finala = []
    finalb = []

    # Store precomputed values
    dp = dict()

    # Store all primes in arr1[]
    for i in range(min1, max1 + 1):
        if i in arr1 and a[i] == True:
            finala.append(i)

    # Store all primes of arr2[]
    for i in range(min2, max2 + 1):
        if i in arr2 and b[i] == True:
            finalb.append(i)

    # Calculating the LCS
    return recursion(finala, finalb,
                     0, 0, dp)


# Driver Code
arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
arr2 = [2, 5, 6, 3, 7, 9, 8]

# Function Call
print(longestCommonSubseq(arr1, arr2))