Attractive numbers: Difference between revisions

Add ABC
No edit summary
(Add ABC)
 
(22 intermediate revisions by 17 users not shown)
Line 19:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F is_prime(n)
I n < 2
R 0B
Line 50:
r.append(L.index)
 
print(r.map(String).join(‘,’))</langsyntaxhighlight>
 
{{out}}
Line 59:
=={{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 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}}
<langsyntaxhighlight Actionlang="action!">INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC IsAttractive(BYTE n BYTE ARRAY primes)
Line 221 ⟶ 253:
FI
OD
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Attractive_numbers.png Screenshot from Atari 8-bit computer]
Line 236 ⟶ 268:
=={{header|Ada}}==
{{trans|C}}
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Attractive_Numbers is
Line 302 ⟶ 334:
begin
Show_Attractive (Max => 120);
end Attractive_Numbers;</langsyntaxhighlight>
 
{{out}}
Line 313 ⟶ 345:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<langsyntaxhighlight lang="algol68">BEGIN # find some attractive numbers - numbers whose prime factor counts are #
# prime, n must be > 1 #
PR read "primes.incl.a68" PR
Line 343 ⟶ 375:
OD;
print( ( newline, "Found ", whole( a count, 0 ), " attractive numbers", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 355 ⟶ 387:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">% find some attractive numbers - numbers whose prime factor count is prime %
begin
% implements the sieve of Eratosthenes %
Line 405 ⟶ 437:
for i := 2 until maxNumber do if s( countPrimeFactors( i, s ) ) then writeon( i )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 412 ⟶ 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 460 ⟶ 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 469 ⟶ 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 519 ⟶ 620:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 527 ⟶ 628:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang="basic">10 DEFINT A-Z
20 M=120
30 DIM C(M): C(0)=-1: C(1)=-1
Line 539 ⟶ 640:
110 NEXT
120 IF NOT C(C) THEN PRINT I,
130 NEXT</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10
Line 558 ⟶ 659:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
manifest $( MAXIMUM = 120 $)
 
Line 592 ⟶ 693:
$)
wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32
Line 602 ⟶ 703:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
#define TRUE 1
Line 652 ⟶ 753:
printf("\n");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 665 ⟶ 766:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace AttractiveNumbers {
Line 719 ⟶ 820:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 729 ⟶ 830:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <iomanip>
 
Line 777 ⟶ 878:
cout << endl;
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 789 ⟶ 890:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">sieve = proc (max: int) returns (array[bool])
prime: array[bool] := array[bool]$fill(1,max,true)
prime[1] := false
Line 829 ⟶ 930:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27
Line 837 ⟶ 938:
102 105 106 108 110 111 112 114 115 116 117 118 119 120</pre>
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. ATTRACTIVE-NUMBERS.
 
Line 924 ⟶ 1,025:
GO TO SET-COMPOSITES-LOOP.
DONE.
EXIT.</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32
Line 933 ⟶ 1,034:
 
=={{header|Comal}}==
<langsyntaxhighlight lang="comal">0010 FUNC factors#(n#) CLOSED
0020 count#:=0
0030 WHILE n# MOD 2=0 DO n#:=n# DIV 2;count#:+1
Line 954 ⟶ 1,055:
0200 ENDFOR i#
0210 PRINT
0220 END</langsyntaxhighlight>
{{out}}
<pre>4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32
Line 963 ⟶ 1,064:
 
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">
<lang Lisp>
(defun attractivep (n)
(primep (length (factors n))) )
Line 978 ⟶ 1,079:
(cond ((> d max-d) (return (list n))) ; n is prime
((zerop (rem n d)) (return (cons d (factors (truncate n d)))))))))
</syntaxhighlight>
</lang>
{{out}}
<pre>(dotimes (i 121) (when (attractivep i) (princ i) (princ " ")))
Line 984 ⟶ 1,085:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
const MAXIMUM := 120;
 
Line 1,043 ⟶ 1,144:
cand := cand + 1;
end loop;
print_nl();</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32
Line 1,050 ⟶ 1,151:
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 1,103 ⟶ 1,255:
}
writeln;
}</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 1,114 ⟶ 1,266:
See [[#Pascal]].
=={{header|Draco}}==
<langsyntaxhighlight lang="draco">/* Sieve of Eratosthenes */
proc nonrec sieve([*] bool prime) void:
word p, c, max;
Line 1,162 ⟶ 1,314:
fi
od
corp</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32
Line 1,169 ⟶ 1,321:
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 1,208 ⟶ 1,396:
|> List.filter is_attractive
|> List.iteri print_iter
</syntaxhighlight>
</lang>
{{out}}
<pre>>dotnet fsi attractive_numbers.fsx
Line 1,223 ⟶ 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 1,240 ⟶ 1,428:
=={{header|Fortran}}==
{{trans|C++}}
<langsyntaxhighlight lang="fortran">
program attractive_numbers
use iso_fortran_env, only: output_unit
Line 1,327 ⟶ 1,515:
end function count_prime_factors
end program attractive_numbers
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,340 ⟶ 1,528:
=={{header|FreeBASIC}}==
{{trans|D}}
<langsyntaxhighlight lang="freebasic">
Const limite = 120
 
Line 1,391 ⟶ 1,579:
Wend
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,400 ⟶ 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 1,474 ⟶ 1,737:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,487 ⟶ 1,750:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class AttractiveNumbers {
static boolean isPrime(int n) {
if (n < 2) return false
Line 1,530 ⟶ 1,793:
println()
}
}</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 1,539 ⟶ 1,802:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes
import Data.Bool (bool)
 
Line 1,547 ⟶ 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,559 ⟶ 1,822:
 
main :: IO ()
main = print $ takeWhile (<= 120) attractiveNumbers</langsyntaxhighlight>
 
Or simply:
<langsyntaxhighlight lang="haskell">import Data.Numbers.Primes
 
attractiveNumbers :: [Integer]
Line 1,571 ⟶ 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,585 ⟶ 1,868:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 1,804 ⟶ 2,087:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre> 4 6 8 9 10 12 14 15 18 20
Line 1,817 ⟶ 2,100:
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang="java">public class Attractive {
 
static boolean is_prime(int n) {
Line 1,861 ⟶ 2,144:
System.out.println();
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,879 ⟶ 2,162:
* `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">
<lang jq>
def count(s): reduce s as $x (null; .+1);
 
Line 1,889 ⟶ 2,172:
[range($m; $n+1) | select(is_attractive)];
printattractive(1; 120)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,898 ⟶ 2,181:
 
=={{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,907 ⟶ 2,190:
printattractive(1, 120)
</langsyntaxhighlight>{{out}}
<pre>
The attractive numbers from 1 to 120 are:
Line 1,915 ⟶ 2,198:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang="scala">// Version 1.3.21
 
const val MAX = 120
Line 1,968 ⟶ 2,251:
}
println()
}</langsyntaxhighlight>
 
{{output}}
Line 1,980 ⟶ 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 2,205 ⟶ 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 2,214 ⟶ 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 2,246 ⟶ 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 2,255 ⟶ 2,538:
 
=={{header|Maple}}==
<langsyntaxhighlight Maplelang="maple">attractivenumbers := proc(n::posint)
local an, i;
an :=[]:
Line 2,264 ⟶ 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}}==
<langsyntaxhighlight lang="modula2">MODULE AttractiveNumbers;
FROM InOut IMPORT WriteCard, WriteLn;
 
Line 2,336 ⟶ 2,689:
END;
WriteLn();
END AttractiveNumbers.</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27
Line 2,346 ⟶ 2,699:
=={{header|Nanoquery}}==
{{trans|C}}
<langsyntaxhighlight Nanoquerylang="nanoquery">MAX = 120
 
def is_prime(n)
Line 2,413 ⟶ 2,766:
end
end
println</langsyntaxhighlight>
 
{{out}}
Line 2,426 ⟶ 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 2,433 ⟶ 2,786:
;
(filter attractive? (sequence 2 120))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,443 ⟶ 2,796:
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight Nimlang="nim">import strformat
 
const MAX = 120
Line 2,498 ⟶ 2,851:
 
main()
</syntaxhighlight>
</lang>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 2,509 ⟶ 2,862:
{{trans|Java}}
 
<langsyntaxhighlight lang="objeck">class AttractiveNumber {
function : Main(args : String[]) ~ Nil {
max := 120;
Line 2,585 ⟶ 2,938:
return -1;
}
}</langsyntaxhighlight>
 
{{output}}
Line 2,594 ⟶ 2,947:
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>
 
Line 2,599 ⟶ 3,033:
{{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,808 ⟶ 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,841 ⟶ 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,867 ⟶ 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,882 ⟶ 3,316:
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php"><?php
function isPrime ($x) {
if ($x < 2) return false;
Line 2,912 ⟶ 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}}==
<langsyntaxhighlight lang="pli">attractive: procedure options(main);
%replace MAX by 120;
declare prime(1:MAX) bit(1);
Line 2,963 ⟶ 3,397:
end;
end;
end attractive;</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32
Line 2,972 ⟶ 3,406:
 
=={{header|PL/M}}==
<langsyntaxhighlight 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;
Line 3,044 ⟶ 3,478:
END;
CALL EXIT;
EOF</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32
Line 3,054 ⟶ 3,488:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<langsyntaxhighlight lang="prolog">prime_factors(N, Factors):-
S is sqrt(N),
prime_factors(N, Factors, S, 2).
Line 3,111 ⟶ 3,545:
 
main:-
print_attractive_numbers(1, 120, 1).</langsyntaxhighlight>
 
{{out}}
Line 3,122 ⟶ 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 3,149 ⟶ 3,583:
EndIf
Next
PrintN("") : Input()</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 3,160 ⟶ 3,594:
===Procedural===
{{Works with|Python|2.7.12}}
<langsyntaxhighlight Pythonlang="python">from sympy import sieve # library for primes
 
def get_pfct(n):
Line 3,184 ⟶ 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 3,191 ⟶ 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 3,298 ⟶ 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 3,310 ⟶ 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 3,322 ⟶ 3,756:
 
=={{header|R}}==
<langsyntaxhighlight lang="rsplus">
is_prime <- function(num) {
if (num < 2) return(FALSE)
Line 3,365 ⟶ 3,799:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,373 ⟶ 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 3,388 ⟶ 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 3,403 ⟶ 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 3,432 ⟶ 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 3,479 ⟶ 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 3,509 ⟶ 3,943:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project: Attractive Numbers
 
Line 3,548 ⟶ 3,982:
next
return 1
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,579 ⟶ 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 3,591 ⟶ 4,040:
=={{header|Rust}}==
Uses [https://crates.io/crates/primal primal]
<langsyntaxhighlight lang="rust">use primal::Primes;
 
const MAX: u64 = 120;
Line 3,633 ⟶ 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 3,639 ⟶ 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 3,666 ⟶ 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 3,679 ⟶ 4,163:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension BinaryInteger {
Line 3,729 ⟶ 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 3,736 ⟶ 4,220:
 
=={{header|Tcl}}==
<langsyntaxhighlight Tcllang="tcl">proc isPrime {n} {
if {$n < 2} {
return 0
Line 3,788 ⟶ 4,272:
}
}
showRange 1 120</langsyntaxhighlight>
{{out}}
<pre>Attractive numbers in range 1..120 are:
Line 3,799 ⟶ 4,283:
=={{header|Vala}}==
{{trans|D}}
<langsyntaxhighlight lang="vala">bool is_prime(int n) {
var d = 5;
if (n < 2) return false;
Line 3,847 ⟶ 4,331:
}
stdout.printf("\n");
}</langsyntaxhighlight>
 
{{out}}
Line 3,859 ⟶ 4,343:
 
=={{header|VBA}}==
<langsyntaxhighlight VBAlang="vba">Option Explicit
 
Public Sub AttractiveNumbers()
Line 3,937 ⟶ 4,421:
 
Finish2:
End Function</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|D}}
<langsyntaxhighlight lang="vbnet">Module Module1
Const MAX = 120
 
Line 3,996 ⟶ 4,480:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 4,004 ⟶ 4,488:
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
Line 4,076 ⟶ 4,560:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,090 ⟶ 4,574:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int
var max = 120
Line 4,099 ⟶ 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 4,116 ⟶ 4,600:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 4,148 ⟶ 4,632:
if rem(C/10) then ChOut(0, 9\tab\) else CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
Line 4,165 ⟶ 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 4,184 ⟶ 4,668:
if(n!=m) acc.append(n/m); // opps, missed last factor
else acc;
}</langsyntaxhighlight>
{{out}}
<pre>
2,094

edits