0

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.

Piotr Semenov
  • 1,761
  • 16
  • 24
Yola
  • 18,496
  • 11
  • 65
  • 106

1 Answers1

2

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])
Piotr Semenov
  • 1,761
  • 16
  • 24