Posts

Search

Amdocs Coding Question 2

 Given a range of integers [X,Y] where X and Y both are included. Write a program to find the composite numbers and the count C of numbers in that sub-range. A composite number is a number (except 1) that i snot a prime number.

Constraints:-

1. X, Y>1

2. X<Y

Input Format:-

 A single line of input contains X and Y separated by a single white space .

Output Format:-

Multiple lines of output contain three integers each M,N and C where [M,N] are the numbers and C is the count of numbers in the sub-range. Integers in each output line are separated by a single white space.

Solution:-

import java.util.*;

public class Main

{

    static boolean isPrime(int n) { 

     if (n%2==0) return false; 

    for(int i=3;i<=Math.sqrt(n);i+=2) { 

        if(n%i==0) 

            return false; 

    } 

    return true; 

static void sieveAlg1(int x,int y)

{

    int ar[]=new int[50];

    int co=0,z=0;

    for(int i=x;i<=y;i++)

    {

        boolean c=isPrime(i);

        if(c==false)

        {

            co++;

            ar[z++]=i;

            continue;

        }

        if(co>=7)

        {

            System.out.println(ar[0]+" "+ar[co-1]+" "+co);

            z=0;

            co=0;

        }

        else if(c==true)

        {

            co=0;

            z=0;

        }

   }

    

}

public static void main(String[] args) {

    Scanner sc =new Scanner(System.in);

    int testStart=sc.nextInt();

    int testStop=sc.nextInt();

    sieveAlg1(testStart,testStop);     

}

}