InterviewBit - Prime Sum  

by ne on 2020-12-02 under Algo

This problem is a property of InterviewBit (www.interviewbit.com).

Full details of the question, and if you want to try it out yourself, proceed to interviewbit : https://www.interviewbit.com/problems/prime-sum/

Here's the implementation:

 


public class Solution {
    public int[] primesum(int A) {
        
        for(int i=2;i<1+A/2;i++){
            if(isPrime(i)){
                if(isPrime(A-i)){
                    return new int[]{i,A-i};
                }
            }
        }
        return new int[]{};
    }
    private boolean isPrime(int n){
        int sqrm=1+(int)Math.sqrt(n);
        for(int i=2;i<sqrm;i++){
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
}



Happy Coding !