  C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD # Is Using a List instead of an Array for generating Prime Numbers more efficient?  » java » Is Using a List instead of an Array for generating Prime Numbers more efficient?

By : LÙn LÒe LOẹt
Date : November 14 2020, 04:51 PM
fixed the issue. Will look into that further Your algorithm is not efficient. So regardless of which data structure you use
you won't get a great performance speed-up. You should use another algorithm code : ## Generating A List Of Prime Numbers

By : Mahmoud Amine Ka
Date : March 29 2020, 07:55 AM
To fix this issue Good job on the prime testing. Your loop, however, is both more code than you need and kind of off-track. Based on the code sample you show, you probably want this:
code :
``````generatePrime(X, Y, N) :-
between(X, Y, N),
isPrime(N).
``````
``````?- generatePrime(2, 10, X).
X = 2 ;
X = 3 ;
X = 5 ;
X = 7 ;
false.
``````
``````generatePrime(X, Y) :-
between(X, Y, N),
isPrime(N),
write(N), nl,
fail.
generatePrime(_, _).
``````
``````generatePrimes(X, Y) :-
forall(
(between(X, Y, N), isPrime(N)),
(write(N), nl))).
`````` ## Efficient method for generating lists of large prime numbers

By : Seth Mbhele
Date : March 29 2020, 07:55 AM
help you fix your problem You can use a more efficient algorithm to generate the entire list of prime numbers up to N. This is the Sieve of Erathostenes. Please have a look at the linked article, it even includes an example pseudocode. The basic idea of the algorithm is:
maintain L, a list of potentially prime numbers (initially all numbers from 2 to N) pick the next prime number (p) as the first element of L (intially 2) remove all numbers that are a multiple of p, up to N, since they cannot be prime repeat from step 2
code :
``````def eratosthenes2(n):
multiples = set()
for i in range(2, n+1):
if i not in multiples:
yield i
multiples.update(range(i*i, n+1, i))

print(list(eratosthenes2(100)))
`````` ## Most efficient way to filter prime numbers from a list of random numbers in Python

By : user7072889
Date : March 29 2020, 07:55 AM
will be helpful for those in need Sieve of Eratosthenes, taking about 0.17 seconds for primes under 10 million on PyPy 3.5 on my device:
code :
``````from array import array

def primes(upper):
numbers = array('B', ) * (upper + 1)

for i in range(2, int(upper ** 0.5) + 1):
if numbers[i]:
low_multiple = i * i
numbers[low_multiple:upper + 1:i] = array('B', ) * ((upper - low_multiple) // i + 1)

return {i for i, x in enumerate(numbers) if i >= 2 and x}
``````
``````filter_primes = primes(10_000_000).intersection
`````` ## Use of `all()` function while generating prime numbers with list comprehensions

By : user2674204
Date : March 29 2020, 07:55 AM
This might help you Explanation of the code by deciding a single number whether its a prime or not:
Lets see if 5 is a prime number or not
code :
``````calculation = [5%2!=0?, 5%3!=0?, 5%4!=0?]
Calculation = [True, True, True]
all(calculation) -> True (because all element of an iterable object(list) is true)
``````
``````calculation = [4%2!=0?, 4%3!=0?]
Calculation = [False, True]
all(calculation) -> False (because if any element in an iterable collection is false means all() will return false.)
`````` ## What is the most efficient way to store prime numbers in an array 