Attractive numbers: Difference between revisions
m
syntax highlighting fixup automation
(Frink) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 19:
{{trans|Python}}
<
I n < 2
R 0B
Line 50:
r.append(L.index)
print(r.map(String).join(‘,’))</
{{out}}
Line 59:
=={{header|8080 Assembly}}==
<
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</
{{out}}
Line 174:
=={{header|Action!}}==
{{libheader|Action! Sieve of Eratosthenes}}
<
BYTE FUNC IsAttractive(BYTE n BYTE ARRAY primes)
Line 221:
FI
OD
RETURN</
{{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}}
<
procedure Attractive_Numbers is
Line 302:
begin
Show_Attractive (Max => 120);
end Attractive_Numbers;</
{{out}}
Line 313:
=={{header|ALGOL 68}}==
{{libheader|ALGOL 68-primes}}
<
# 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</
{{out}}
<pre>
Line 355:
=={{header|ALGOL W}}==
<
begin
% implements the sieve of Eratosthenes %
Line 405:
for i := 2 until maxNumber do if s( countPrimeFactors( i, s ) ) then writeon( i )
end
end.</
{{out}}
<pre>
Line 412:
=={{header|AppleScript}}==
<
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</
{{output}}
<
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}}==
<
print select 1..120 => attractive?</
{{out}}
Line 478:
=={{header|AutoHotkey}}==
<
c := prime_numbers(n).count()
if c = 1
Line 529:
ans.push(n)
return ans
}</
Examples:<
loop
{
Line 539:
}
MsgBox, 262144, ,% result
return</
{{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}}==
<
# syntax: GAWK -f ATTRACTIVE_NUMBERS.AWK
# converted from C
Line 588:
return(1)
}
</syntaxhighlight>
{{out}}
<pre>
Line 596:
=={{header|BASIC}}==
<
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</
{{out}}
<pre> 4 6 8 9 10
Line 627:
=={{header|BCPL}}==
<
manifest $( MAXIMUM = 120 $)
Line 661:
$)
wrch('*N')
$)</
{{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}}
<
#define TRUE 1
Line 721:
printf("\n");
return 0;
}</
{{out}}
Line 734:
=={{header|C sharp|C#}}==
{{trans|D}}
<
namespace AttractiveNumbers {
Line 788:
}
}
}</
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 798:
=={{header|C++}}==
{{trans|C}}
<
#include <iomanip>
Line 846:
cout << endl;
return 0;
}</
{{out}}
Line 858:
=={{header|CLU}}==
<
prime: array[bool] := array[bool]$fill(1,max,true)
prime[1] := false
Line 898:
end
end
end start_up</
{{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}}==
<
PROGRAM-ID. ATTRACTIVE-NUMBERS.
Line 993:
GO TO SET-COMPOSITES-LOOP.
DONE.
EXIT.</
{{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}}==
<
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</
{{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}}==
<
(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>
{{out}}
<pre>(dotimes (i 121) (when (attractivep i) (princ i) (princ " ")))
Line 1,053:
=={{header|Cowgol}}==
<
const MAXIMUM := 120;
Line 1,112:
cand := cand + 1;
end loop;
print_nl();</
{{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++}}
<
enum MAX = 120;
Line 1,172:
}
writeln;
}</
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 1,183:
See [[#Pascal]].
=={{header|Draco}}==
<
proc nonrec sieve([*] bool prime) void:
word p, c, max;
Line 1,231:
fi
od
corp</
{{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#}}==
<
// taken from Primality by trial division
let rec primes =
Line 1,277:
|> List.filter is_attractive
|> List.iteri print_iter
</syntaxhighlight>
{{out}}
<pre>>dotnet fsi attractive_numbers.fsx
Line 1,292:
=={{header|Factor}}==
{{works with|Factor|0.99}}
<
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</
{{out}}
<pre>
Line 1,309:
=={{header|Fortran}}==
{{trans|C++}}
<
program attractive_numbers
use iso_fortran_env, only: output_unit
Line 1,396:
end function count_prime_factors
end program attractive_numbers
</syntaxhighlight>
{{out}}
Line 1,409:
=={{header|FreeBASIC}}==
{{trans|D}}
<
Const limite = 120
Line 1,460:
Wend
End
</syntaxhighlight>
{{out}}
<pre>
Line 1,471:
=={{header|Frink}}==
<
{{out}}
<pre>
Line 1,479:
=={{header|Go}}==
Simple functions to test for primality and to count prime factors suffice here.
<
import "fmt"
Line 1,550:
}
fmt.Println()
}</
{{out}}
Line 1,563:
=={{header|Groovy}}==
{{trans|Java}}
<
static boolean isPrime(int n) {
if (n < 2) return false
Line 1,606:
println()
}
}</
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 1,615:
=={{header|Haskell}}==
<
import Data.Bool (bool)
Line 1,623:
main :: IO ()
main = print $ takeWhile (<= 120) attractiveNumbers</
Or equivalently, as a list comprehension:
<
attractiveNumbers :: [Integer]
Line 1,635:
main :: IO ()
main = print $ takeWhile (<= 120) attractiveNumbers</
Or simply:
<
attractiveNumbers :: [Integer]
Line 1,647:
main :: IO ()
main = print $ takeWhile (<= 120) attractiveNumbers</
{{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>
echo (#~ (1 p: ])@#@q:) >:i.120
</syntaxhighlight>
{{out}}
<pre>
Line 1,661:
=={{header|JavaScript}}==
<
'use strict';
Line 1,880:
// MAIN ---
return main();
})();</
{{Out}}
<pre> 4 6 8 9 10 12 14 15 18 20
Line 1,893:
=={{header|Java}}==
{{trans|C}}
<
static boolean is_prime(int n) {
Line 1,937:
System.out.println();
}
}</
{{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>
def count(s): reduce s as $x (null; .+1);
Line 1,965:
[range($m; $n+1) | select(is_attractive)];
printattractive(1; 120)</
{{out}}
<pre>
Line 1,974:
=={{header|Julia}}==
<
# 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)
</
<pre>
The attractive numbers from 1 to 120 are:
Line 1,991:
=={{header|Kotlin}}==
{{trans|Go}}
<
const val MAX = 120
Line 2,044:
}
println()
}</
{{output}}
Line 2,056:
=={{header|LLVM}}==
<
; 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" }</
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 2,290:
=={{header|Lua}}==
<
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</
{{out}}
<pre>4 6 8 9 10 12 14 15 18 20 21 22 25 26 27
Line 2,331:
=={{header|Maple}}==
<
local an, i;
an :=[]:
Line 2,340:
end do:
end proc:
attractivenumbers(120);</
{{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}}==
<
AttractiveNumberQ[n_Integer] := FactorInteger[n][[All, 2]] // Total // PrimeQ
Reap[Do[If[AttractiveNumberQ[i], Sow[i]], {i, 120}]][[2, 1]]</
{{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}}==
<
FROM InOut IMPORT WriteCard, WriteLn;
Line 2,412:
END;
WriteLn();
END AttractiveNumbers.</
{{out}}
<pre> 4 6 8 9 10 12 14 15 18 20 21 22 25 26 27
Line 2,422:
=={{header|Nanoquery}}==
{{trans|C}}
<
def is_prime(n)
Line 2,489:
end
end
println</
{{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).
<
(define (prime? n)
(= (length (factor n)) 1))
Line 2,509:
;
(filter attractive? (sequence 2 120))
</syntaxhighlight>
{{out}}
<pre>
Line 2,519:
=={{header|Nim}}==
{{trans|C}}
<
const MAX = 120
Line 2,574:
main()
</syntaxhighlight>
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 2,585:
{{trans|Java}}
<
function : Main(args : String[]) ~ Nil {
max := 120;
Line 2,661:
return -1;
}
}</
{{output}}
Line 2,675:
{{works with|Free Pascal}}
same procedure as in http://rosettacode.org/wiki/Abundant,_deficient_and_perfect_number_classifications
<
{ 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.</
{{out}}
<pre> 1 with many factors 0.000 s
Line 2,917:
=={{header|Perl}}==
{{libheader|ntheory}}
<
is_prime +factor $_ and print "$_ " for 1..120;</
{{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}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 2,958:
=={{header|PHP}}==
<
function isPrime ($x) {
if ($x < 2) return false;
Line 2,988:
if (isPrime(countFacs($i))) echo $i." ";
}
?></
{{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}}==
<
%replace MAX by 120;
declare prime(1:MAX) bit(1);
Line 3,039:
end;
end;
end attractive;</
{{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}}==
<
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</
{{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}}
<
S is sqrt(N),
prime_factors(N, Factors, S, 2).
Line 3,187:
main:-
print_attractive_numbers(1, 120, 1).</
{{out}}
Line 3,198:
=={{header|PureBasic}}==
<
Dim prime.b(#MAX)
FillMemory(@prime(),#MAX,#True,#PB_Byte) : FillMemory(@prime(),2,#False,#PB_Byte)
Line 3,225:
EndIf
Next
PrintN("") : Input()</
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 3,236:
===Procedural===
{{Works with|Python|2.7.12}}
<
def get_pfct(n):
Line 3,260:
for i,each in enumerate(pool):
if each in primes:
print i,</
{{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}}
<
from itertools import chain, count, takewhile
Line 3,374:
# MAIN ---
if __name__ == '__main__':
main()</
{{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].
<
primefactors size 1 = ] is attractive ( n --> b )
120 times
[ i^ 1+ attractive if
[ i^ 1+ echo sp ] ]</
{{out}}
Line 3,398:
=={{header|R}}==
<
is_prime <- function(num) {
if (num < 2) return(FALSE)
Line 3,441:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,449:
=={{header|Racket}}==
<
(require math/number-theory)
(define attractive? (compose1 prime? prime-omega))
(filter attractive? (range 1 121))</
{{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.
<
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
}</
{{out}}
<pre>Attractive numbers from 1 to 120:
Line 3,508:
If the argument for the program is negative, only a ''count'' of attractive numbers up to and including '''│N│''' is shown.
<
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. */</
This REXX program makes use of '''LINESIZE''' REXX program (or BIF) which is used to determine the
screen width (or linesize) of the terminal (console).
Line 3,585:
=={{header|Ring}}==
<
# Project: Attractive Numbers
Line 3,624:
next
return 1
</syntaxhighlight>
{{out}}
<pre>
Line 3,658:
=={{header|Ruby}}==
<
p (1..120).select{|n| n.prime_division.sum(&:last).prime? }
</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,667:
=={{header|Rust}}==
Uses [https://crates.io/crates/primal primal]
<
const MAX: u64 = 120;
Line 3,709:
}
println!("The attractive numbers up to and including 120 are\n{:?}", output);
}</
{{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)].
<
private val max = 120
private var count = 0
Line 3,742:
.groupBy { case (_, index) => index / 20 }
.foreach { case (_, row) => println(row.map(_._1).mkString) }
}</
=={{header|Sidef}}==
<
n.bigomega.is_prime
}
1..120 -> grep(is_attractive).say</
{{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}}==
<
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)")</
{{out}}
Line 3,812:
=={{header|Tcl}}==
<
if {$n < 2} {
return 0
Line 3,864:
}
}
showRange 1 120</
{{out}}
<pre>Attractive numbers in range 1..120 are:
Line 3,875:
=={{header|Vala}}==
{{trans|D}}
<
var d = 5;
if (n < 2) return false;
Line 3,923:
}
stdout.printf("\n");
}</
{{out}}
Line 3,935:
=={{header|VBA}}==
<
Public Sub AttractiveNumbers()
Line 4,013:
Finish2:
End Function</
=={{header|Visual Basic .NET}}==
{{trans|D}}
<
Const MAX = 120
Line 4,072:
End Sub
End Module</
{{out}}
<pre>The attractive numbers up to and including 120 are:
Line 4,082:
=={{header|Vlang}}==
{{trans|Go}}
<
if n < 2 {
return false
Line 4,152:
}
}
}</
{{out}}
Line 4,166:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<
import "/math" for Int
Line 4,180:
}
}
System.print()</
{{out}}
Line 4,192:
=={{header|XPL0}}==
<
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);
];
]</
{{out}}
Line 4,241:
Using GMP (GNU Multiple Precision Arithmetic Library, probabilistic
primes) because it is easy and fast to test for primeness.
<
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");</
Using [[Prime decomposition#zkl]]
<
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;
}</
{{out}}
<pre>
|