How to resolve the algorithm Safe primes and unsafe primes step by step in the Perl programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Safe primes and unsafe primes step by step in the Perl programming language

Table of Contents

Problem Statement

Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Safe primes and unsafe primes step by step in the Perl programming language

Source code in the perl programming language

use ntheory qw(forprimes is_prime);

my $upto = 1e7;
my %class = ( safe => [], unsafe => [2] );

forprimes {
    push @{$class{ is_prime(($_-1)>>1) ? 'safe' : 'unsafe' }}, $_;
} 3, $upto;

for (['safe', 35], ['unsafe', 40]) {
    my($type, $quantity) = @$_;
    print  "The first $quantity $type primes are:\n";
    print join(" ", map { comma($class{$type}->[$_-1]) } 1..$quantity), "\n";
    for my $q ($upto/10, $upto) {
        my $n = scalar(grep { $_ <= $q } @{$class{$type}});
        printf "The number of $type primes up to %s: %s\n", comma($q), comma($n);
    }
}

sub comma {
    (my $s = reverse shift) =~ s/(.{3})/$1,/g;
    $s =~ s/,(-?)$/$1/;
    $s = reverse $s;
}


  

You may also check:How to resolve the algorithm String length step by step in the Elixir programming language
You may also check:How to resolve the algorithm Subleq step by step in the Logo programming language
You may also check:How to resolve the algorithm Mind boggling card trick step by step in the J programming language
You may also check:How to resolve the algorithm Teacup rim text step by step in the Haskell programming language
You may also check:How to resolve the algorithm Day of the week step by step in the Picat programming language