Attractive numbers: Difference between revisions

m
syntax highlighting fixup automation
(Frink)
m (syntax highlighting fixup automation)
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}}
Line 174:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<langsyntaxhighlight lang=Action!>INCLUDE "H6:SIEVE.ACT"
 
BYTE FUNC IsAttractive(BYTE n BYTE ARRAY primes)
Line 221:
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:
=={{header|Ada}}==
{{trans|C}}
<langsyntaxhighlight lang=Ada>with Ada.Text_IO;
 
procedure Attractive_Numbers is
Line 302:
begin
Show_Attractive (Max => 120);
end Attractive_Numbers;</langsyntaxhighlight>
 
{{out}}
Line 313:
=={{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:
OD;
print( ( newline, "Found ", whole( a count, 0 ), " attractive numbers", newline ) )
END</langsyntaxhighlight>
{{out}}
<pre>
Line 355:
 
=={{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:
for i := 2 until maxNumber do if s( countPrimeFactors( i, s ) ) then writeon( i )
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 412:
 
=={{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:
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:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>attractive?: function [x] -> prime? size factors.prime x
 
print select 1..120 => attractive?</langsyntaxhighlight>
 
{{out}}
Line 478:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang=AutoHotkey>AttractiveNumbers(n){
c := prime_numbers(n).count()
if c = 1
Line 529:
ans.push(n)
return ans
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight lang=AutoHotkey>c:= 0
loop
{
Line 539:
}
MsgBox, 262144, ,% result
return</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32 33 34
Line 547:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f ATTRACTIVE_NUMBERS.AWK
# converted from C
Line 588:
return(1)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 596:
 
=={{header|BASIC}}==
<langsyntaxhighlight lang=basic>10 DEFINT A-Z
20 M=120
30 DIM C(M): C(0)=-1: C(1)=-1
Line 608:
110 NEXT
120 IF NOT C(C) THEN PRINT I,
130 NEXT</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10
Line 627:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang=bcpl>get "libhdr"
manifest $( MAXIMUM = 120 $)
 
Line 661:
$)
wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27 28 30 32
Line 671:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang=c>#include <stdio.h>
 
#define TRUE 1
Line 721:
printf("\n");
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 734:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang=csharp>using System;
 
namespace AttractiveNumbers {
Line 788:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 798:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang=cpp>#include <iostream>
#include <iomanip>
 
Line 846:
cout << endl;
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 858:
 
=={{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 898:
end
end
end start_up</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27
Line 906:
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 993:
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 1,002:
 
=={{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 1,023:
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 1,032:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang=Lisp>
(defun attractivep (n)
(primep (length (factors n))) )
Line 1,047:
(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 1,053:
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang=cowgol>include "cowgol.coh";
const MAXIMUM := 120;
 
Line 1,112:
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,122:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang=d>import std.stdio;
 
enum MAX = 120;
Line 1,172:
}
writeln;
}</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 1,183:
See [[#Pascal]].
=={{header|Draco}}==
<langsyntaxhighlight lang=draco>/* Sieve of Eratosthenes */
proc nonrec sieve([*] bool prime) void:
word p, c, max;
Line 1,231:
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,240:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang=Fsharp>// attractive_numbers.fsx
// taken from Primality by trial division
let rec primes =
Line 1,277:
|> List.filter is_attractive
|> List.iteri print_iter
</syntaxhighlight>
</lang>
{{out}}
<pre>>dotnet fsi attractive_numbers.fsx
Line 1,292:
=={{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,309:
=={{header|Fortran}}==
{{trans|C++}}
<langsyntaxhighlight lang=fortran>
program attractive_numbers
use iso_fortran_env, only: output_unit
Line 1,396:
end function count_prime_factors
end program attractive_numbers
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,409:
=={{header|FreeBASIC}}==
{{trans|D}}
<langsyntaxhighlight lang=freebasic>
Const limite = 120
 
Line 1,460:
Wend
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,471:
 
=={{header|Frink}}==
<langsyntaxhighlight lang=frink>println[select[2 to 120, {|x| !isPrime[x] and isPrime[length[factorFlat[x]]]}]]</langsyntaxhighlight>
{{out}}
<pre>
Line 1,479:
=={{header|Go}}==
Simple functions to test for primality and to count prime factors suffice here.
<langsyntaxhighlight lang=go>package main
 
import "fmt"
Line 1,550:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,563:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang=groovy>class AttractiveNumbers {
static boolean isPrime(int n) {
if (n < 2) return false
Line 1,606:
println()
}
}</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 1,615:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>import Data.Numbers.Primes
import Data.Bool (bool)
 
Line 1,623:
 
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,635:
 
main :: IO ()
main = print $ takeWhile (<= 120) attractiveNumbers</langsyntaxhighlight>
 
Or simply:
<langsyntaxhighlight lang=haskell>import Data.Numbers.Primes
 
attractiveNumbers :: [Integer]
Line 1,647:
 
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|J}}==
<syntaxhighlight lang=j>
<lang j>
echo (#~ (1 p: ])@#@q:) >:i.120
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,661:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang=javascript>(() => {
'use strict';
 
Line 1,880:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre> 4 6 8 9 10 12 14 15 18 20
Line 1,893:
=={{header|Java}}==
{{trans|C}}
<langsyntaxhighlight lang=java>public class Attractive {
 
static boolean is_prime(int n) {
Line 1,937:
System.out.println();
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,955:
* `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,965:
[range($m; $n+1) | select(is_attractive)];
printattractive(1; 120)</langsyntaxhighlight>
{{out}}
<pre>
Line 1,974:
 
=={{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,983:
printattractive(1, 120)
</langsyntaxhighlight>{{out}}
<pre>
The attractive numbers from 1 to 120 are:
Line 1,991:
=={{header|Kotlin}}==
{{trans|Go}}
<langsyntaxhighlight lang=scala>// Version 1.3.21
 
const val MAX = 120
Line 2,044:
}
println()
}</langsyntaxhighlight>
 
{{output}}
Line 2,056:
 
=={{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,281:
}
 
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,290:
 
=={{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,322:
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,331:
 
=={{header|Maple}}==
<langsyntaxhighlight lang=Maple>attractivenumbers := proc(n::posint)
local an, i;
an :=[]:
Line 2,340:
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 lang=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|Modula-2}}==
<langsyntaxhighlight lang=modula2>MODULE AttractiveNumbers;
FROM InOut IMPORT WriteCard, WriteLn;
 
Line 2,412:
END;
WriteLn();
END AttractiveNumbers.</langsyntaxhighlight>
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27
Line 2,422:
=={{header|Nanoquery}}==
{{trans|C}}
<langsyntaxhighlight lang=Nanoquery>MAX = 120
 
def is_prime(n)
Line 2,489:
end
end
println</langsyntaxhighlight>
 
{{out}}
Line 2,502:
The ''factor'' function returns a list of the prime factors of an integer with repetition,
e. g. (factor 12) is (2 2 3).
<langsyntaxhighlight lang=NewLisp>
(define (prime? n)
(= (length (factor n)) 1))
Line 2,509:
;
(filter attractive? (sequence 2 120))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,519:
=={{header|Nim}}==
{{trans|C}}
<langsyntaxhighlight lang=Nim>import strformat
 
const MAX = 120
Line 2,574:
 
main()
</syntaxhighlight>
</lang>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 2,585:
{{trans|Java}}
 
<langsyntaxhighlight lang=objeck>class AttractiveNumber {
function : Main(args : String[]) ~ Nil {
max := 120;
Line 2,661:
return -1;
}
}</langsyntaxhighlight>
 
{{output}}
Line 2,675:
{{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,884:
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,917:
=={{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 lang=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,943:
<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,958:
 
=={{header|PHP}}==
<langsyntaxhighlight lang=php><?php
function isPrime ($x) {
if ($x < 2) return false;
Line 2,988:
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 3,039:
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 3,048:
 
=={{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,120:
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,130:
=={{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,187:
 
main:-
print_attractive_numbers(1, 120, 1).</langsyntaxhighlight>
 
{{out}}
Line 3,198:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang=PureBasic>#MAX=120
Dim prime.b(#MAX)
FillMemory(@prime(),#MAX,#True,#PB_Byte) : FillMemory(@prime(),2,#False,#PB_Byte)
Line 3,225:
EndIf
Next
PrintN("") : Input()</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 3,236:
===Procedural===
{{Works with|Python|2.7.12}}
<langsyntaxhighlight lang=Python>from sympy import sieve # library for primes
 
def get_pfct(n):
Line 3,260:
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,267:
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,374:
# 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,386:
<code>primefactors</code> is defined at [https://rosettacode.org/wiki/Prime_decomposition#Quackery Prime decomposition].
 
<langsyntaxhighlight lang=Quackery> [ primefactors size
primefactors size 1 = ] is attractive ( n --> b )
 
120 times
[ i^ 1+ attractive if
[ i^ 1+ echo sp ] ]</langsyntaxhighlight>
 
{{out}}
Line 3,398:
 
=={{header|R}}==
<langsyntaxhighlight lang=rsplus>
is_prime <- function(num) {
if (num < 2) return(FALSE)
Line 3,441:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,449:
 
=={{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,464:
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.
 
<langsyntaxhighlight lang=perl6>use Lingua::EN::Numbers;
use ntheory:from<Perl5> <factor is_prime>;
 
Line 3,479:
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,508:
 
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,555:
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,585:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
# Project: Attractive Numbers
 
Line 3,624:
next
return 1
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,658:
 
=={{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,667:
=={{header|Rust}}==
Uses [https://crates.io/crates/primal primal]
<langsyntaxhighlight lang=rust>use primal::Primes;
 
const MAX: u64 = 120;
Line 3,709:
}
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,715:
=={{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 lang=Scala>object AttractiveNumbers extends App {
private val max = 120
private var count = 0
Line 3,742:
.groupBy { case (_, index) => index / 20 }
.foreach { case (_, row) => println(row.map(_._1).mkString) }
}</langsyntaxhighlight>
 
=={{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,755:
=={{header|Swift}}==
 
<langsyntaxhighlight lang=swift>import Foundation
 
extension BinaryInteger {
Line 3,805:
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,812:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang=Tcl>proc isPrime {n} {
if {$n < 2} {
return 0
Line 3,864:
}
}
showRange 1 120</langsyntaxhighlight>
{{out}}
<pre>Attractive numbers in range 1..120 are:
Line 3,875:
=={{header|Vala}}==
{{trans|D}}
<langsyntaxhighlight lang=vala>bool is_prime(int n) {
var d = 5;
if (n < 2) return false;
Line 3,923:
}
stdout.printf("\n");
}</langsyntaxhighlight>
 
{{out}}
Line 3,935:
 
=={{header|VBA}}==
<langsyntaxhighlight lang=VBA>Option Explicit
 
Public Sub AttractiveNumbers()
Line 4,013:
 
Finish2:
End Function</langsyntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{trans|D}}
<langsyntaxhighlight lang=vbnet>Module Module1
Const MAX = 120
 
Line 4,072:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 4,082:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang=vlang>fn is_prime(n int) bool {
if n < 2 {
return false
Line 4,152:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,166:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight lang=ecmascript>import "/fmt" for Fmt
import "/math" for Int
Line 4,180:
}
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 4,192:
 
=={{header|XPL0}}==
<langsyntaxhighlight lang=XPL0>func IsPrime(N); \Return 'true' if N is prime
int N, I;
[if N <= 2 then return N = 2;
Line 4,224:
if rem(C/10) then ChOut(0, 9\tab\) else CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
Line 4,241:
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,260:
if(n!=m) acc.append(n/m); // opps, missed last factor
else acc;
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits