Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

sorting boolean array with prime index


import java.util.Scanner;

public class BooleanPrimes {
    public static int counter ;
    public static void main(String[] argh)
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter a number: ");
        int size = scanner.nextInt();
        boolean[] boolArray = new boolean[size+1];
        generateBoolArray(boolArray,size+1);
    }

    public static boolean[] generateBoolArray(boolean[] boolArr, int size) // initializing boolean array with true values
    {
        for (int i = 2; i < size; ++i)
        {
            boolArr[i] = true;
        }
        return chickIfIndexPrime(boolArr, boolArr.length,size);
    }

    public static boolean[] chickIfIndexPrime(boolean[] arrIsPrime, int input, int size)
    {
        int start = 2;
        while (start*start <= input) // first+second loop checking if start is prime
        {
            int i = 2;
            boolean isprime = true;
            while(i*i < start && isprime ) // second loop
            {
                if(start%i == 0) {
                    isprime = false;
                }
                ++i;
            }
            if(isprime==true)
            {
                for(int j=4; j<arrIsPrime.length;++j) // third loop checking if the index of the array is prime
                {

                    if(j%start ==0 )
                    {
                        if(j == start)
                        { // Bug Fixed 
                            ++j;
                            if(j >=arrIsPrime.length){
                                break;
                         }
                    }
                    arrIsPrime[j]=false;

                }
             }
          }
          
            ++start;

        }
        printArray(arrIsPrime,size); // eventually printing the array
        return arrIsPrime;
    }



    public static void printArray(boolean[] arr ,int size){
        System.out.println("The prime numbers from 2 till "+(size-1));
        int i=0,j = 0 ;
        while(i<arr.length){
            if(arr[i]==true){
                System.out.print(i+" ");
            }
            ++i;
        }

        System.out.println();

    }

}










Comment

PREVIOUS NEXT
Code Example
Java :: Java (sun-jdk-1.8.0_51) sample 
Java :: how much epsom salt should strawberries need 
Java :: hasAuthority method not working with thymeleaf 
Java :: java declared fields vs fields 
Java :: masquer saisie mot de passe java console 
Java :: how to get data from firebase notification in java 
Java :: andriod button wont change color 
Java :: Java Using range(e1, e2) Method 
Java :: print string in reverse order using recursion java 
Java :: how to sort a interable in java 
Java :: Java program to calculate cubic capacity cc in bikes 
Java :: how to reorder numbers in a list randomly in java 
Java :: ant bild skip java doc 
Java :: fix intellij resetting the java version everytime you add a dependency 
Java :: sethomebutton listener android 
Java :: fail if condition not met in java if condition 
Java :: fab icon color 
Java :: detect view in scrollview android 
Java :: Labeled continue Statement Java 
Java :: forge close gui java 
Java :: aws lambda upload file to s3 java 
Java :: convert java code to kotlin online 
Java :: recycler view event being raised multiple times 
Java :: java binary search tree 
Java :: if statement with string java 
Java :: java join array 
Java :: arraylist add at index 
Java :: calling a method in java 
Java :: use of getclass()in string 
Java :: Java Scanner nextDouble() 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =