Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Optimise the Steve of eratosthenes Algorithm #4129

Closed
Swarga-codes opened this issue Mar 27, 2023 · 11 comments
Closed

[BUG] Optimise the Steve of eratosthenes Algorithm #4129

Swarga-codes opened this issue Mar 27, 2023 · 11 comments
Assignees
Labels

Comments

@Swarga-codes
Copy link
Contributor

Description

I was going through the code, that is, the SIEVE OF ERASTOSTHENES Algo and found it was somewhere between 80 82 lines, the code was quite long, can I improve it by reducing the size of the code?

Steps to reproduce

No response

Excepted behavior

The code is performing as expected but it was quite large and lengthy, I would like to shorten the code.

Screenshots

image

Additional context

No response

@sanjug-sonowal
Copy link

i have optimised the algorithm

@Swarga-codes
Copy link
Contributor Author

I mean to say that the number of lines is quite large I can reduce it to half the size of the current algorithm. @siriak can you assign me this?

@yanurag1414
Copy link

Code was good but i can reduce lines and optimise it.@siriak can you assign me this.

@siriak
Copy link
Member

siriak commented Apr 6, 2023

Please open PRs with the changes you want to make

@rohan472000
Copy link
Contributor

@siriak , opened a PR with some changes, please review and suggest if its need any change.

@dhanmeetsingh
Copy link

Hey there!

So, here are some steps that can be taken to improve the existing code for Sieve of Eratosthenes algorithm in an informal way:

We can replace the 'Type' enum with a boolean array to store the prime numbers' status. This will reduce the memory used and make the code simpler to understand.
Instead of calculating the square root of the input number in each iteration, we can calculate it once before the loop starts and store it in a variable. This will make the code run faster.
We can combine the two for loops that write the primes to the result array and calculate the count of primes into one loop. This will make the code more efficient.
Instead of using the 'filter' method to count the number of prime numbers, we can use a variable to count them as we mark each prime number. This will save us from iterating through the array again just to count the number of primes.
Hope this helps!

`
public class SieveOfEratosthenes {

public static int[] findPrimesTill(int n) {
    boolean[] primes = new boolean[n + 1];
    Arrays.fill(primes, true);
    primes[0] = primes[1] = false;

    int cap = (int) Math.sqrt(n);
    for (int i = 2; i <= cap; i++) {
        if (primes[i]) {
            for (int j = i * i; j <= n; j += i) {
                primes[j] = false;
            }
        }
    }

    int[] result = new int[n + 1];
    int count = 0;
    for (int i = 2; i <= n; i++) {
        if (primes[i]) {
            result[count++] = i;
        }
    }
    return Arrays.copyOf(result, count);
}

public static void main(String[] args) {
    int n = 100;
    System.out.println("Searching for all primes from zero to " + n);
    int[] primes = findPrimesTill(n);
    System.out.println("Found: " + Arrays.toString(primes));
}

}
`

@pengxiaoshuai
Copy link

powerful

@Aman254
Copy link

Aman254 commented May 5, 2023

Sure! Why not Can you please Assign Me ? A Request

@Jaiyadav88
Copy link

It can be optimized ,which reduces the length of the code ,Assign it to me?

@rohan472000
Copy link
Contributor

rohan472000 commented May 7, 2023

@siriak kindly look in #4149 for this issue.

@siriak
Copy link
Member

siriak commented May 7, 2023

Fixed in #4149

@siriak siriak closed this as completed May 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

9 participants