<commit_before>use v6;

=begin pod

=TITLE 10001st prime

=AUTHOR polettix

L<https://projecteuler.net/problem=7>

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see
that the 6th prime is 13.

What is the 10001st prime number?

Expected result: 104743

=end pod

# The number of primes we want, defaults to challenge's request
my $nth = shift(@*ARGS) || 10001;

# A simple implementation of Eratosthenes' sieve
sub primes_iterator {
    return sub {
        state %D;
        state $q //= 2;
        while %D{$q}:exists {
            my $p = %D{$q}:delete;

            my $x = $q + $p;
            $x += $p while %D{$x} :exists;
            %D{$x} = $p;
            ++$q;
        }
        %D{$q * $q} = $q;
        return $q++;
    }
}

my $it = primes_iterator();
for 1 .. $nth - 1 -> $i {
    $it();
    say "found $i primes so far" unless $i % 100;
}
say 'result: ', $it();

# vim: expandtab shiftwidth=4 ft=perl6<commit_msg>Moved number of primes into a MAIN argument<commit_after>use v6;

=begin pod

=TITLE 10001st prime

=AUTHOR polettix

L<https://projecteuler.net/problem=7>

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see
that the 6th prime is 13.

What is the 10001st prime number?

Expected result: 104743

=end pod

# A simple implementation of Eratosthenes' sieve
sub primes_iterator {
    return sub {
        state %D;
        state $q //= 2;
        while %D{$q}:exists {
            my $p = %D{$q}:delete;

            my $x = $q + $p;
            $x += $p while %D{$x} :exists;
            %D{$x} = $p;
            ++$q;
        }
        %D{$q * $q} = $q;
        return $q++;
    }
}

#= nth: The number of primes we want, defaults to challenge's request
#= vebose: print verbose progress information
sub MAIN(Int :$nth = 10001, Bool :$verbose = False) {
    my $it = primes_iterator();
    for 1 .. $nth - 1 -> $i {
        $it();
        if $verbose {
            say "found $i primes so far" unless $i % 100;
        }
    }

    say $it();
}

# vim: expandtab shiftwidth=4 ft=perl6
{'fraction_non_alphanumeric': 0.1407766990291262, 'fraction_numerical': 0.040776699029126215, 'mean_word_length': 2.1513761467889907, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3729519', 'n_tokens_mistral': 826, 'n_tokens_neox': 737, 'n_words': 256}