A Way For Learning

Fibonacci-Prime Number

No comments

Fibonacci-prime number : Given a number N, you need to find if N is fibonacci-prime number or not. A fibonacci-prime is any number that is both a prime and a fibonacci number.

import java.math.BigInteger;
import java.util.HashSet;
import java.util.Scanner;

public class Solution
{
    static boolean isPrime(BigInteger bigInteger)
    {
        if(bigInteger.isProbablePrime(1))
            return true;
        return false;
    }

    @SuppressWarnings("unchecked")
    static void findAndSetFibonacciPrimeNumbers(HashSet set)
    {
        BigInteger a=new BigInteger("0");
        BigInteger b=new BigInteger("1");
        BigInteger s=a.add(b);
        BigInteger range=new BigInteger("10").pow(75);
        while (range.compareTo(s)==1)
        {
            a=b;
            b=s;
            s=a.add(b);
            if(s.isProbablePrime(2))
                set.add(s);
        }
    }
    public static void main(String[] args)
    {
        HashSet<BigInteger> set=new HashSet<>();
        findAndSetFibonacciPrimeNumbers(set);
        /*System.out.println(set.toString());*/
        Scanner sc=new Scanner(System.in);
        int testCases=sc.nextInt();
        while (testCases-->0)
        {
            BigInteger n=new BigInteger(sc.next());
            if(set.contains(n))
                System.out.println(1);
            else System.out.println(0);
        }
    }
}

No comments :

Post a Comment