How to resolve the algorithm Fortunate numbers step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Fortunate numbers step by step in the Raku programming language

Table of Contents

Problem Statement

A Fortunate number is the smallest integer m > 1 such that for a given positive integer n, primorial(n) + m is a prime number, where primorial(n) is the product of the first n prime numbers. For example the first fortunate number is 3 because primorial(1) is 2 and 2 + 3 = 5 which is prime whereas 2 + 2 = 4 is composite.

After sorting and removal of any duplicates, compute and show on this page the first 8 Fortunate numbers or, if your language supports big integers, the first 50 Fortunate numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Fortunate numbers step by step in the Raku programming language

Source code in the raku programming language

my @primorials = [\*] grep *.is-prime, ^∞;

say display :title("First 50 distinct fortunate numbers:\n"),
   (squish sort @primorials[^75].hyper.map: -> $primorial {
       (2..∞).first: (* + $primorial).is-prime
   })[^50];

sub display ($list, :$cols = 10, :$fmt = '%6d', :$title = "{+$list} matching:\n") {
    cache $list;
    $title ~ $list.batch($cols)».fmt($fmt).join: "\n"
}


  

You may also check:How to resolve the algorithm Abbreviations, automatic step by step in the Erlang programming language
You may also check:How to resolve the algorithm Copy a string step by step in the BQN programming language
You may also check:How to resolve the algorithm Sorting algorithms/Selection sort step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Variables step by step in the Pascal programming language
You may also check:How to resolve the algorithm Keyboard input/Obtain a Y or N response step by step in the LiveCode programming language