< December 2021 >
MonTueWedThuFriSatSun
29300102030405
06070809101112
13141516171819
20212223242526
27282930310102

Monday, 27 December 2021

08:07 PM

Perl Weekly Challenge 144: Semiprimes and Ulam Sequence [blogs.perl.org] 08:07 PM, Monday, 27 December 2021 04:00 PM, Monday, 27 December 2021

These are some answers to the Week 144 of the Perl Weekly Challenge organized by Mohammad S. Anwar.

Spoiler Alert: This weekly challenge deadline is due in a few days from now (on December 26, 2021 at 24:00). This blog post offers some solutions to this challenge, please don’t read on if you intend to complete the challenge on your own.

Task 1: Semiprimes

Write a script to generate all Semiprime number <= 100.

For more information about Semiprime, please checkout the Wikipedia page.

In mathematics, a semiprime is a natural number that is the product of exactly two prime numbers. The two primes in the product may equal each other, so the semiprimes include the squares of prime numbers.

Example:

10 is Semiprime as 10 = 2 x 5
15 is Semiprime as 15 = 3 x 5

Semiprimes might look like a somewhat useless curiosity, but they are in fact extremely important in the field of public key cryptography. For example, generating the public and private keys for a RSA cipher involves generating two very large prime numbers (with several dozens of digits) and computing their product. Raku has built-in features to quickly create RSA keys (and also to encode messages and decode cryptograms): arbitrary precision integers, an efficient is-prime primality test (using the fast Miller-Rabin test) and modular arithmetic. See this for further details. But that’s another subject.

Semiprimes in Raku

We start by generating a list of prime numbers between 1 and 50. Then we want to generate all pairs of such primes. Since we also want square semiprimes (i.e. numbers that are squares of prime numbers), we cannot use the combinations method. We will use instead the X cross product operator between the array of primes and itself, multiply the pair items, filter out those which are too large, sort them and remove duplicates.

use v6;

constant \MAX = 100;
my @primes = grep { .is-prime }, 1..MAX/2;
my @nums = grep { $_ <= MAX }, map { [*] $_[0,1] }, 
    (@primes X @primes);
say @nums.sort.squish;
# say now - INIT now;

This program displays the following output:

$ raku ./semi-primes.raku
(4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95)

Semiprimes in Perl

Our Perl implementation is essentially a port of the Raku implementation, except that we had to roll out our primessubroutine for generating a list of prime integers, and to use two nested loops for generating the pairs of primes.

use strict;
use warnings;
use feature "say";
use constant MAX => 100;

sub primes {
    my $max = shift;
    my @primes = (2, 3, 5);
    PRIMES: for my $c (7..$max/2) {
        for my $i (2..$c/2) {
            next PRIMES unless $c % $i;
        }
        push @primes, $c;
    }
    return @primes;
}

my @p = primes MAX;
my @semi_primes;
# Generating pairs of primes and their product 
for my $i (0..$#p) {
    for my $j (0..$i) {
        my $product = $p[$i] * $p[$j];
        push @semi_primes, $product if $product <= MAX;
    }
}
my @result;
my $j = -1;
# Removing duplicate products
for my $i (sort {$a <=> $b} @semi_primes) {
    push @result, $i if $i != $j;
    $j = $i;
}
say "@result";

This program displays the following output:

$ perl semi-primes.pl
4 6 9 10 14 15 21 22 25 26 33 34 35 38 39 46 49 51 55 57 58 62 65 69 74 77 82 85 86 87 91 93 94 95

Task 2: Ulam Sequence

You are given two positive numbers, $u and $v.

Write a script to generate Ulam Sequence having at least 10 Ulam numbers where $u and $v are the first 2 Ulam numbers.

For more information about Ulam Sequence, please checkout this website.

The standard Ulam sequence (the (1, 2)-Ulam sequence) starts with U1 = 1 and U2 = 2. Then for n > 2, Un is defined to be the smallest integer that is the sum of two distinct earlier terms in exactly one way and larger than all earlier terms.

Example 1:

Input: $u = 1, $v = 2
Output: 1, 2, 3, 4, 6, 8, 11, 13, 16, 18

Example 2:

Input: $u = 2, $v = 3
Output: 2, 3, 5, 7, 8, 9, 13, 14, 18, 19

Example 3:

Input: $u = 2, $v = 5
Output: 2, 5, 7, 9, 11, 12, 13, 15, 19, 23

The fact that a member of the sequence has to be “the smallest integer that is the sum of two distinct earlier terms in exactly one way” makes it quite difficult to construct the number directly from the previous ones. For example, suppose we have so far 1, 2, 3, 4. It would probably possible to find the next one. But the fact that it has to be the sum “in exactly one way” excludes 5 from the sequence, because it can be reached in two ways (1 + 4 and 2 + 3). But to be able to find that, we basically need to build all possibilities. In other words, we basically need a brute force approach: find all pairs of previous terms, perform the sums and find the smallest unique sum that is larger than the largest previous term. In the process, we can possibly improve the process by pruning values that are too small or too large, to reduce the number of values to examine, but it is still basically a brute force approach.

Note that the task specification asks us to find “at least 10 Ulam numbers”. I’ve decided to generate 12 Ulam numbers, i.e. 10 numbers in addition to the two seeds. This wasn’t requested, but it is still in line the the requirement of providing at least 10 Ulam numbers.

Ulam Sequence in Raku

For a given existing sequence of numbers, we use the combinations method to generate all possible pairs, sum each of them, compute the sums, filter out the sums that are too small and sort them. Then we loop on the resulting list, remove the duplicates and insert in the sequencearray the first valid candidate. And we start again with the new sequence and do it ten times in total.

uses v6;

sub ulam ($first, $second) {
    my @sequence = $first, $second;
    for 1..10 {
        my @sums = sort grep { $_ > @sequence[*-1] }, 
            map { [+] $_[0, 1] }, @sequence.combinations: 2;
        my $last = 0;
        for 0..@sums.end -> \i {
            next if @sums[i] == $last;
            push @sequence, @sums[i] and last if i >= @sums.end;
            $last = @sums[i] and next if @sums[i] == @sums[i+1];
            push @sequence, @sums[i] and last;
        }
    }
    return @sequence;
}
for (1,2), (2,3), (2,5) -> $test {
  say "$test => ", ulam |$test;

}

This program displays the following output:

$ raku ./ulam_seq.raku
1 2 => [1 2 3 4 6 8 11 13 16 18 26 28]
2 3 => [2 3 5 7 8 9 13 14 18 19 24 25]
2 5 => [2 5 7 9 11 12 13 15 19 23 27 29]

Ulam Sequence in Perl

This is essentially a port to Perl of the Raku solution above, except that we had to implement our own combine subroutine to replace the Raku built-in combination method.

use strict;
use warnings;
use feature "say";

sub combine {
    my @seq = @_;
    my $min = $seq[-1];
    my @comb_sums;
    for my $i (0..$#seq) {
        for my $j (0..$i-1) {
            my $sum =  $seq[$i] + $seq[$j];
            next if $sum <= $min;
            push @comb_sums, $sum;
        }
    }
    return sort { $a <=> $b } @comb_sums;
}

sub ulam {
    my @sequence = @{$_[0]};
    for (1..10) {
        my @sums = combine @sequence;
        my $last = 0;
        for my $i (0..$#sums) {
            next if $sums[$i] == $last;
            push @sequence, $sums[$i] and last if $i >= $#sums;
            $last = $sums[$i] and next if $sums[$i] == $sums[$i+1];
            push @sequence, $sums[$i] and last;
        }
    }
    return @sequence;
}
for my $test ([1,2], [2,3], [2,5]) {
    say "@$test => ", join " ", ulam $test;
}

This program displays the following output:

1 2 => 1 2 3 4 6 8 11 13 16 18 26 28
2 3 => 2 3 5 7 8 9 13 14 18 19 24 25
2 5 => 2 5 7 9 11 12 13 15 19 23 27 29

Wrapping up

The next week Perl Weekly Challenge will start soon. If you want to participate in this challenge, please check https://perlweeklychallenge.org/ and make sure you answer the challenge before 23:59 BST (British summer time) on January 2, 2022. And, please, also spread the word about the Perl Weekly Challenge if you can.

01:31 PM

Steinar H. Gunderson: USB-C shenanigans [Planet Debian] 01:31 PM, Monday, 27 December 2021 02:20 PM, Monday, 27 December 2021

At some point, my phone stopped taking charge (over USB-C) from one charger, but not the other—it would briefly say “charging”, then drop it, wait a few seconds, and then try again in an infinite loop. However, charging every night on the included charger worked fine, so I wasn't that worried.

Naturally, I blamed the charger. But the other day, I noticed what was going on; if I flipped the cable, it would charge with absolutely no problems at all! And similarly, if I flipped the cable for the other charger, I'd see the issue. So it was merely about the twisting of each cable that caused the simplest insertion direction to be the correct one for the included charger, and the wrong one for the other one. (USB-C has some special logic to detect and deal with this “reversed” polarity. I guess it's somehow broken in my phone.)

Moral: Unknown.

12:00 AM

Abiola Ajadi: Outreachy-Everyone Struggles! [Planet Debian] 12:00 AM, Monday, 27 December 2021 08:20 PM, Tuesday, 28 December 2021

Hello Everyone!

Three weeks into my internship and it’s been great so far with Awesome Mentors. I am currently learning a new Language which is Ruby and this is the perfect time to remind myself that everyone struggles! I struggled a bit getting farmiliar with the codebase and pushing my first merge request during the internship. I won’t say i have a perfect understanding of how everything works, but i am learning.

What is Debci?

Debci stands for Debian Continous Integration before i move on, what is continous integration? according to Atlassian ‘Continuous integration (CI) is the practice of automating the integration of code changes from multiple contributors into a single software project. It’s a primary DevOps best practice, allowing developers to frequently merge code changes into a central repository where builds and tests then run. Automated tools are used to assert the new code’s correctness before integration.’

From the official documentation; The Debian continuous integration (debci) is an automated system that coordinates the execution of automated tests against packages in the Debian system.

Debci exist to make sure packages work currently after an update, How it does this is by testing all of the packages that have tests written in them to make sure it works and nothing is broken. It has a UI for developers to see if the test passes or not.

Progress

This week; I have learnt how to write automated test and Squashing multiple commits into one single commit.

Feeds

FeedRSSLast fetchedNext fetched after
XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Bits of DNA XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
blogs.perl.org XML 12:00 AM, Tuesday, 18 January 2022 12:15 AM, Tuesday, 18 January 2022
Blue Collar Bioinformatics XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Boing Boing XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Epistasis Blog XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Futility Closet XML 12:00 AM, Tuesday, 18 January 2022 12:15 AM, Tuesday, 18 January 2022
gCaptain XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Hackaday XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
In between lines of code XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
InciWeb Incidents for California XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
LeafSpring XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Living in an Ivory Basement XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
LWN.net XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Mastering Emacs XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Planet Debian XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Planet Emacsen XML 12:00 AM, Tuesday, 18 January 2022 12:15 AM, Tuesday, 18 January 2022
RNA-Seq Blog XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
RStudio Blog - Latest Comments XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
RWeekly.org - Blogs to Learn R from the Community XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
The Adventure Blog XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
The Allium XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
Variance Explained XML 12:00 AM, Tuesday, 18 January 2022 12:30 AM, Tuesday, 18 January 2022
January 2022
MonTueWedThuFriSatSun
27282930310102
03040506070809
10111213141516
17181920212223
24252627282930
31010203040506
December 2021
MonTueWedThuFriSatSun
29300102030405
06070809101112
13141516171819
20212223242526
27282930310102
November 2021
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
29300102030405
October 2021
MonTueWedThuFriSatSun
27282930010203
04050607080910
11121314151617
18192021222324
25262728293031
September 2021
MonTueWedThuFriSatSun
30310102030405
06070809101112
13141516171819
20212223242526
27282930010203
August 2021
MonTueWedThuFriSatSun
26272829303101
02030405060708
09101112131415
16171819202122
23242526272829
30310102030405
July 2021
MonTueWedThuFriSatSun
28293001020304
05060708091011
12131415161718
19202122232425
26272829303101
June 2021
MonTueWedThuFriSatSun
31010203040506
07080910111213
14151617181920
21222324252627
28293001020304
May 2021
MonTueWedThuFriSatSun
26272829300102
03040506070809
10111213141516
17181920212223
24252627282930
31010203040506
April 2021
MonTueWedThuFriSatSun
29303101020304
05060708091011
12131415161718
19202122232425
26272829300102
March 2021
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
29303101020304
February 2021
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
November 2020
MonTueWedThuFriSatSun
26272829303101
02030405060708
09101112131415
16171819202122
23242526272829
30010203040506
September 2020
MonTueWedThuFriSatSun
31010203040506
07080910111213
14151617181920
21222324252627
28293001020304
July 2020
MonTueWedThuFriSatSun
29300102030405
06070809101112
13141516171819
20212223242526
27282930310102
June 2020
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
29300102030405
May 2020
MonTueWedThuFriSatSun
27282930010203
04050607080910
11121314151617
18192021222324
25262728293031
April 2020
MonTueWedThuFriSatSun
30310102030405
06070809101112
13141516171819
20212223242526
27282930010203
February 2020
MonTueWedThuFriSatSun
27282930310102
03040506070809
10111213141516
17181920212223
24252627282901
January 2020
MonTueWedThuFriSatSun
30310102030405
06070809101112
13141516171819
20212223242526
27282930310102
December 2019
MonTueWedThuFriSatSun
25262728293001
02030405060708
09101112131415
16171819202122
23242526272829
30310102030405
November 2019
MonTueWedThuFriSatSun
28293031010203
04050607080910
11121314151617
18192021222324
25262728293001
October 2019
MonTueWedThuFriSatSun
30010203040506
07080910111213
14151617181920
21222324252627
28293031010203
August 2019
MonTueWedThuFriSatSun
29303101020304
05060708091011
12131415161718
19202122232425
26272829303101
July 2019
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
29303101020304
June 2019
MonTueWedThuFriSatSun
27282930310102
03040506070809
10111213141516
17181920212223
24252627282930
May 2019
MonTueWedThuFriSatSun
29300102030405
06070809101112
13141516171819
20212223242526
27282930310102
April 2019
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
29300102030405
March 2019
MonTueWedThuFriSatSun
25262728010203
04050607080910
11121314151617
18192021222324
25262728293031
February 2019
MonTueWedThuFriSatSun
28293031010203
04050607080910
11121314151617
18192021222324
25262728010203
January 2019
MonTueWedThuFriSatSun
31010203040506
07080910111213
14151617181920
21222324252627
28293031010203
December 2018
MonTueWedThuFriSatSun
26272829300102
03040506070809
10111213141516
17181920212223
24252627282930
31010203040506
November 2018
MonTueWedThuFriSatSun
29303101020304
05060708091011
12131415161718
19202122232425
26272829300102
October 2018
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
29303101020304
September 2018
MonTueWedThuFriSatSun
27282930310102
03040506070809
10111213141516
17181920212223
24252627282930
August 2018
MonTueWedThuFriSatSun
30310102030405
06070809101112
13141516171819
20212223242526
27282930310102
July 2018
MonTueWedThuFriSatSun
25262728293001
02030405060708
09101112131415
16171819202122
23242526272829
30310102030405
June 2018
MonTueWedThuFriSatSun
28293031010203
04050607080910
11121314151617
18192021222324
25262728293001
May 2018
MonTueWedThuFriSatSun
30010203040506
07080910111213
14151617181920
21222324252627
28293031010203
April 2018
MonTueWedThuFriSatSun
26272829303101
02030405060708
09101112131415
16171819202122
23242526272829
30010203040506
February 2018
MonTueWedThuFriSatSun
29303101020304
05060708091011
12131415161718
19202122232425
26272801020304
January 2018
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
29303101020304
December 2017
MonTueWedThuFriSatSun
27282930010203
04050607080910
11121314151617
18192021222324
25262728293031
November 2017
MonTueWedThuFriSatSun
30310102030405
06070809101112
13141516171819
20212223242526
27282930010203
September 2017
MonTueWedThuFriSatSun
28293031010203
04050607080910
11121314151617
18192021222324
25262728293001
August 2017
MonTueWedThuFriSatSun
31010203040506
07080910111213
14151617181920
21222324252627
28293031010203
March 2017
MonTueWedThuFriSatSun
27280102030405
06070809101112
13141516171819
20212223242526
27282930310102
January 2017
MonTueWedThuFriSatSun
26272829303101
02030405060708
09101112131415
16171819202122
23242526272829
30310102030405
November 2016
MonTueWedThuFriSatSun
31010203040506
07080910111213
14151617181920
21222324252627
28293001020304
October 2016
MonTueWedThuFriSatSun
26272829300102
03040506070809
10111213141516
17181920212223
24252627282930
31010203040506
September 2016
MonTueWedThuFriSatSun
29303101020304
05060708091011
12131415161718
19202122232425
26272829300102
August 2016
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
29303101020304
July 2016
MonTueWedThuFriSatSun
27282930010203
04050607080910
11121314151617
18192021222324
25262728293031
May 2016
MonTueWedThuFriSatSun
25262728293001
02030405060708
09101112131415
16171819202122
23242526272829
30310102030405
April 2016
MonTueWedThuFriSatSun
28293031010203
04050607080910
11121314151617
18192021222324
25262728293001
December 2014
MonTueWedThuFriSatSun
01020304050607
08091011121314
15161718192021
22232425262728
29303101020304
October 2014
MonTueWedThuFriSatSun
29300102030405
06070809101112
13141516171819
20212223242526
27282930310102