I completely agree with haukex that the size of your arrays is most probably completely irrelevant to your performance problem.
I think that your program is slow because its algorithm is slow, very slow when numbers get big.
A very simple improvement, when you try division, is to try with two and only odd numbers. This alone will make your algorithm about twice faster. Going further in the same direction is to skip multiples of 2, 3, 5, etc., or, better yet, to maintain a list of prime numbers and to try division only with prime numbers.
Another possible option is to improve your primality test. Take a look at the Miller-Rabin algorithm (https://en.wikipedia.org/wiki/Miller%E2%80%93Rabin_primality_test).
Although I am not really keen on micro-optimizations, this:
might be better written as:while ($i <= $num - 1) {
while ($i < $num) {
In reply to Re: Avoid keeping larger lists in Memory
by Laurent_R
in thread Avoid keeping larger lists in Memory
by pr33
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |