Rebol3 Code Examplex


Attractive numbers

Identify numbers with a prime count of prime factors.

Rebol [
    title: "Rosetta code: Attractive numbers"
    file:  %Attractive_numbers.r3
    url:   https://rosettacode.org/wiki/Attractive_numbers
]

count-prime-factors: function [n [integer!]][
    if n = 1    [return 0]
    if prime? n [return 1]
    nn: n count: 0 f: 2
    forever [
        either zero? nn % f [
            ++ count
            nn: nn / f
            if nn = 1     [break]
            if prime? nn  [f: nn]
        ][
            f: either f >= 3 [f + 2] [3]
        ]
    ]
    count
]

attractive-numbers: function [max-n [integer!]][
    out: copy []
    repeat i max-n [
        if prime? count-prime-factors i [ append out i ]
    ]
    new-line/skip out true 20 
]

print ["The attractive numbers up to and including" max-n: 120 "are:"]
probe attractive-numbers :max-n

Output:

The attractive numbers up to and including 120 are:
[
    4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32 33 34
    35 38 39 42 44 45 46 48 49 50 51 52 55 57 58 62 63 65 66 68
    69 70 72 74 75 76 77 78 80 82 85 86 87 91 92 93 94 95 98 99
    102 105 106 108 110 111 112 114 115 116 117 118 119 120
]