Cubans In Their Prime

Wherein we look at an idea as it grows, seeking out the unique differences that arise along the way…

THE WEEKLY CHALLENGE – PERL & RAKU #158 Task 2


“Last time I was in Havana, a meal at a paladar would have been rice and beans. Now? Sushi. A certain sign of impending apocalypse.”

— Anthony Bourdain


First Series Cuban Primes

Submitted by: Mohammad S Anwar

Write a script to compute first series Cuban Primes ≤ 1000. Please refer wikipedia page for more informations.

Output
7, 19, 37, 61, 127, 271, 331, 397, 547, 631, 919.

ANALYSIS

Unlike the Brazilian numbers we examined earlier, which are named for a prestigious mathematics tournament in Brazil, the Cuban primes have little if anything to actually do with Cuba, and are instead coined as wordplay on their cubic base function. There are multiple types of Cuban primes, and those of the first series can be thought of as built from a list of the differences between successive cubes.

The actual equation used to define the base series is

(x3y3) / (xy) : x = y + 1, y > 0

so in the specific case cited, the denominator reduces to

( (y + 1) – y ) → 1

and we can see the that we are indeed talking, as we said, about the difference between x3 and y3, where x and y are adjacent integers.

There is a second series derived from the base equation with the conditions

x = y + 2, y > 0

as well. In both series we then select for those values that are also prime. There are other differences that can be used between x and y, but an interesting property of the base equation is that the solutions are epicyclic around modulo 3, and for

x = y + 3, y > 0

there are no prime solutions, nor for any multiple of 3.

METHOD

PERL 5 SOLUTION

I got a little curious and solved for the generalized form of the Cuban numbers for the first 10 series. We can see the connection to mod 3, as for every third series would then produce a remainder 0, yielding x = y and no primes.

use warnings;
use strict;
use utf8;
use feature ":5.26";
use feature qw(signatures);
no warnings 'experimental::signatures';


sub nth_series_cubic ( $dif ) {
    my @c;
    my $x;
    for my $y ( 1..100 ) {  
        $x = $y + $dif;
        push @c, ($x**3 - $y**3) / ($x - $y) ;
    }
    return @c;
}

sub is_prime ($num) {
    for ( 2..sqrt $num ) {
        return 0 unless $num % $_;
    }
    return 1;
}

## output section
my $out;
for my $diff ( 1..10 ) {
    $out .= sprintf "  %3s  | %s\n", 
        $diff, 
        join ' ', grep { is_prime($_) && $_ <= 1000 } nth_series_cubic( $diff ) ;
}

say<<~"END";
    series | sequence
    -------+------------------------------------------
    $out
    END

Which delivers the output

series | sequence
-------+------------------------------------------
    1  | 7 19 37 61 127 271 331 397 547 631 919
    2  | 13 109 193 433 769
    3  | 
    4  | 31 79 151 367
    5  | 43 67 97 223 277 337 727 823
    6  | 
    7  | 73 103 139 181 229 283 409 643 733 829
    8  | 163 379 523 691 883
    9  | 
   10  | 457 613 997
Raku Solution

In Raku we’ll only do the first series as requested, in the form of a list comprehension. Using a lazy infinite list of integers, we process them using a reduced form of the base equation until we exceed the limit. Along the way we use the gather/take construct to take only those values that are also prime.

unit sub MAIN ( $limit = 1000) ;

say gather for 1..* -> $x {
    my $n = 3 * $x² + 3 * $x + 1;
    last if $n > $limit;
    take $n if $n.is-prime;
}


The Perl Weekly Challenge, that idyllic glade wherein we stumble upon the holes for these sweet descents, is now known as

The Weekly Challenge – Perl and Raku

It is the creation of the lovely Mohammad Sajid Anwar and a veritable swarm of contributors from all over the world, who gather, as might be expected, weekly online to solve puzzles. Everyone is encouraged to visit, learn and contribute at

https://theweeklychallenge.org

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s