Finding Nth Additive Prime [Java]

Problem :

A positive integer is called an additive prime number if it is prime and the sum of its digits is also prime.

For example, 83 is prime, and 8 + 3 = 11, which is also prime. Thus, 83 is an additive prime. Note that all single digit primes sum to themselves, and are thus additive primes.

Your task is to calculate elements in the sequence of additive primes.

Input Format:

Given an integer N.

Output Format:

Print the Nth additive prime.

Sample Input00:


Sample Output00:


Sample Input01:


Sample Output01:


Sample Input02:


Sample Output02:



1 <= N <= 200,000

Solution Code:


import java.util.Scanner;

public class AdditivePrimes {

	public static void main(String[] args) {

		Scanner sc = new Scanner(;

		int N = Integer.parseInt(sc.nextLine());
		int S =N*50;
		boolean[] isPrime = new boolean[S];

		int l = 0;
		int number = 2;
		for(int i=2;i