How to resolve the algorithm Long primes step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Long primes step by step in the Raku programming language

Table of Contents

Problem Statement

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:

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.

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.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Long primes step by step in the Raku programming language

Source code in the raku programming language

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;
}


  

You may also check:How to resolve the algorithm SHA-256 Merkle tree step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Empty program step by step in the Tcl programming language
You may also check:How to resolve the algorithm Greatest common divisor step by step in the x86 Assembly programming language
You may also check:How to resolve the algorithm Search a list step by step in the Scheme programming language
You may also check:How to resolve the algorithm Arithmetic/Complex step by step in the Raku programming language