logo
down
shadow

Is Using a List instead of an Array for generating Prime Numbers more efficient?


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 :


Share : facebook icon twitter icon
Generating A List Of Prime Numbers

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

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

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', [1]) * (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', [0]) * ((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

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

What is the most efficient way to store prime numbers in an array


By : mohamed badhrudeen
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further I'm assuming you already know how to compute the primes and are looking for a compact way to store them.
If by "most efficient" you mean "compressed into the smallest possible space" there is a method that stores primes in a bitarray that is about half as many bits as just storing a true/false flag in a bitarray.
Related Posts Related Posts :
  • Forcefully terminating a thread I didn't write in Java
  • Issue with ojdbc jar in Maven
  • String array length java
  • Java basic compilation error
  • Inquiry about writing a Char via ByteBuffer
  • Getting unexpected results when type casting between long and double
  • Variable throws variable might not have been initialized error
  • Start/stop java application from an external script
  • Confusion in declaring String Objects
  • What are some ways to do multi-core programming on Android, not just multi-thread?
  • hibernate - process scoped identity
  • Null Pointer Exception on getGraphics()
  • Can we print a java message on console without using main method, static variable and static method?
  • sending text from editText to ListView (2 activities)
  • Transfer value from JList to another class in Java
  • Creating 2 streams with one socket
  • How measure time stolen from virtual machine, in Scala/Java?
  • How can I programmatically generate keypress events?
  • org.apache.commons.net.ssh documentation
  • Implementation of Dijkstra`s Algorithm - Stuck in an infinite loop
  • Should I throw IllegalArgmentException when 'user of given id not exist'?
  • In Nimbus, how to paint TextField when MouseOver?
  • why won't checkforWin(File f) return true when it is true?
  • Android: Cursor is always returning null even if the database is not empty
  • Repeating for loop in menu
  • Can Java garbage collector randomly delete objects in the On-Heap tier?
  • Apache CXF Exception: SSL connection unexpectedly closed
  • Add properties to ArrayList<Object> in Java
  • Multiple Adapters or One Adapter for different lists and objects - Code Performance
  • Is it good practice in Java for a class's method to redundantly return a modified global field of the class?
  • How to change the color of the background in libgdx labels?
  • direct file path not working
  • ClassFileTransformer + Javassist: no such field
  • How do I change and instance variable for an object which the user selects from a combobox?
  • reading and updating a large xml file in java
  • ActionListener and Thread.sleep
  • What is the maximum of number of arguments for varargs in java?
  • When I compile I get an error as "ask" cannot be resolved or is not a field"
  • What is the best way to check if 4 integer variables are equal to 0?
  • Storing constant webdriver elements in an enum
  • Java: Why main class does not extend Thread class
  • Java - how to find out if a directory is being used by another process?
  • How can I get a method to print?
  • Java unknown exceptions
  • BufferedReader,StreamWriter crash
  • found raw type: JComboBox
  • Java Generics -> Function return type
  • how to refresh contents of jtable on event of action listener?
  • Why does List interface extend Collection interface?
  • Pros and Cons of Clojure http client libraries
  • use final inside a for each loop
  • Convert java data object to service object
  • Unsupported major.minor version 51.0 while executing JSP
  • How do I display a word diagonally in Java?
  • making sure one task completes before another starts in java
  • My program gives an index out of bounds error
  • Removing duplicate characters in a String (user inputted keyword)
  • Jersey 2.0 Content-Length not set
  • AWS.SimpleQueueService.NonExistentQueue Exception thrown when Accessing Existing SQS queue
  • Where can I find the source code for the com.sun.jdi package?
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org