Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8471683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:51:46+00:00 2026-06-10T16:51:46+00:00

My goal is to Turn off, or set to false, all array spots that

  • 0

My goal is to Turn off, or set to false, all array spots that are not prime. Array is provided as a parameter.

public static boolean[] sieveOfEratosthenes(boolean [] a){

    int increment= 2;

    for(int n = 0; n < 9; n++){
        for(int i = increment; i < a.length; i += increment){
            a[i] = false;
        }
        increment += 1;
    }   
    a[2] = true;
    a[3] = true;
    a[5] = true;
    a[7] = true;
    return a;
}

Code works fine, I’m just wondering if there is a more efficient way than using:

a[2] = true;
a[3] = true;
a[5] = true;
a[7] = true;

to reset those array items as true.

Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T16:51:47+00:00Added an answer on June 10, 2026 at 4:51 pm

    A good way to do this is to start your loop at iteration * 2. So your loop looks like this

    int increment= 2;
    
    for(int n = 0; n < 9; n++){
        for(int i = increment*2; i < a.length; i += increment){
            a[i] = false;
        }
        increment += 1;
    }  
    

    This way you skip the first one.

    Second modify your outer loop so that it ignores values that are already false because its prime factors took care of its products.
    Now your loop looks like this

    int increment= 2;
    
    for(int n = 0; n < 9; n++){
        if(a[increment]) {
          for(int i = increment*2; i < a.length; i += increment){
              a[i] = false;
          }
          increment += 1;
        }
    }
    

    Third to handle any size array loop over your array for your increment value

    int count = a.length;
    
    for(int increment = 2; increment < count; increment++){
        if(a[increment]) {
          for(int i = increment*2; i < count; i += increment){
              a[i] = false;
          }          
        }
    }
    

    This current loop assumes one and zero are considered prime. So set a[0] = a[1] = false; to reflect the fact that 0 and 1 are not prime.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The goal: Allow user to turn on and off different layers of data; and
How do a turn this public static List<T> CreateListOfAnonymous<T>(T unused) {return new List<T>();} into
How can I turn the first array in to the second one? The goal
Goal is to make a dialog that appears on menu_key pressed, but it keeps
Goal: Produce an Excel document with information from 3 associated models that is similar
Goal: to create a percentage column based off the values of calculated columns. Here's
I have a scenario that I'm trying to turn into a more responsive UI
I recently wrote an extremely basic edge detection algorithm that works on an array
The ultimate goal is to specify contracts for a class that resides in an
I'm attempting to turn the html5 canvas into a slide annotator. My goal is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.