user
A   **long prime**   (as defined here)   is a prime number whose reciprocal   (in decimal)   has
a   *period length*   of one less than the prime number.

**Long primes**   are also known as:

:   :   :   * base ten cyclic numbers
            * full reptend primes
            * golden primes
            * long period primes
            * maximal period primes
            * proper primes

Another definition:   primes   **p**   such that the decimal expansion of   **1/p**   has period   **p-1**,   which is the greatest period possible for any integer.

Example

**7**   is the first long prime,   the reciprocal of seven
is   **1****/****7**,   which
is equal to the repeating decimal fraction   **0.142857142857···**

The length of the   *repeating*   part of the decimal fraction
is six,   (the underlined part)   which is one less
than the (decimal) prime number   **7**.
  
Thus   **7**   is a long prime.

There are other (more) general definitions of a   **long prime**   which
include wording/verbiage for bases other than ten.

Task
:   * Show all long primes up to   **500**   (preferably on one line).
    * Show the   *number*   of long primes up to         **500**
    * Show the   *number*   of long primes up to      **1,000**
    * Show the   *number*   of long primes up to      **2,000**
    * Show the   *number*   of long primes up to      **4,000**
    * Show the   *number*   of long primes up to      **8,000**
    * Show the   *number*   of long primes up to   **16,000**
    * Show the   *number*   of long primes up to   **32,000**
    * Show the   *number*   of long primes up to   **64,000**   (optional)
    * Show all output here.

Also see

* Wikipedia: full reptend prime
* MathWorld: full reptend prime
* OEIS: A001913
assistant
(formerly Perl 6)

**Works with**: Rakudo version 2018.06

Not very fast as the numbers get larger.

```
use Math::Primesieve;
my $sieve = Math::Primesieve.new;

sub is-long (Int $p) {
    my $r = 1;
    my $rr = $r = (10 * $r) % $p for ^$p;
    my $period;
    loop {
        $r = (10 * $r) % $p;
        ++$period;
        last if $period >= $p or $r == $rr;
    }
    $period == $p - 1 and $p > 2;
}

my @primes = $sieve.primes(500);
my @long-primes = @primes.grep: {.&is-long};

put "Long primes ≤ 500:\n", @long-primes;

@long-primes = ();

for 500, 1000, 2000, 4000, 8000, 16000, 32000, 64000 -> $upto {
    state $from = 0;
    my @extend = $sieve.primes($from, $upto);
    @long-primes.append: @extend.hyper(:8degree).grep: {.&is-long};
    say "\nNumber of long primes ≤ $upto: ", +@long-primes;
    $from = $upto;
}

```

Output:

```
Long primes ≤ 500:
7 17 19 23 29 47 59 61 97 109 113 131 149 167 179 181 193 223 229 233 257 263 269 313 337 367 379 383 389 419 433 461 487 491 499

Number of long primes ≤ 500: 35

Number of long primes ≤ 1000: 60

Number of long primes ≤ 2000: 116

Number of long primes ≤ 4000: 218

Number of long primes ≤ 8000: 390

Number of long primes ≤ 16000: 716

Number of long primes ≤ 32000: 1300

Number of long primes ≤ 64000: 2430
```