How to resolve the algorithm Attractive numbers step by step in the Draco programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Attractive numbers step by step in the Draco programming language

Table of Contents

Problem Statement

A number is an   attractive number   if the number of its prime factors (whether distinct or not) is also prime.

The number   20,   whose prime decomposition is   2 × 2 × 5,   is an   attractive number   because the number of its prime factors   (3)   is also prime.

Show sequence items up to   120.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Attractive numbers step by step in the Draco programming language

Source code in the draco programming language

/* Sieve of Eratosthenes */
proc nonrec sieve([*] bool prime) void:
    word p, c, max;
    max := (dim(prime,1)-1)>>1;
    prime[0] := false;
    prime[1] := false;
    for p from 2 upto max do prime[p] := true od;
    for p from 2 upto max>>1 do
        if prime[p] then
            for c from p*2 by p upto max do
                prime[c] := false
            od
        fi
    od
corp

/* Count the prime factors of a number */
proc nonrec n_factors(word n; [*] bool prime) word:
    word count, fac;
    fac := 2;
    count := 0;
    while fac <= n do
        if prime[fac] then
            while n % fac = 0 do
                count := count + 1;
                n := n / fac
            od
        fi;
        fac := fac + 1
    od;
    count
corp

/* Find attractive numbers <= 120 */
proc nonrec main() void:
    word MAX = 120;
    [MAX+1] bool prime;
    unsigned MAX i;
    byte col;
    sieve(prime);
    col := 0;
    for i from 2 upto MAX do
        if prime[n_factors(i, prime)] then
            write(i:4);
            col := col + 1;
            if col % 18 = 0 then writeln() fi
        fi
    od
corp

  

You may also check:How to resolve the algorithm Straddling checkerboard step by step in the Phix programming language
You may also check:How to resolve the algorithm Colour pinstripe/Display step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Copy a string step by step in the 6502 Assembly programming language
You may also check:How to resolve the algorithm Menu step by step in the F# programming language
You may also check:How to resolve the algorithm Display a linear combination step by step in the Tcl programming language