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:

1

Sample Output00:

2

Sample Input01:

2

Sample Output01:

3

Sample Input02:

9

Sample Output02:

43

Constraints:

1 <= N <= 200,000

Solution Code:

 

import java.util.Scanner;

public class AdditivePrimes {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		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

 

Leave a Reply

Your email address will not be published. Required fields are marked *