How to resolve the algorithm Cuban primes step by step in the Perl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Cuban primes step by step in the Perl programming language
Table of Contents
Problem Statement
The name cuban has nothing to do with Cuba (the country), but has to do with the fact that cubes (3rd powers) play a role in its definition.
Cuban primes were named in 1923 by Allan Joseph Champneys Cunningham.
Note that cuban prime isn't capitalized (as it doesn't refer to the nation of Cuba).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Cuban primes step by step in the Perl programming language
Source code in the perl programming language
use feature 'say';
use ntheory 'is_prime';
sub cuban_primes {
my ($n) = @_;
my @primes;
for (my $k = 1 ; ; ++$k) {
my $p = 3 * $k * ($k + 1) + 1;
if (is_prime($p)) {
push @primes, $p;
last if @primes >= $n;
}
}
return @primes;
}
sub commify {
scalar reverse join ',', unpack '(A3)*', reverse shift;
}
my @c = cuban_primes(200);
while (@c) {
say join ' ', map { sprintf "%9s", commify $_ } splice(@c, 0, 10);
}
say '';
for my $n (1 .. 6) {
say "10^$n-th cuban prime is: ", commify((cuban_primes(10**$n))[-1]);
}
You may also check:How to resolve the algorithm Call a foreign-language function step by step in the Python programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Erlang programming language
You may also check:How to resolve the algorithm Menu step by step in the BASIC programming language
You may also check:How to resolve the algorithm 15 puzzle game step by step in the Amazing Hopper programming language
You may also check:How to resolve the algorithm Euler's identity step by step in the Julia programming language