Attractive numbers: Difference between revisions

Add ABC
(Add BASIC)
(Add ABC)
 
(44 intermediate revisions by 25 users not shown)
Line 15:
:*   The OEIS entry:   [[oeis:A063989|A063989: Numbers with a prime number of prime divisors]].
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F is_prime(n)
I n < 2
R 0B
L(i) 2 .. Int(sqrt(n))
I n % i == 0
R 0B
R 1B
 
F get_pfct(=n)
V i = 2
[Int] factors
L i * i <= n
I n % i
i++
E
n I/= i
factors.append(i)
I n > 1
factors.append(n)
R factors.len
 
[Int] pool
 
L(each) 0..120
pool.append(get_pfct(each))
 
[Int] r
L(each) pool
I is_prime(each)
r.append(L.index)
 
print(r.map(String).join(‘,’))</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|8080 Assembly}}==
 
<langsyntaxhighlight lang="8080asm"> ;;; Show attractive numbers up to 120
MAX: equ 120 ; can be up to 255 (8 bit math is used)
;;; CP/M calls
Line 125 ⟶ 166:
ppage: equ 3 ; primes in page 3
fctrs: equ 256*fcpage
plist: equ 256*ppage</langsyntaxhighlight>
 
{{out}}
 
<pre>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</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN factors n:
PUT {} IN factors
PUT 2 IN factor
WHILE n >= factor:
SELECT:
n mod factor = 0:
INSERT factor IN factors
PUT n/factor IN n
ELSE:
PUT factor+1 IN factor
RETURN factors
 
HOW TO REPORT attractive n:
REPORT 1 = #factors #factors n
 
PUT 0 IN col
FOR i IN {1..120}:
IF attractive i:
WRITE i>>5
PUT col+1 IN col
IF col mod 10=0: WRITE /</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC IsAttractive(BYTE n BYTE ARRAY primes)
BYTE count,f
 
IF n<=1 THEN
RETURN (0)
ELSEIF primes(n) THEN
RETURN (0)
FI
 
count=0 f=2
DO
IF n MOD f=0 THEN
count==+1
n==/f
IF n=1 THEN
EXIT
ELSEIF primes(n) THEN
f=n
FI
ELSEIF f>=3 THEN
f==+2
ELSE
f=3
FI
OD
 
IF primes(count) THEN
RETURN (1)
FI
RETURN (0)
 
PROC Main()
DEFINE MAX="120"
BYTE ARRAY primes(MAX+1)
BYTE i
 
Put(125) PutE() ;clear the screen
Sieve(primes,MAX+1)
PrintF("Attractive numbers in range 1..%B:%E",MAX)
FOR i=1 TO MAX
DO
IF IsAttractive(i,primes) THEN
PrintF("%B ",i)
FI
OD
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Attractive_numbers.png Screenshot from Atari 8-bit computer]
<pre>
Attractive numbers in range 1..120:
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
</pre>
 
=={{header|Ada}}==
{{trans|C}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Attractive_Numbers is
Line 199 ⟶ 334:
begin
Show_Attractive (Max => 120);
end Attractive_Numbers;</langsyntaxhighlight>
 
{{out}}
Line 207 ⟶ 342:
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</pre>
 
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<syntaxhighlight lang="algol68">BEGIN # find some attractive numbers - numbers whose prime factor counts are #
# prime, n must be > 1 #
PR read "primes.incl.a68" PR
# find the attractive numbers #
INT max number = 120;
[]BOOL sieve = PRIMESIEVE ENTIER sqrt( max number );
print( ( "The attractve numbers up to ", whole( max number, 0 ), newline ) );
INT a count := 0;
FOR i FROM 2 TO max number DO
IF INT v := i;
INT f count := 0;
WHILE NOT ODD v DO
f count +:= 1;
v OVERAB 2
OD;
FOR j FROM 3 BY 2 TO max number WHILE v > 1 DO
WHILE v > 1 AND v MOD j = 0 DO
f count +:= 1;
v OVERAB j
OD
OD;
f count > 0
THEN
IF sieve[ f count ] THEN
print( ( " ", whole( i, -3 ) ) );
IF ( a count +:= 1 ) MOD 20 = 0 THEN print( ( newline ) ) FI
FI
FI
OD;
print( ( newline, "Found ", whole( a count, 0 ), " attractive numbers", newline ) )
END</syntaxhighlight>
{{out}}
<pre>
The attractve numbers up to 120
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
Found 74 attractive numbers
</pre>
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">% find some attractive numbers - numbers whose prime factor count is prime %
begin
% implements the sieve of Eratosthenes %
Line 259 ⟶ 437:
for i := 2 until maxNumber do if s( countPrimeFactors( i, s ) ) then writeon( i )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 266 ⟶ 444:
 
=={{header|AppleScript}}==
<langsyntaxhighlight lang="applescript">on isPrime(n)
if (n < 4) then return (n > 1)
if ((n mod 2 is 0) or (n mod 3 is 0)) then return false
Line 314 ⟶ 492:
if (isPrime(primeFactorCount(n))) then set end of output to n
end repeat
return output</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{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}</langsyntaxhighlight>
 
It's possible of course to dispense with the isPrime() handler and instead use primeFactorCount() to count the prime factors of its own output, with 1 indicating an attractive number. The loss of performance only begins to become noticeable in the unlikely event of needing 300,000 or more such numbers!
Line 323 ⟶ 501:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">attractive?: function [x] -> prime? size factors.prime x
 
print select 1..120 => attractive?</langsyntaxhighlight>
 
{{out}}
 
<pre>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 </pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">AttractiveNumbers(n){
c := prime_numbers(n).count()
if c = 1
return
return isPrime(c)
}
isPrime(n){
return (prime_numbers(n).count() = 1)
}
prime_numbers(n) {
if (n <= 3)
return [n]
ans := []
done := false
while !done
{
if !Mod(n,2){
ans.push(2)
n /= 2
continue
}
if !Mod(n,3) {
ans.push(3)
n /= 3
continue
}
if (n = 1)
return ans
sr := sqrt(n)
done := true
; try to divide the checked number by all numbers till its square root.
i := 6
while (i <= sr+6){
if !Mod(n, i-1) { ; is n divisible by i-1?
ans.push(i-1)
n /= i-1
done := false
break
}
if !Mod(n, i+1) { ; is n divisible by i+1?
ans.push(i+1)
n /= i+1
done := false
break
}
i += 6
}
}
ans.push(n)
return ans
}</syntaxhighlight>
Examples:<syntaxhighlight lang="autohotkey">c:= 0
loop
{
if AttractiveNumbers(A_Index)
c++, result .= SubStr(" " A_Index, -2) . (Mod(c, 20) ? " " : "`n")
if A_Index = 120
break
}
MsgBox, 262144, ,% result
return</syntaxhighlight>
{{out}}
<pre> 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 </pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ATTRACTIVE_NUMBERS.AWK
# converted from C
Line 373 ⟶ 620:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 381 ⟶ 628:
 
=={{header|BASIC}}==
<syntaxhighlight lang BASIC="basic">10 DEFINT A-Z: N=120
20 DIM P(N)M=120
30 FORDIM I=2 TO NC(M): PC(I0)=-1: NEXT IC(1)=-1
40 FOR I=2 TO SQR(NM)
50 IF NOT C(I) THEN FOR J=I+I TO M STEP I: C(J)=-1: NEXT
50 IF P(I)>1 GOTO 70
60 NEXT
60 FOR J=I+I TO N STEP I: P(J)=P(J)+1: NEXT J
70 NEXTFOR I=2 TO M
80 FORN=I: IC=2 TO N0
90 IFFOR P(P(I))J=12 THENTO PRINT I,M
100 IF NOT C(J) THEN IF N MOD J=0 THEN N=N\J: C=C+1: GOTO 100
100 NEXT I</lang>
110 NEXT
120 IF NOT C(C) THEN PRINT I,
130 NEXT</syntaxhighlight>
{{out}}
<pre> 4 6 8 9 10
12 14 15 1618 1820
2021 2122 2225 2426 2527
2628 2730 2832 3233 3334
3435 3538 3639 3842 3944
4045 4446 4548 4649 4850
4951 5052 5155 5257 5458
5562 5663 5765 5866 6268
6369 6470 6572 6874 6975
7276 7477 7578 7680 7782
7885 8086 8187 8291 8592
8693 8794 8895 9198 9299
93102 105 94 106 95 108 96 98110
99111 112 100 114 102 115 104 106116
108117 111118 112119 114 115120</pre>
 
116 117 118 119</pre>
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
manifest $( MAXIMUM = 120 $)
 
let sieve(prime, max) be
$( for i=0 to max do i!prime := i>=2
for i=2 to max>>1 if i!prime
$( let j = i<<1
while j <= max do
$( j!prime := false
j := j+i
$)
$)
$)
 
let factors(n, prime, max) = valof
$( let count = 0
for i=2 to max if i!prime
until n rem i
$( count := count + 1
n := n / i
$)
resultis count
$)
 
let start() be
$( let n = 0 and prime = vec MAXIMUM
sieve(prime, MAXIMUM)
for i=2 to MAXIMUM
if factors(i, prime, MAXIMUM)!prime
$( writed(i, 4)
n := n + 1
unless n rem 18 do wrch('*N')
$)
wrch('*N')
$)</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
#define TRUE 1
Line 461 ⟶ 753:
printf("\n");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 474 ⟶ 766:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace AttractiveNumbers {
Line 528 ⟶ 820:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 538 ⟶ 830:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
 
Line 586 ⟶ 878:
cout << endl;
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 596 ⟶ 888:
102 105 106 108 110 111 112 114 115 116 117 118 119 120
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">sieve = proc (max: int) returns (array[bool])
prime: array[bool] := array[bool]$fill(1,max,true)
prime[1] := false
for p: int in int$from_to(2, max/2) do
if prime[p] then
for c: int in int$from_to_by(p*p, max, p) do
prime[c] := false
end
end
end
return(prime)
end sieve
 
n_factors = proc (n: int, prime: array[bool]) returns (int)
count: int := 0
i: int := 2
while i<=n do
if prime[i] then
while n//i=0 do
count := count + 1
n := n/i
end
end
i := i + 1
end
return(count)
end n_factors
 
start_up = proc ()
MAX = 120
po: stream := stream$primary_output()
prime: array[bool] := sieve(MAX)
col: int := 0
for i: int in int$from_to(2, MAX) do
if prime[n_factors(i,prime)] then
stream$putright(po, int$unparse(i), 4)
col := col + 1
if col//15 = 0 then stream$putl(po, "") end
end
end
end start_up</syntaxhighlight>
{{out}}
<pre> 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</pre>
=={{header|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. ATTRACTIVE-NUMBERS.
 
DATA DIVISION.
WORKING-STORAGE SECTION.
77 MAXIMUM PIC 999 VALUE 120.
01 SIEVE-DATA VALUE SPACES.
03 MARKER PIC X OCCURS 120 TIMES.
88 PRIME VALUE SPACE.
03 SIEVE-MAX PIC 999.
03 COMPOSITE PIC 999.
03 CANDIDATE PIC 999.
 
01 FACTORIZE-DATA.
03 FACTOR-NUM PIC 999.
03 FACTORS PIC 999.
03 FACTOR PIC 999.
03 QUOTIENT PIC 999V999.
03 FILLER REDEFINES QUOTIENT.
05 FILLER PIC 999.
05 DECIMAL PIC 999.
 
01 OUTPUT-FORMAT.
03 OUT-NUM PIC ZZZ9.
03 OUT-LINE PIC X(72) VALUE SPACES.
03 COL-PTR PIC 99 VALUE 1.
 
PROCEDURE DIVISION.
BEGIN.
PERFORM SIEVE.
PERFORM CHECK-ATTRACTIVE
VARYING CANDIDATE FROM 2 BY 1
UNTIL CANDIDATE IS GREATER THAN MAXIMUM.
PERFORM WRITE-LINE.
STOP RUN.
 
CHECK-ATTRACTIVE.
MOVE CANDIDATE TO FACTOR-NUM.
PERFORM FACTORIZE.
IF PRIME(FACTORS), PERFORM ADD-TO-OUTPUT.
 
ADD-TO-OUTPUT.
MOVE CANDIDATE TO OUT-NUM.
STRING OUT-NUM DELIMITED BY SIZE INTO OUT-LINE
WITH POINTER COL-PTR.
IF COL-PTR IS EQUAL TO 73, PERFORM WRITE-LINE.
 
WRITE-LINE.
DISPLAY OUT-LINE.
MOVE SPACES TO OUT-LINE.
MOVE 1 TO COL-PTR.
 
FACTORIZE SECTION.
BEGIN.
MOVE ZERO TO FACTORS.
PERFORM DIVIDE-PRIME
VARYING FACTOR FROM 2 BY 1
UNTIL FACTOR IS GREATER THAN MAXIMUM.
GO TO DONE.
 
DIVIDE-PRIME.
IF PRIME(FACTOR),
DIVIDE FACTOR-NUM BY FACTOR GIVING QUOTIENT,
IF DECIMAL IS EQUAL TO ZERO,
ADD 1 TO FACTORS,
MOVE QUOTIENT TO FACTOR-NUM,
GO TO DIVIDE-PRIME.
DONE.
EXIT.
 
SIEVE SECTION.
BEGIN.
MOVE 'X' TO MARKER(1).
DIVIDE MAXIMUM BY 2 GIVING SIEVE-MAX.
PERFORM SET-COMPOSITES THRU SET-COMPOSITES-LOOP
VARYING CANDIDATE FROM 2 BY 1
UNTIL CANDIDATE IS GREATER THAN SIEVE-MAX.
GO TO DONE.
 
SET-COMPOSITES.
MULTIPLY CANDIDATE BY 2 GIVING COMPOSITE.
SET-COMPOSITES-LOOP.
IF COMPOSITE IS NOT GREATER THAN MAXIMUM,
MOVE 'X' TO MARKER(COMPOSITE),
ADD CANDIDATE TO COMPOSITE,
GO TO SET-COMPOSITES-LOOP.
DONE.
EXIT.</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|Comal}}==
<syntaxhighlight lang="comal">0010 FUNC factors#(n#) CLOSED
0020 count#:=0
0030 WHILE n# MOD 2=0 DO n#:=n# DIV 2;count#:+1
0040 fac#:=3
0050 WHILE fac#<=n# DO
0060 WHILE n# MOD fac#=0 DO n#:=n# DIV fac#;count#:+1
0070 fac#:+2
0080 ENDWHILE
0090 RETURN count#
0100 ENDFUNC factors#
0110 //
0120 ZONE 4
0130 seen#:=0
0140 FOR i#:=2 TO 120 DO
0150 IF factors#(factors#(i#))=1 THEN
0160 PRINT i#,
0170 seen#:+1
0180 IF seen# MOD 18=0 THEN PRINT
0190 ENDIF
0200 ENDFOR i#
0210 PRINT
0220 END</syntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
(defun attractivep (n)
(primep (length (factors n))) )
 
; For primality testing we can use different methods, but since we have to define factors that's what we'll use
(defun primep (n)
(= (length (factors n)) 1) )
 
(defun factors (n)
"Return a list of factors of N."
(when (> n 1)
(loop with max-d = (isqrt n)
for d = 2 then (if (evenp d) (+ d 1) (+ d 2)) do
(cond ((> d max-d) (return (list n))) ; n is prime
((zerop (rem n d)) (return (cons d (factors (truncate n d)))))))))
</syntaxhighlight>
{{out}}
<pre>(dotimes (i 121) (when (attractivep i) (princ i) (princ " ")))
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</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
const MAXIMUM := 120;
 
typedef N is int(0, MAXIMUM + 1);
var prime: uint8[MAXIMUM + 1];
 
sub Sieve() is
MemSet(&prime[0], 1, @bytesof prime);
prime[0] := 0;
prime[1] := 0;
var cand: N := 2;
while cand <= MAXIMUM >> 1 loop
if prime[cand] != 0 then
var comp := cand + cand;
while comp <= MAXIMUM loop
prime[comp] := 0;
comp := comp + cand;
end loop;
end if;
cand := cand + 1;
end loop;
end sub;
 
sub Factors(n: N): (count: N) is
count := 0;
var p: N := 2;
while p <= MAXIMUM loop
if prime[p] != 0 then
while n % p == 0 loop
count := count + 1;
n := n / p;
end loop;
end if;
p := p + 1;
end loop;
end sub;
 
sub Padding(n: N) is
if n < 10 then print(" ");
elseif n < 100 then print(" ");
else print(" ");
end if;
end sub;
 
var cand: N := 2;
var col: uint8 := 0;
Sieve();
while cand <= MAXIMUM loop
if prime[Factors(cand)] != 0 then
Padding(cand);
print_i32(cand as uint32);
col := col + 1;
if col % 18 == 0 then
print_nl();
end if;
end if;
cand := cand + 1;
end loop;
print_nl();</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">for x = 1 to 120
 
let n = x
let c = 0
 
do
 
if int(n mod 2) = 0 then
 
let n = int(n / 2)
let c = c + 1
 
endif
 
wait
 
loop int(n mod 2) = 0
 
for i = 3 to sqrt(n) step 2
 
do
 
if int(n mod i) = 0 then
 
let n = int(n / i)
let c = c + 1
 
endif
 
wait
 
loop int(n mod i) = 0
 
next i
 
if n > 2 then
 
let c = c + 1
 
endif
 
if prime(c) then
 
print x, " ",
 
endif
 
next x</syntaxhighlight>
{{out| Output}}<pre>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 </pre>
 
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.stdio;
 
enum MAX = 120;
Line 649 ⟶ 1,255:
}
writeln;
}</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 656 ⟶ 1,262:
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</pre>
 
=={{header|Delphi}}==
See [[#Pascal]].
=={{header|Draco}}==
<syntaxhighlight lang="draco">/* 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</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight lang=easylang>
func isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func count n .
f = 2
repeat
if n mod f = 0
cnt += 1
n /= f
else
f += 1
.
until n = 1
.
return cnt
.
for i = 2 to 120
n = count i
if isprim n = 1
write i & " "
.
.
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight Fsharplang="fsharp">// attractive_numbers.fsx
// taken from Primality by trial division
let rec primes =
Line 696 ⟶ 1,396:
|> List.filter is_attractive
|> List.iteri print_iter
</syntaxhighlight>
</lang>
{{out}}
<pre>>dotnet fsi attractive_numbers.fsx
Line 711 ⟶ 1,411:
=={{header|Factor}}==
{{works with|Factor|0.99}}
<langsyntaxhighlight lang="factor">USING: formatting grouping io math.primes math.primes.factors
math.ranges sequences ;
 
"The attractive numbers up to and including 120 are:" print
120 [1,b] [ factors length prime? ] filter 20 <groups>
[ [ "%4d" printf ] each nl ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 728 ⟶ 1,428:
=={{header|Fortran}}==
{{trans|C++}}
<langsyntaxhighlight lang="fortran">
program attractive_numbers
use iso_fortran_env, only: output_unit
Line 815 ⟶ 1,515:
end function count_prime_factors
end program attractive_numbers
</syntaxhighlight>
</lang>
 
{{out}}
Line 828 ⟶ 1,528:
=={{header|FreeBASIC}}==
{{trans|D}}
<langsyntaxhighlight lang="freebasic">
Const limite = 120
 
Line 879 ⟶ 1,579:
Wend
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 888 ⟶ 1,588:
102 105 106 108 110 111 112 114 115 116 117 118 119 120
</pre>
 
=={{header|Frink}}==
<syntaxhighlight lang="frink">println[select[2 to 120, {|x| !isPrime[x] and isPrime[length[factorFlat[x]]]}]]</syntaxhighlight>
{{out}}
<pre>
[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]
</pre>
 
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Attractive_numbers}}
 
'''Solution.''' Let us make a function to determine whether a number is "attractive" or not.
 
[[File:Fōrmulæ - Attractive numbers 01.png]]
 
'''Test case.''' Show sequence items up to 120.
 
[[File:Fōrmulæ - Attractive numbers 02.png]]
 
[[File:Fōrmulæ - Attractive numbers 03.png]]
 
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
local fn IsPrime( n as NSUInteger ) as BOOL
NSUInteger i
if ( n < 2 ) then exit fn = NO
if ( n = 2 ) then exit fn = YES
if ( n mod 2 == 0 ) then exit fn = NO
for i = 3 to int(n^.5) step 2
if ( n mod i == 0 ) then exit fn = NO
next
end fn = YES
 
local fn Factors( n as NSInteger ) as NSInteger
NSInteger count = 0, f = 2
do
if n mod f == 0 then count++ : n /= f else f++
until ( f > n )
end fn = count
 
void local fn AttractiveNumbers( limit as NSInteger )
NSInteger c = 0, n
printf @"Attractive numbers through %d are:", limit
for n = 4 to limit
if fn IsPrime( fn Factors( n ) )
printf @"%4d \b", n
c++
if ( c mod 10 == 0 ) then print
end if
next
end fn
 
fn AttractiveNumbers( 120 )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Attractive numbers through 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
 
</pre>
 
 
=={{header|Go}}==
Simple functions to test for primality and to count prime factors suffice here.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 962 ⟶ 1,737:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 975 ⟶ 1,750:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class AttractiveNumbers {
static boolean isPrime(int n) {
if (n < 2) return false
Line 1,018 ⟶ 1,793:
println()
}
}</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 1,027 ⟶ 1,802:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes
import Data.Bool (bool)
 
Line 1,035 ⟶ 1,810:
 
main :: IO ()
main = print $ takeWhile (<= 120) attractiveNumbers</langsyntaxhighlight>
 
Or equivalently, as a list comprehension:
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes
 
attractiveNumbers :: [Integer]
Line 1,047 ⟶ 1,822:
 
main :: IO ()
main = print $ takeWhile (<= 120) attractiveNumbers</langsyntaxhighlight>
 
Or simply:
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes
 
attractiveNumbers :: [Integer]
Line 1,059 ⟶ 1,834:
 
main :: IO ()
main = print $ takeWhile (<= 120) attractiveNumbers</langsyntaxhighlight>
{{Out}}
<pre>[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]</pre>
 
=={{header|Insitux}}==
Notice that this implementation is not optimally performant, as primes is called multiple times when the output could be shared, the same is true for distinct-factor and factor.
<syntaxhighlight lang="insitux">
(function primes n
(let find-range (range 2 (inc n))
check-nums (range 2 (-> n ceil sqrt inc))
skip-each-after #(skip-each % (skip %1 %2))
muls (xmap #(drop 0 (skip-each-after (dec %1) % find-range)) check-nums))
(remove (flatten muls) find-range))
(function distinct-factor n
(filter @(div? n) (primes n)))
(function factor n
(map (fn t (find (div? n) (map @(** t) (range (round (sqrt n)) 0)))) (distinct-factor n)))
(function decomposed-factors n
(map (fn dist t (repeat dist (/ (logn t) (logn dist)))) (distinct-factor n) (factor n)))
(var prime? @((primes %)))
 
(var attract-num? (comp decomposed-factors flatten len prime?))
(filter attract-num? (range 121))</syntaxhighlight>
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang j>
echo (#~ (1 p: ])@#@q:) >:i.120
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,073 ⟶ 1,868:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,292 ⟶ 2,087:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre> 4 6 8 9 10 12 14 15 18 20
Line 1,305 ⟶ 2,100:
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang="java">public class Attractive {
 
static boolean is_prime(int n) {
Line 1,349 ⟶ 2,144:
System.out.println();
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,358 ⟶ 2,153:
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
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
This entry uses:
* `is_prime` as defined at [[Erd%C5%91s-primes#jq | Erdős primes]] on RC
* `prime_factors` as defined at [[Smith_numbers#jq | Smith numbers]] on RC
<syntaxhighlight lang="jq">
def count(s): reduce s as $x (null; .+1);
 
def is_attractive:
count(prime_factors) | is_prime;
 
def printattractive($m; $n):
"The attractive numbers from \($m) to \($n) are:\n",
[range($m; $n+1) | select(is_attractive)];
printattractive(1; 120)</syntaxhighlight>
{{out}}
<pre>
The attractive numbers from 1 to 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]
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
# oneliner is println("The attractive numbers from 1 to 120 are:\n", filter(x -> isprime(sum(values(factor(x)))), 1:120))
Line 1,370 ⟶ 2,190:
printattractive(1, 120)
</langsyntaxhighlight>{{out}}
<pre>
The attractive numbers from 1 to 120 are:
Line 1,378 ⟶ 2,198:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.3.21
 
const val MAX = 120
Line 1,431 ⟶ 2,251:
}
println()
}</langsyntaxhighlight>
 
{{output}}
Line 1,443 ⟶ 2,263:
 
=={{header|LLVM}}==
<langsyntaxhighlight lang="llvm">; This is not strictly LLVM, as it uses the C library function "printf".
; LLVM does not provide a way to print values, so the alternative would be
; to just load the string into memory, and that would be boring.
Line 1,668 ⟶ 2,488:
}
 
attributes #0 = { noinline nounwind optnone uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 1,677 ⟶ 2,497:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Returns true if x is prime, and false otherwise
function isPrime (x)
if x < 2 then return false end
Line 1,709 ⟶ 2,529:
for i = 1, 120 do
if isPrime(#factors(i)) then io.write(i .. "\t") end
end</langsyntaxhighlight>
{{out}}
<pre>4 6 8 9 10 12 14 15 18 20 21 22 25 26 27
Line 1,718 ⟶ 2,538:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">attractivenumbers := proc(n::posint)
local an, i;
an :=[]:
Line 1,727 ⟶ 2,547:
end do:
end proc:
attractivenumbers(120);</langsyntaxhighlight>
{{out}}
<pre>[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]</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[AttractiveNumberQ]
AttractiveNumberQ[n_Integer] := FactorInteger[n][[All, 2]] // Total // PrimeQ
Reap[Do[If[AttractiveNumberQ[i], Sow[i]], {i, 120}]][[2, 1]]</langsyntaxhighlight>
{{out}}
<pre>{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}</pre>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
AttractiveNumber(N):=block([Q:0],
if not primep(N) then (
if primep(apply("+", map(lambda([Z], Z[2]), ifactors(N)))) then Q: N
), Q
)$
 
delete(0, makelist(AttractiveNumber(K), K, 1, 120));
</syntaxhighlight>
Using sublist
<syntaxhighlight lang="maxima">
attractivep(n):=block(ifactors(n),apply("+",map(second,%%)),if primep(%%) then true)$
sublist(makelist(i,i,120),attractivep);
</syntaxhighlight>
{{out}}
<pre>
[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]</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
isPrime = function(n)
if n < 2 then return false
if n < 4 then return true
for i in range(2,floor(n ^ 0.5))
if n % i == 0 then return false
end for
return true
end function
 
countFactors = function(n)
cnt = 0
for i in range(2, n)
while n % i == 0
cnt += 1
n /= i
end while
end for
return cnt
end function
 
isAttractive = function(n)
if n < 1 then return false
factorCnt = countFactors(n)
return isPrime(factorCnt)
end function
 
numbers = []
for i in range(2, 120)
if isAttractive(i) then numbers.push(i)
end for
 
print numbers.join(", ")
</syntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (show (filter attractive [1..120]))]
 
attractive :: num->bool
attractive n = #factors (#factors n) = 1
 
factors :: num->[num]
factors = f 2
where f d n = [], if d>n
= d:f d (n div d), if n mod d=0
= f (d+1) n, otherwise</syntaxhighlight>
{{out}}
<pre>[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]</pre>
=={{header|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE AttractiveNumbers;
FROM InOut IMPORT WriteCard, WriteLn;
 
CONST
Max = 120;
 
VAR
n, col: CARDINAL;
Prime: ARRAY [1..Max] OF BOOLEAN;
 
PROCEDURE Sieve;
VAR i, j: CARDINAL;
BEGIN
Prime[1] := FALSE;
FOR i := 2 TO Max DO
Prime[i] := TRUE;
END;
 
FOR i := 2 TO Max DIV 2 DO
IF Prime[i] THEN
j := i*2;
WHILE j <= Max DO
Prime[j] := FALSE;
j := j + i;
END;
END;
END;
END Sieve;
 
PROCEDURE Factors(n: CARDINAL): CARDINAL;
VAR i, factors: CARDINAL;
BEGIN
factors := 0;
FOR i := 2 TO Max DO
IF i > n THEN
RETURN factors;
END;
IF Prime[i] THEN
WHILE n MOD i = 0 DO
n := n DIV i;
factors := factors + 1;
END;
END;
END;
RETURN factors;
END Factors;
 
BEGIN
Sieve();
col := 0;
FOR n := 2 TO Max DO
IF Prime[Factors(n)] THEN
WriteCard(n, 4);
col := col + 1;
IF col MOD 15 = 0 THEN
WriteLn();
END;
END;
END;
WriteLn();
END AttractiveNumbers.</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|Nanoquery}}==
{{trans|C}}
<langsyntaxhighlight Nanoquerylang="nanoquery">MAX = 120
 
def is_prime(n)
Line 1,807 ⟶ 2,766:
end
end
println</langsyntaxhighlight>
 
{{out}}
Line 1,820 ⟶ 2,779:
The ''factor'' function returns a list of the prime factors of an integer with repetition,
e. g. (factor 12) is (2 2 3).
<syntaxhighlight lang="newlisp">
<lang NewLisp>
(define (prime? n)
(= (length (factor n)) 1))
Line 1,827 ⟶ 2,786:
;
(filter attractive? (sequence 2 120))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,837 ⟶ 2,796:
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
const MAX = 120
Line 1,892 ⟶ 2,851:
 
main()
</syntaxhighlight>
</lang>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 1,899 ⟶ 2,858:
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</pre>
 
=={{header|Objeck}}==
{{trans|Java}}
 
<syntaxhighlight lang="objeck">class AttractiveNumber {
function : Main(args : String[]) ~ Nil {
max := 120;
"The attractive numbers up to and including {$max} are:"->PrintLine();
count := 0;
for(i := 1; i <= max; i += 1;) {
n := CountPrimeFactors(i);
if(IsPrime(n)) {
" {$i}"->Print();
if(++count % 20 = 0) {
""->PrintLine();
};
};
};
 
""->PrintLine();
}
function : IsPrime(n : Int) ~ Bool {
if(n < 2) {
return false;
};
 
if(n % 2 = 0) {
return n = 2;
};
 
if(n % 3 = 0) {
return n = 3;
};
 
d := 5;
while(d *d <= n) {
if(n % d = 0) {
return false;
};
d += 2;
if(n % d = 0) {
return false;
};
d += 4;
};
 
return true;
}
function : CountPrimeFactors(n : Int) ~ Int {
if(n = 1) {
return 0;
};
 
if(IsPrime(n)) {
return 1;
};
 
count := 0;
f := 2;
while(true) {
if(n % f = 0) {
count++;
n /= f;
if(n = 1) { return count; };
if(IsPrime(n)) { f := n; };
}
else if(f >= 3) {
f += 2;
}
else {
f := 3;
};
};
 
return -1;
}
}</syntaxhighlight>
 
{{output}}
<pre>
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
</pre>
 
=={{header|Odin}}==
<syntaxhighlight lang="Go">
import "core:fmt"
main :: proc() {
const_max :: 120
fmt.println("\nAttractive numbers up to and including", const_max, "are: ")
count := 0
for i in 1 ..= const_max {
n := countPrimeFactors(i)
if isPrime(n) {
fmt.print(i, " ")
count += 1
if count % 20 == 0 {
fmt.println()
}
}
}
fmt.println()
}
/* definitions */
isPrime :: proc(n: int) -> bool {
switch {
case n < 2:
return false
case n % 2 == 0:
return n == 2
case n % 3 == 0:
return n == 3
case:
d := 5
for d * d <= n {
if n % d == 0 {
return false
}
d += 2
if n % d == 0 {
return false
}
d += 4
}
return true
}
}
countPrimeFactors :: proc(n: int) -> int {
n := n
switch {
case n == 1:
return 0
case isPrime(n):
return 1
case:
count, f := 0, 2
for {
if n % f == 0 {
count += 1
n /= f
if n == 1 {
return count
}
if isPrime(n) {
f = n
}
} else if f >= 3 {
f += 2
} else {
f = 3
}
}
return count
}
}
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Pascal}}==
{{works with|Free Pascal}}
same procedure as in http://rosettacode.org/wiki/Abundant,_deficient_and_perfect_number_classifications
<langsyntaxhighlight lang="pascal">program AttractiveNumbers;
{ numbers with count of factors = prime
* using modified sieve of erathosthes
Line 2,112 ⟶ 3,242:
writeln('time counting : ',T*86400 :8:3,' s');
writeln('time total : ',(now-T0)*86400 :8:3,' s');
end.</langsyntaxhighlight>
{{out}}
<pre> 1 with many factors 0.000 s
Line 2,145 ⟶ 3,275:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use ntheory <is_prime factor>;
 
is_prime +factor $_ and print "$_ " for 1..120;</langsyntaxhighlight>
{{out}}
<pre>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</pre>
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">attractive</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 2,171 ⟶ 3,301:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %,d attractive numbers up to %,d (%s)\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">l</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,186 ⟶ 3,316:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
function isPrime ($x) {
if ($x < 2) return false;
Line 2,216 ⟶ 3,346:
if (isPrime(countFacs($i))) echo $i."&ensp;";
}
?></langsyntaxhighlight>
{{out}}
<pre>4 6 8 10 12 14 15 18 20 21 22 26 27 28 30 32 33 34 35 36 38 39 42 44 45 46 48 50 51 52 55 57 58 62 63 65 66 68 69 70 74 75 76 77 78 80 82 85 86 87 91 92 93 94 95 98 99 100 102 105 106 108 110 111 112 114 115 116 117 118 119 120</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">attractive: procedure options(main);
%replace MAX by 120;
declare prime(1:MAX) bit(1);
sieve: procedure;
declare (i, j, sqm) fixed;
prime(1) = 0;
do i=2 to MAX; prime(i) = '1'b; end;
sqm = sqrt(MAX);
do i=2 to sqm;
if prime(i) then do j=i*2 to MAX by i;
prime(j) = '0'b;
end;
end;
end sieve;
factors: procedure(nn) returns(fixed);
declare (f, i, n, nn) fixed;
n = nn;
f = 0;
do i=2 to n;
if prime(i) then do while(mod(n,i) = 0);
f = f+1;
n = n/i;
end;
end;
return(f);
end factors;
attractive: procedure(n) returns(bit(1));
declare n fixed;
return(prime(factors(n)));
end attractive;
declare (i, col) fixed;
i = 0;
col = 0;
call sieve();
do i=2 to MAX;
if attractive(i) then do;
put edit(i) (F(4));
col = col + 1;
if mod(col,18) = 0 then put skip;
end;
end;
end attractive;</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|PL/M}}==
<syntaxhighlight lang="pli">100H:
BDOS: PROCEDURE (F, ARG); DECLARE F BYTE, ARG ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT;
PUT$CHAR: PROCEDURE (CH); DECLARE CH BYTE; CALL BDOS(2,CH); END PUT$CHAR;
 
DECLARE MAXIMUM LITERALLY '120';
 
PRINT4: PROCEDURE (N);
DECLARE (N, MAGN, Z) BYTE;
CALL PUT$CHAR(' ');
MAGN = 100;
Z = 0;
DO WHILE MAGN > 0;
IF NOT Z AND N < MAGN THEN
CALL PUT$CHAR(' ');
ELSE DO;
CALL PUT$CHAR('0' + N/MAGN);
N = N MOD MAGN;
Z = 1;
END;
MAGN = MAGN/10;
END;
END PRINT4;
 
NEW$LINE: PROCEDURE;
CALL PUT$CHAR(13);
CALL PUT$CHAR(10);
END NEW$LINE;
 
SIEVE: PROCEDURE (MAX, PRIME);
DECLARE PRIME ADDRESS;
DECLARE (I, J, MAX, P BASED PRIME) BYTE;
P(0)=0;
P(1)=0;
DO I=2 TO MAX; P(I)=1; END;
DO I=2 TO SHR(MAX,1);
IF P(I) THEN DO J=SHL(I,1) TO MAX BY I;
P(J) = 0;
END;
END;
END SIEVE;
 
FACTORS: PROCEDURE (N, MAX, PRIME) BYTE;
DECLARE PRIME ADDRESS;
DECLARE (I, J, N, MAX, F, P BASED PRIME) BYTE;
F = 0;
DO I=2 TO MAX;
IF P(I) THEN DO WHILE N MOD I = 0;
F = F + 1;
N = N / I;
END;
END;
RETURN F;
END FACTORS;
 
ATTRACTIVE: PROCEDURE(N, MAX, PRIME) BYTE;
DECLARE PRIME ADDRESS;
DECLARE (N, MAX, P BASED PRIME) BYTE;
RETURN P(FACTORS(N, MAX, PRIME));
END ATTRACTIVE;
 
DECLARE (I, COL) BYTE INITIAL (0, 0);
CALL SIEVE(MAXIMUM, .MEMORY);
DO I=2 TO MAXIMUM;
IF ATTRACTIVE(I, MAXIMUM, .MEMORY) THEN DO;
CALL PRINT4(I);
COL = COL + 1;
IF COL MOD 18 = 0 THEN CALL NEW$LINE;
END;
END;
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">prime_factors(N, Factors):-
S is sqrt(N),
prime_factors(N, Factors, S, 2).
Line 2,279 ⟶ 3,545:
 
main:-
print_attractive_numbers(1, 120, 1).</langsyntaxhighlight>
 
{{out}}
Line 2,290 ⟶ 3,556:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">#MAX=120
Dim prime.b(#MAX)
FillMemory(@prime(),#MAX,#True,#PB_Byte) : FillMemory(@prime(),2,#False,#PB_Byte)
Line 2,317 ⟶ 3,583:
EndIf
Next
PrintN("") : Input()</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 2,328 ⟶ 3,594:
===Procedural===
{{Works with|Python|2.7.12}}
<langsyntaxhighlight Pythonlang="python">from sympy import sieve # library for primes
 
def get_pfct(n):
Line 2,352 ⟶ 3,618:
for i,each in enumerate(pool):
if each in primes:
print i,</langsyntaxhighlight>
{{out}}
<pre>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</pre>
Line 2,359 ⟶ 3,625:
Without importing a primes library – at this scale a light and visible implementation is more than enough, and provides more material for comparison.
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Attractive numbers'''
 
from itertools import chain, count, takewhile
Line 2,466 ⟶ 3,732:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27
Line 2,478 ⟶ 3,744:
<code>primefactors</code> is defined at [https://rosettacode.org/wiki/Prime_decomposition#Quackery Prime decomposition].
 
<langsyntaxhighlight Quackerylang="quackery"> [ primefactors size
primefactors size 1 = ] is attractive ( n --> b )
 
120 times
[ i^ 1+ attractive if
[ i^ 1+ echo sp ] ]</langsyntaxhighlight>
 
{{out}}
Line 2,490 ⟶ 3,756:
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
is_prime <- function(num) {
if (num < 2) return(FALSE)
Line 2,533 ⟶ 3,799:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,541 ⟶ 3,807:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">#lang racket
(require math/number-theory)
(define attractive? (compose1 prime? prime-omega))
(filter attractive? (range 1 121))</langsyntaxhighlight>
 
{{out}}
Line 2,556 ⟶ 3,822:
This algorithm is concise but not really well suited to finding large quantities of consecutive attractive numbers. It works, but isn't especially speedy. More than a hundred thousand or so gets tedious. There are other, much faster (though more verbose) algorithms that ''could'' be used. This algorithm '''is''' well suited to finding '''arbitrary''' attractive numbers though.
 
<syntaxhighlight lang="raku" perl6line>use Lingua::EN::Numbers;
use ntheory:from<Perl5> <factor is_prime>;
 
Line 2,571 ⟶ 3,837:
put "\nCount of attractive numbers from {comma $a} to {comma $b}:\n" ~
comma count $a, $b
}</langsyntaxhighlight>
{{out}}
<pre>Attractive numbers from 1 to 120:
Line 2,600 ⟶ 3,866:
 
If the argument for the program is negative, &nbsp; only a &nbsp; ''count'' &nbsp; of attractive numbers up to and including &nbsp; '''│N│''' &nbsp; is shown.
<langsyntaxhighlight lang="rexx">/*REXX program finds and shows lists (or counts) attractive numbers up to a specified N.*/
parse arg N . /*get optional argument from the C.L. */
if N=='' | N=="," then N= 120 /*Not specified? Then use the default.*/
Line 2,647 ⟶ 3,913:
do j=3 by 2 until p==n; do k=3 by 2 until k*k>j; if j//k==0 then iterate j
end /*k*/; @.j = 1; p= p + 1
end /*j*/; return /* [↑] generate N primes. */</langsyntaxhighlight>
This REXX program makes use of &nbsp; '''LINESIZE''' &nbsp; REXX program (or BIF) which is used to determine the
screen width (or linesize) of the terminal (console).
Line 2,677 ⟶ 3,943:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project: Attractive Numbers
 
Line 2,716 ⟶ 3,982:
next
return 1
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,747 ⟶ 4,013:
119 = [7*17] - 2 is prime
120 = [2*2*2*3*5] - 5 is prime
</pre>
 
=={{header|RPL}}==
{{works with|HP|49g}}
≪ { }
2 120 '''FOR''' n
FACTORS 0
2 3 PICK SIZE '''FOR''' j OVER j GET + 2 '''STEP'''
NIP
'''IF''' ISPRIME? '''THEN''' n + '''END'''
'''NEXT'''
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
{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}
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require "prime"
p (1..120).select{|n| n.prime_division.sum(&:last).prime? }
</syntaxhighlight>
</lang>
{{out}}<pre>[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]
</pre>
Line 2,759 ⟶ 4,040:
=={{header|Rust}}==
Uses [https://crates.io/crates/primal primal]
<langsyntaxhighlight lang="rust">use primal::Primes;
 
const MAX: u64 = 120;
Line 2,801 ⟶ 4,082:
}
println!("The attractive numbers up to and including 120 are\n{:?}", output);
}</langsyntaxhighlight>
{{out}}<pre>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]</pre>
Line 2,807 ⟶ 4,088:
=={{header|Scala}}==
{{Out}}Best seen in running your browser either by [https://scalafiddle.io/sf/23oE3SQ/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/U0QUQu0uTT24vbDEHU1c0Q Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">object AttractiveNumbers extends App {
private val max = 120
private var count = 0
Line 2,834 ⟶ 4,115:
.groupBy { case (_, index) => index / 20 }
.foreach { case (_, row) => println(row.map(_._1).mkString) }
}</langsyntaxhighlight>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program attractive_numbers;
numbers := [n in [2..120] | attractive(n)];
printtab(numbers, 20, 3);
 
proc printtab(list, cols, width);
lines := [list(k..cols+k-1) : k in [1, cols+1..#list]];
loop for line in lines do
print(+/[lpad(str item, width+1) : item in line]);
end loop;
end proc;
 
proc attractive(n);
return #factorize(#factorize(n)) = 1;
end proc;
 
proc factorize(n);
factors := [];
d := 2;
loop until d > n do
loop while n mod d = 0 do
factors with:= d;
n div:= d;
end loop;
d +:= 1;
end loop;
return factors;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre> 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</pre>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_attractive(n) {
n.bigomega.is_prime
}
 
1..120 -> grep(is_attractive).say</langsyntaxhighlight>
{{out}}
<pre>[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]</pre>
Line 2,847 ⟶ 4,163:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension BinaryInteger {
Line 2,897 ⟶ 4,213:
let attractive = Array((1...).lazy.filter({ $0.isAttractive }).prefix(while: { $0 <= 120 }))
 
print("Attractive numbers up to and including 120: \(attractive)")</langsyntaxhighlight>
 
{{out}}
Line 2,904 ⟶ 4,220:
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">proc isPrime {n} {
if {$n < 2} {
return 0
Line 2,956 ⟶ 4,272:
}
}
showRange 1 120</langsyntaxhighlight>
{{out}}
<pre>Attractive numbers in range 1..120 are:
Line 2,967 ⟶ 4,283:
=={{header|Vala}}==
{{trans|D}}
<langsyntaxhighlight lang="vala">bool is_prime(int n) {
var d = 5;
if (n < 2) return false;
Line 3,015 ⟶ 4,331:
}
stdout.printf("\n");
}</langsyntaxhighlight>
 
{{out}}
Line 3,027 ⟶ 4,343:
 
=={{header|VBA}}==
<langsyntaxhighlight VBAlang="vba">Option Explicit
 
Public Sub AttractiveNumbers()
Line 3,105 ⟶ 4,421:
 
Finish2:
End Function</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|D}}
<langsyntaxhighlight lang="vbnet">Module Module1
Const MAX = 120
 
Line 3,164 ⟶ 4,480:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 3,171 ⟶ 4,487:
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</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn is_prime(n int) bool {
if n < 2 {
return false
}
else if n%2 == 0 {
return n == 2
}
else if n%3 == 0 {
return n == 3
}
else {
mut d := 5
for d*d <= n {
if n%d == 0 {
return false
}
d += 2
if n%d == 0 {
return false
}
d += 4
}
return true
}
}
 
fn count_prime_factors(n int) int {
mut nn := n
if n == 1 {
return 0
}
else if is_prime(nn) {
return 1
}
else {
mut count, mut f := 0, 2
for {
if nn%f == 0 {
count++
nn /= f
if nn == 1{
return count
}
if is_prime(nn) {
f = nn
}
} else if f >= 3{
f += 2
} else {
f = 3
}
}
return count
}
}
 
fn main() {
max := 120
println('The attractive numbers up to and including $max are:')
mut count := 0
for i in 1 .. max+1 {
n := count_prime_factors(i)
if is_prime(n) {
print('${i:4}')
count++
if count%20 == 0 {
println('')
}
}
}
}</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int
var max = 120
Line 3,184 ⟶ 4,583:
var n = Int.primeFactors(i).count
if (Int.isPrime(n)) {
SystemFmt.write(Fmt.d(4"$4d", i))
count = count + 1
if (count%20 == 0) System.print()
}
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 3,198 ⟶ 4,597:
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
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
if (N&1) = 0 then \even >2\ return false;
for I:= 3 to sqrt(N) do
[if rem(N/I) = 0 then return false;
I:= I+1;
];
return true;
];
 
func Factors(N); \Return number of factors for N
int N, Cnt, F;
[Cnt:= 0;
F:= 2;
repeat if rem(N/F) = 0 then
[Cnt:= Cnt+1;
N:= N/F;
]
else F:= F+1;
until F > N;
return Cnt;
];
 
int C, N;
[C:= 0;
for N:= 4 to 120 do
if IsPrime(Factors(N)) then
[IntOut(0, N);
C:= C+1;
if rem(C/10) then ChOut(0, 9\tab\) else CrLf(0);
];
]</syntaxhighlight>
 
{{out}}
<pre>
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
</pre>
 
Line 3,203 ⟶ 4,649:
Using GMP (GNU Multiple Precision Arithmetic Library, probabilistic
primes) because it is easy and fast to test for primeness.
<langsyntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
fcn attractiveNumber(n){ BI(primeFactors(n).len()).probablyPrime() }
 
println("The attractive numbers up to and including 120 are:");
[1..120].filter(attractiveNumber)
.apply("%4d".fmt).pump(Void,T(Void.Read,19,False),"println");</langsyntaxhighlight>
Using [[Prime decomposition#zkl]]
<langsyntaxhighlight lang="zkl">fcn primeFactors(n){ // Return a list of factors of n
acc:=fcn(n,k,acc,maxD){ // k is 2,3,5,7,9,... not optimum
if(n==1 or k>maxD) acc.close();
Line 3,222 ⟶ 4,668:
if(n!=m) acc.append(n/m); // opps, missed last factor
else acc;
}</langsyntaxhighlight>
{{out}}
<pre>
2,115

edits