How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the Raku programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the Raku programming language

Table of Contents

Problem Statement

Calculate the sequence where each term an is the nth that has n divisors. Show here, on this page, at least the first 15 terms of the sequence.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sequence: nth number with exactly n divisors step by step in the Raku programming language

Source code in the raku programming language

sub div-count (\x) {
    return 2 if x.is-prime;
    +flat (1 .. x.sqrt.floor).map: -> \d {
        unless x % d { my \y = x div d; y == d ?? y !! (y, d) }
    }
}

my $limit = 20;

my @primes = grep { .is-prime }, 1..*;
@primes[$limit]; # prime the array. SCNR

put "First $limit terms of OEIS:A073916";
put (1..$limit).hyper(:2batch).map: -> $n {
    ($n > 4 and $n.is-prime) ??
    exp($n - 1, @primes[$n - 1]) !!
    do {
        my $i = 0;
        my $iterator = $n %% 2 ?? (1..*) !! (1..*).map: *²;
        $iterator.first: {
            next unless $n == .&div-count;
            next unless ++$i == $n;
            $_
        }
    }
};


  

You may also check:How to resolve the algorithm Greatest common divisor step by step in the XPL0 programming language
You may also check:How to resolve the algorithm A+B step by step in the smart BASIC programming language
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Lua programming language
You may also check:How to resolve the algorithm Sequence: smallest number greater than previous term with exactly n divisors step by step in the Quackery programming language
You may also check:How to resolve the algorithm Write entire file step by step in the Yabasic programming language