Search code examples
dynamic-memory-allocationparipari-gp

Dynamic array in GP/PARI


I need to calculate number of primes from 1 to N. For this i want to divide every next number n for primes in range 2 to sqrt(n). For this, in turn, i need to store all previously collected primes.

How can i store them effectively?

Just in case, i'm not interesting in algorithm for finding number of primes, i'm interested in how to store such kind data in general. In C++ i could use std::vector as it reallocates itself appropriately or maybe some kind of list.


Solution

  • In PARI/GP, for efficient dynamic arrays you can use List. It behaves like the std::vector in C++. Please, see the example below:

    xs = List()
    gp> List([])
    
    listput(xs, 1);
    listput(xs, 2);
    
    xs
    gp> List([1, 2])