Perl Weekly Challenge 148.

My solutions (task 1 and task 2 ) to the The Weekly Challenge - 148.

Task 1: Eban Numbers

Submitted by: Mohammad S Anwar
Write a script to generate all Eban Numbers <= 100.

An Eban number is a number that has no letter ‘e’ in it when
the number is spelled in English (American or British).

Example
2, 4, 6, 30, 32 are the first 5 Eban numbers.

I guess I only need to write down the numbers and check their spelling. It wouldn’t be difficult to enumerate all numbers up to 100, but I found in CPAN a module Lingua::EN::Numbers that has a function num2en that translates numbers to English words. With it, the problem becomes extremely simple:

perl -MLingua::EN::Numbers=num2en -E '
for(0..@ARGV[0]//100){say "$_ ",num2en($_)  unless ($n=num2en($_))=~/e/}'

Results:

2 two
4 four
6 six
30 thirty
32 thirty-two
34 thirty-four
36 thirty-six
40 forty
42 forty-two
44 forty-four
46 forty-six
50 fifty
52 fifty-two
54 fifty-four
56 fifty-six
60 sixty
62 sixty-two
64 sixty-four
66 sixty-six

Curiously, there are large gaps, so if I want to get a EBAN number larger than 66 I have to go up to 2000!

perl -MLingua::EN::Numbers=num2en -E '
for(0..$ARGV[0]){say "$_ ",num2en($_)  unless ($n=num2en($_))=~/e/}' 4000

Results:

2 two
4 four
6 six
30 thirty
32 thirty-two
34 thirty-four
36 thirty-six
40 forty
42 forty-two
44 forty-four
46 forty-six
50 fifty
52 fifty-two
54 fifty-four
56 fifty-six
60 sixty
62 sixty-two
64 sixty-four
66 sixty-six
2000 two thousand
2002 two thousand and two
2004 two thousand and four
2006 two thousand and six
2030 two thousand and thirty
2032 two thousand and thirty-two
2034 two thousand and thirty-four
2036 two thousand and thirty-six
2040 two thousand and forty
2042 two thousand and forty-two
2044 two thousand and forty-four
2046 two thousand and forty-six
2050 two thousand and fifty
2052 two thousand and fifty-two
2054 two thousand and fifty-four
2056 two thousand and fifty-six
2060 two thousand and sixty
2062 two thousand and sixty-two
2064 two thousand and sixty-four
2066 two thousand and sixty-six
4000 four thousand

The full program would be

 1  # Perl weekly challenge 148
 2  # Task 1: Eban numbers
 3  #
 4  # See https://wlmb.github.io/2022/01/17/PWC148/#task-1-eban-numbers
 5  use v5.12;
 6  use warnings;
 7  use Lingua::EN::Numbers qw(num2en);
 8  # Usage: ./ch-1.pl [upper_bound]"
 9  my $upper_bound=$ARGV[0]//100;
10  say "The Eban numbers up to $upper_bound are";
11  for(0..$upper_bound){
12      say "$_ ",num2en($_)  unless (my $n=num2en($_))=~/e/
13  }

Example:

./ch-1.pl
./ch-1.pl 4000

Results:

The Eban numbers up to 100 are
2 two
4 four
6 six
30 thirty
32 thirty-two
34 thirty-four
36 thirty-six
40 forty
42 forty-two
44 forty-four
46 forty-six
50 fifty
52 fifty-two
54 fifty-four
56 fifty-six
60 sixty
62 sixty-two
64 sixty-four
66 sixty-six
The Eban numbers up to 4000 are
2 two
4 four
6 six
30 thirty
32 thirty-two
34 thirty-four
36 thirty-six
40 forty
42 forty-two
44 forty-four
46 forty-six
50 fifty
52 fifty-two
54 fifty-four
56 fifty-six
60 sixty
62 sixty-two
64 sixty-four
66 sixty-six
2000 two thousand
2002 two thousand and two
2004 two thousand and four
2006 two thousand and six
2030 two thousand and thirty
2032 two thousand and thirty-two
2034 two thousand and thirty-four
2036 two thousand and thirty-six
2040 two thousand and forty
2042 two thousand and forty-two
2044 two thousand and forty-four
2046 two thousand and forty-six
2050 two thousand and fifty
2052 two thousand and fifty-two
2054 two thousand and fifty-four
2056 two thousand and fifty-six
2060 two thousand and sixty
2062 two thousand and sixty-two
2064 two thousand and sixty-four
2066 two thousand and sixty-six
4000 four thousand

Task 2: Cardano Triplets

Submitted by: Mohammad S Anwar
Write a script to generate first 5 Cardano Triplets.

A triplet of positive integers (a,b,c) is called a Cardano
Triplet if it satisfies the below condition.

(a+b c**(1/2))**(1/3)+(a-b c**(1/2))**(1/3)=1

Example
(2,1,5) is the first Cardano Triplets.

As it is, this equation has square roots and cubic roots which may not be integer, but we can get rid of them by manipulating the equation. Taking its third power we obtain 2a+3(a2-b2 c)1/3=1. Isolating the third power and taking the third power of the resulting equation, it becomes 27(a2-b2 c)=(1-2a)3. Finally, expanding and collecting terms it becomes 8a3+15a2+6a-27b2c-1=0. In this form, the task can be solved with a simple PDL oneliner

perl -MPDL -MPDL::NiceSlice -E '$c=($b=($a=sequence(long,100))->(*1))->(*1);
$k=8*$a*$a*$a+15*$a*$a+6*$a-27*$b*$b*$c-1; say whichND(!$k)->(:,0:4)'

I build a cube of numbers k(a,b,c)=8 a3 + 15a2 + 6a -27b2 c-1 where the indices are taken from the same sequence using different sets of dummy indices (1 and 2 for a, 0 and 2 for b, and 0 and 1 for c, some of which are intrinsically assigned). The call whichND(!$k) then returns the indices a,b,c for which the value of k is zero.

Results:

[
 [ 2  1  5]
 [17 18  5]
 [47 80  5]
 [ 5  2 13]
 [44 45 13]
]

A full version would be

 1  # Perl weekly challenge 148
 2  # Task 2: Cardano triplets
 3  #
 4  # See https://wlmb.github.io/2022/01/17/PWC148/#task-2-cardano-triplets
 5  use v5.12;
 6  use warnings;
 7  use PDL;
 8  use PDL::NiceSlice;
 9  #Usage: .ch-2.pl [-howmany N] [-upperbound U]
10  # to obtain N Cardano triplets with integeres up to U
11  my %options=(-howmany=>5, -upperbound=>100, @ARGV);
12  my ($how_many, $upper_bound)=@options{(-howmany, -upperbound)};
13  my $n=sequence(long,$upper_bound);
14  my $a=$n;
15  my $b=$n(*1);
16  my $c=$n(*1,*1);
17  my $k=8*$a*$a*$a+15*$a*$a+6*$a-27*$b*$b*$c-1;
18  my $r=whichND($k==0); # $r contains Cardano triplets
19  my $found=$r->dim(1); # how many triplets were found
20  say "Not enough candidates, please increase upper bound"
21      unless $found>=$how_many;
22  $how_many=$found if $how_many>$found;
23  say "The first $how_many Cardano triplets are\n", $r(:,0:$how_many-1) if $how_many>0;

Example:

./ch-2.pl

Results:

The first 5 Cardano triplets are

[
 [ 2  1  5]
 [17 18  5]
 [47 80  5]
 [ 5  2 13]
 [44 45 13]
]

Let’s get some more:

./ch-2.pl -howmany 30

Results:

Not enough candidates, please increase upper bound
The first 22 Cardano triplets are

[
 [ 2  1  5]
 [17 18  5]
 [47 80  5]
 [ 5  2 13]
 [44 45 13]
 [17  9 20]
 [47 40 20]
 [ 8  3 21]
 [71 72 21]
 [11  4 29]
 [98 99 29]
 [14  5 37]
 [17  6 45]
 [ 5  1 52]
 [20  7 53]
 [23  8 61]
 [26  9 69]
 [29 10 77]
 [47 20 80]
 [71 36 84]
 [32 11 85]
 [35 12 93]
]

As I didn’t get all 30 numbers, I increase the upper bound:

./ch-2.pl -howmany 30 -upperbound 200

Results:

The first 30 Cardano triplets are

[
 [  2   1   5]
 [ 17  18   5]
 [ 47  80   5]
 [  5   2  13]
 [ 44  45  13]
 [ 17   9  20]
 [ 47  40  20]
 [  8   3  21]
 [ 71  72  21]
 [ 11   4  29]
 [ 98  99  29]
 [ 14   5  37]
 [125 126  37]
 [ 17   6  45]
 [152 153  45]
 [  5   1  52]
 [ 20   7  53]
 [179 180  53]
 [ 23   8  61]
 [ 26   9  69]
 [ 29  10  77]
 [ 47  20  80]
 [ 71  36  84]
 [197 165  84]
 [ 32  11  85]
 [ 35  12  93]
 [ 38  13 101]
 [ 41  14 109]
 [ 11   2 116]
 [ 44  15 117]
]
Written on January 17, 2022