%{
Calculate Prime Numbers
author: gerold@zuderstorfer.com
Modified version of the Sieve of Eratosthenes
%}

nr_max_prime = 430;
prime = zeros(1, nr_max_prime);
rest = zeros(1, nr_max_prime);

next_p_idx = 1;
cur_num = 1;

while next_p_idx <= nr_max_prime
    cur_num = cur_num + 1;
    is_prime = true;
    for cur_p_idx = 1 : next_p_idx
        rest(cur_p_idx) = rest(cur_p_idx) + 1;
        if rest(cur_p_idx) == prime(cur_p_idx)
            is_prime = false;
            rest(cur_p_idx) = 0;
        end
    end
    if is_prime
        prime(next_p_idx) = cur_num;
        rest(next_p_idx) = 0;
        fprintf("%d: %d\n", next_p_idx, cur_num);
        next_p_idx = next_p_idx + 1;
    end
end
