Attractive numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Thundergnat (talk | contribs) m (Automated syntax highlighting fixup (second round - minor fixes)) |
||
Line 19: | Line 19: | ||
{{trans|Python}} |
{{trans|Python}} |
||
<syntaxhighlight lang=11l>F is_prime(n) |
<syntaxhighlight lang="11l">F is_prime(n) |
||
I n < 2 |
I n < 2 |
||
R 0B |
R 0B |
||
Line 59: | Line 59: | ||
=={{header|8080 Assembly}}== |
=={{header|8080 Assembly}}== |
||
<syntaxhighlight lang=8080asm> ;;; Show attractive numbers up to 120 |
<syntaxhighlight lang="8080asm"> ;;; Show attractive numbers up to 120 |
||
MAX: equ 120 ; can be up to 255 (8 bit math is used) |
MAX: equ 120 ; can be up to 255 (8 bit math is used) |
||
;;; CP/M calls |
;;; CP/M calls |
||
Line 174: | Line 174: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
{{libheader|Action! Sieve of Eratosthenes}} |
{{libheader|Action! Sieve of Eratosthenes}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="action!">INCLUDE "H6:SIEVE.ACT" |
||
BYTE FUNC IsAttractive(BYTE n BYTE ARRAY primes) |
BYTE FUNC IsAttractive(BYTE n BYTE ARRAY primes) |
||
Line 236: | Line 236: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Attractive_Numbers is |
procedure Attractive_Numbers is |
||
Line 313: | Line 313: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{libheader|ALGOL 68-primes}} |
{{libheader|ALGOL 68-primes}} |
||
<syntaxhighlight lang=algol68>BEGIN # find some attractive numbers - numbers whose prime factor counts are # |
<syntaxhighlight lang="algol68">BEGIN # find some attractive numbers - numbers whose prime factor counts are # |
||
# prime, n must be > 1 # |
# prime, n must be > 1 # |
||
PR read "primes.incl.a68" PR |
PR read "primes.incl.a68" PR |
||
Line 355: | Line 355: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
<syntaxhighlight lang=algolw>% find some attractive numbers - numbers whose prime factor count is prime % |
<syntaxhighlight lang="algolw">% find some attractive numbers - numbers whose prime factor count is prime % |
||
begin |
begin |
||
% implements the sieve of Eratosthenes % |
% implements the sieve of Eratosthenes % |
||
Line 412: | Line 412: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
<syntaxhighlight lang=applescript>on isPrime(n) |
<syntaxhighlight lang="applescript">on isPrime(n) |
||
if (n < 4) then return (n > 1) |
if (n < 4) then return (n > 1) |
||
if ((n mod 2 is 0) or (n mod 3 is 0)) then return false |
if ((n mod 2 is 0) or (n mod 3 is 0)) then return false |
||
Line 463: | Line 463: | ||
{{output}} |
{{output}} |
||
<syntaxhighlight 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}</syntaxhighlight> |
<syntaxhighlight 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}</syntaxhighlight> |
||
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! |
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: | Line 469: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
<syntaxhighlight lang=rebol>attractive?: function [x] -> prime? size factors.prime x |
<syntaxhighlight lang="rebol">attractive?: function [x] -> prime? size factors.prime x |
||
print select 1..120 => attractive?</syntaxhighlight> |
print select 1..120 => attractive?</syntaxhighlight> |
||
Line 478: | Line 478: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="autohotkey">AttractiveNumbers(n){ |
||
c := prime_numbers(n).count() |
c := prime_numbers(n).count() |
||
if c = 1 |
if c = 1 |
||
Line 530: | Line 530: | ||
return ans |
return ans |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
Examples:<syntaxhighlight lang= |
Examples:<syntaxhighlight lang="autohotkey">c:= 0 |
||
loop |
loop |
||
{ |
{ |
||
Line 547: | Line 547: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="awk"> |
||
# syntax: GAWK -f ATTRACTIVE_NUMBERS.AWK |
# syntax: GAWK -f ATTRACTIVE_NUMBERS.AWK |
||
# converted from C |
# converted from C |
||
Line 596: | Line 596: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
<syntaxhighlight lang=basic>10 DEFINT A-Z |
<syntaxhighlight lang="basic">10 DEFINT A-Z |
||
20 M=120 |
20 M=120 |
||
30 DIM C(M): C(0)=-1: C(1)=-1 |
30 DIM C(M): C(0)=-1: C(1)=-1 |
||
Line 627: | Line 627: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
<syntaxhighlight lang=bcpl>get "libhdr" |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
manifest $( MAXIMUM = 120 $) |
manifest $( MAXIMUM = 120 $) |
||
Line 671: | Line 671: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<syntaxhighlight lang=c>#include <stdio.h> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#define TRUE 1 |
#define TRUE 1 |
||
Line 734: | Line 734: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang=csharp>using System; |
<syntaxhighlight lang="csharp">using System; |
||
namespace AttractiveNumbers { |
namespace AttractiveNumbers { |
||
Line 798: | Line 798: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=cpp>#include <iostream> |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <iomanip> |
#include <iomanip> |
||
Line 858: | Line 858: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
<syntaxhighlight lang=clu>sieve = proc (max: int) returns (array[bool]) |
<syntaxhighlight lang="clu">sieve = proc (max: int) returns (array[bool]) |
||
prime: array[bool] := array[bool]$fill(1,max,true) |
prime: array[bool] := array[bool]$fill(1,max,true) |
||
prime[1] := false |
prime[1] := false |
||
Line 906: | Line 906: | ||
102 105 106 108 110 111 112 114 115 116 117 118 119 120</pre> |
102 105 106 108 110 111 112 114 115 116 117 118 119 120</pre> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
<syntaxhighlight lang=cobol> IDENTIFICATION DIVISION. |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. ATTRACTIVE-NUMBERS. |
PROGRAM-ID. ATTRACTIVE-NUMBERS. |
||
Line 1,002: | Line 1,002: | ||
=={{header|Comal}}== |
=={{header|Comal}}== |
||
<syntaxhighlight lang=comal>0010 FUNC factors#(n#) CLOSED |
<syntaxhighlight lang="comal">0010 FUNC factors#(n#) CLOSED |
||
0020 count#:=0 |
0020 count#:=0 |
||
0030 WHILE n# MOD 2=0 DO n#:=n# DIV 2;count#:+1 |
0030 WHILE n# MOD 2=0 DO n#:=n# DIV 2;count#:+1 |
||
Line 1,032: | Line 1,032: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="lisp"> |
||
(defun attractivep (n) |
(defun attractivep (n) |
||
(primep (length (factors n))) ) |
(primep (length (factors n))) ) |
||
Line 1,053: | Line 1,053: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
<syntaxhighlight lang=cowgol>include "cowgol.coh"; |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
const MAXIMUM := 120; |
const MAXIMUM := 120; |
||
Line 1,122: | Line 1,122: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
<syntaxhighlight lang=d>import std.stdio; |
<syntaxhighlight lang="d">import std.stdio; |
||
enum MAX = 120; |
enum MAX = 120; |
||
Line 1,183: | Line 1,183: | ||
See [[#Pascal]]. |
See [[#Pascal]]. |
||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
<syntaxhighlight lang=draco>/* Sieve of Eratosthenes */ |
<syntaxhighlight lang="draco">/* Sieve of Eratosthenes */ |
||
proc nonrec sieve([*] bool prime) void: |
proc nonrec sieve([*] bool prime) void: |
||
word p, c, max; |
word p, c, max; |
||
Line 1,240: | Line 1,240: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="fsharp">// attractive_numbers.fsx |
||
// taken from Primality by trial division |
// taken from Primality by trial division |
||
let rec primes = |
let rec primes = |
||
Line 1,292: | Line 1,292: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99}} |
{{works with|Factor|0.99}} |
||
<syntaxhighlight lang=factor>USING: formatting grouping io math.primes math.primes.factors |
<syntaxhighlight lang="factor">USING: formatting grouping io math.primes math.primes.factors |
||
math.ranges sequences ; |
math.ranges sequences ; |
||
Line 1,309: | Line 1,309: | ||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
<syntaxhighlight lang=fortran> |
<syntaxhighlight lang="fortran"> |
||
program attractive_numbers |
program attractive_numbers |
||
use iso_fortran_env, only: output_unit |
use iso_fortran_env, only: output_unit |
||
Line 1,409: | Line 1,409: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang=freebasic> |
<syntaxhighlight lang="freebasic"> |
||
Const limite = 120 |
Const limite = 120 |
||
Line 1,471: | Line 1,471: | ||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
<syntaxhighlight lang=frink>println[select[2 to 120, {|x| !isPrime[x] and isPrime[length[factorFlat[x]]]}]]</syntaxhighlight> |
<syntaxhighlight lang="frink">println[select[2 to 120, {|x| !isPrime[x] and isPrime[length[factorFlat[x]]]}]]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,479: | Line 1,479: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Simple functions to test for primality and to count prime factors suffice here. |
Simple functions to test for primality and to count prime factors suffice here. |
||
<syntaxhighlight lang=go>package main |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,563: | Line 1,563: | ||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
<syntaxhighlight lang=groovy>class AttractiveNumbers { |
<syntaxhighlight lang="groovy">class AttractiveNumbers { |
||
static boolean isPrime(int n) { |
static boolean isPrime(int n) { |
||
if (n < 2) return false |
if (n < 2) return false |
||
Line 1,615: | Line 1,615: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
<syntaxhighlight lang=haskell>import Data.Numbers.Primes |
<syntaxhighlight lang="haskell">import Data.Numbers.Primes |
||
import Data.Bool (bool) |
import Data.Bool (bool) |
||
Line 1,626: | Line 1,626: | ||
Or equivalently, as a list comprehension: |
Or equivalently, as a list comprehension: |
||
<syntaxhighlight lang=haskell>import Data.Numbers.Primes |
<syntaxhighlight lang="haskell">import Data.Numbers.Primes |
||
attractiveNumbers :: [Integer] |
attractiveNumbers :: [Integer] |
||
Line 1,638: | Line 1,638: | ||
Or simply: |
Or simply: |
||
<syntaxhighlight lang=haskell>import Data.Numbers.Primes |
<syntaxhighlight lang="haskell">import Data.Numbers.Primes |
||
attractiveNumbers :: [Integer] |
attractiveNumbers :: [Integer] |
||
Line 1,652: | Line 1,652: | ||
=={{header|J}}== |
=={{header|J}}== |
||
<syntaxhighlight lang=j> |
<syntaxhighlight lang="j"> |
||
echo (#~ (1 p: ])@#@q:) >:i.120 |
echo (#~ (1 p: ])@#@q:) >:i.120 |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
Line 1,661: | Line 1,661: | ||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
<syntaxhighlight lang=javascript>(() => { |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 1,893: | Line 1,893: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang=java>public class Attractive { |
<syntaxhighlight lang="java">public class Attractive { |
||
static boolean is_prime(int n) { |
static boolean is_prime(int n) { |
||
Line 1,955: | Line 1,955: | ||
* `is_prime` as defined at [[Erd%C5%91s-primes#jq | Erdős primes]] on RC |
* `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 |
* `prime_factors` as defined at [[Smith_numbers#jq | Smith numbers]] on RC |
||
<syntaxhighlight lang=jq> |
<syntaxhighlight lang="jq"> |
||
def count(s): reduce s as $x (null; .+1); |
def count(s): reduce s as $x (null; .+1); |
||
Line 1,974: | Line 1,974: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang=julia>using Primes |
<syntaxhighlight 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)) |
# oneliner is println("The attractive numbers from 1 to 120 are:\n", filter(x -> isprime(sum(values(factor(x)))), 1:120)) |
||
Line 1,991: | Line 1,991: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<syntaxhighlight lang=scala>// Version 1.3.21 |
<syntaxhighlight lang="scala">// Version 1.3.21 |
||
const val MAX = 120 |
const val MAX = 120 |
||
Line 2,056: | Line 2,056: | ||
=={{header|LLVM}}== |
=={{header|LLVM}}== |
||
<syntaxhighlight lang=llvm>; This is not strictly LLVM, as it uses the C library function "printf". |
<syntaxhighlight 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 |
; 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. |
; to just load the string into memory, and that would be boring. |
||
Line 2,290: | Line 2,290: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
<syntaxhighlight lang=lua>-- Returns true if x is prime, and false otherwise |
<syntaxhighlight lang="lua">-- Returns true if x is prime, and false otherwise |
||
function isPrime (x) |
function isPrime (x) |
||
if x < 2 then return false end |
if x < 2 then return false end |
||
Line 2,331: | Line 2,331: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="maple">attractivenumbers := proc(n::posint) |
||
local an, i; |
local an, i; |
||
an :=[]: |
an :=[]: |
||
Line 2,345: | Line 2,345: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="mathematica">ClearAll[AttractiveNumberQ] |
||
AttractiveNumberQ[n_Integer] := FactorInteger[n][[All, 2]] // Total // PrimeQ |
AttractiveNumberQ[n_Integer] := FactorInteger[n][[All, 2]] // Total // PrimeQ |
||
Reap[Do[If[AttractiveNumberQ[i], Sow[i]], {i, 120}]][[2, 1]]</syntaxhighlight> |
Reap[Do[If[AttractiveNumberQ[i], Sow[i]], {i, 120}]][[2, 1]]</syntaxhighlight> |
||
Line 2,352: | Line 2,352: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
<syntaxhighlight lang=modula2>MODULE AttractiveNumbers; |
<syntaxhighlight lang="modula2">MODULE AttractiveNumbers; |
||
FROM InOut IMPORT WriteCard, WriteLn; |
FROM InOut IMPORT WriteCard, WriteLn; |
||
Line 2,422: | Line 2,422: | ||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nanoquery">MAX = 120 |
||
def is_prime(n) |
def is_prime(n) |
||
Line 2,502: | Line 2,502: | ||
The ''factor'' function returns a list of the prime factors of an integer with repetition, |
The ''factor'' function returns a list of the prime factors of an integer with repetition, |
||
e. g. (factor 12) is (2 2 3). |
e. g. (factor 12) is (2 2 3). |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="newlisp"> |
||
(define (prime? n) |
(define (prime? n) |
||
(= (length (factor n)) 1)) |
(= (length (factor n)) 1)) |
||
Line 2,519: | Line 2,519: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="nim">import strformat |
||
const MAX = 120 |
const MAX = 120 |
||
Line 2,585: | Line 2,585: | ||
{{trans|Java}} |
{{trans|Java}} |
||
<syntaxhighlight lang=objeck>class AttractiveNumber { |
<syntaxhighlight lang="objeck">class AttractiveNumber { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
max := 120; |
max := 120; |
||
Line 2,675: | Line 2,675: | ||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
same procedure as in http://rosettacode.org/wiki/Abundant,_deficient_and_perfect_number_classifications |
same procedure as in http://rosettacode.org/wiki/Abundant,_deficient_and_perfect_number_classifications |
||
<syntaxhighlight lang=pascal>program AttractiveNumbers; |
<syntaxhighlight lang="pascal">program AttractiveNumbers; |
||
{ numbers with count of factors = prime |
{ numbers with count of factors = prime |
||
* using modified sieve of erathosthes |
* using modified sieve of erathosthes |
||
Line 2,917: | Line 2,917: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
<syntaxhighlight lang=perl>use ntheory <is_prime factor>; |
<syntaxhighlight lang="perl">use ntheory <is_prime factor>; |
||
is_prime +factor $_ and print "$_ " for 1..120;</syntaxhighlight> |
is_prime +factor $_ and print "$_ " for 1..120;</syntaxhighlight> |
||
Line 2,924: | Line 2,924: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--<syntaxhighlight lang= |
<!--<syntaxhighlight 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: #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> |
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span> |
||
Line 2,958: | Line 2,958: | ||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
<syntaxhighlight lang=php><?php |
<syntaxhighlight lang="php"><?php |
||
function isPrime ($x) { |
function isPrime ($x) { |
||
if ($x < 2) return false; |
if ($x < 2) return false; |
||
Line 2,993: | Line 2,993: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
<syntaxhighlight lang=pli>attractive: procedure options(main); |
<syntaxhighlight lang="pli">attractive: procedure options(main); |
||
%replace MAX by 120; |
%replace MAX by 120; |
||
declare prime(1:MAX) bit(1); |
declare prime(1:MAX) bit(1); |
||
Line 3,048: | Line 3,048: | ||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
<syntaxhighlight lang=pli>100H: |
<syntaxhighlight lang="pli">100H: |
||
BDOS: PROCEDURE (F, ARG); DECLARE F BYTE, ARG ADDRESS; GO TO 5; END BDOS; |
BDOS: PROCEDURE (F, ARG); DECLARE F BYTE, ARG ADDRESS; GO TO 5; END BDOS; |
||
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT; |
EXIT: PROCEDURE; CALL BDOS(0,0); END EXIT; |
||
Line 3,130: | Line 3,130: | ||
=={{header|Prolog}}== |
=={{header|Prolog}}== |
||
{{works with|SWI Prolog}} |
{{works with|SWI Prolog}} |
||
<syntaxhighlight lang=prolog>prime_factors(N, Factors):- |
<syntaxhighlight lang="prolog">prime_factors(N, Factors):- |
||
S is sqrt(N), |
S is sqrt(N), |
||
prime_factors(N, Factors, S, 2). |
prime_factors(N, Factors, S, 2). |
||
Line 3,198: | Line 3,198: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="purebasic">#MAX=120 |
||
Dim prime.b(#MAX) |
Dim prime.b(#MAX) |
||
FillMemory(@prime(),#MAX,#True,#PB_Byte) : FillMemory(@prime(),2,#False,#PB_Byte) |
FillMemory(@prime(),#MAX,#True,#PB_Byte) : FillMemory(@prime(),2,#False,#PB_Byte) |
||
Line 3,236: | Line 3,236: | ||
===Procedural=== |
===Procedural=== |
||
{{Works with|Python|2.7.12}} |
{{Works with|Python|2.7.12}} |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="python">from sympy import sieve # library for primes |
||
def get_pfct(n): |
def get_pfct(n): |
||
Line 3,267: | 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. |
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}} |
{{Works with|Python|3.7}} |
||
<syntaxhighlight lang=python>'''Attractive numbers''' |
<syntaxhighlight lang="python">'''Attractive numbers''' |
||
from itertools import chain, count, takewhile |
from itertools import chain, count, takewhile |
||
Line 3,386: | Line 3,386: | ||
<code>primefactors</code> is defined at [https://rosettacode.org/wiki/Prime_decomposition#Quackery Prime decomposition]. |
<code>primefactors</code> is defined at [https://rosettacode.org/wiki/Prime_decomposition#Quackery Prime decomposition]. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="quackery"> [ primefactors size |
||
primefactors size 1 = ] is attractive ( n --> b ) |
primefactors size 1 = ] is attractive ( n --> b ) |
||
Line 3,398: | Line 3,398: | ||
=={{header|R}}== |
=={{header|R}}== |
||
<syntaxhighlight lang=rsplus> |
<syntaxhighlight lang="rsplus"> |
||
is_prime <- function(num) { |
is_prime <- function(num) { |
||
if (num < 2) return(FALSE) |
if (num < 2) return(FALSE) |
||
Line 3,449: | Line 3,449: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang=racket>#lang racket |
<syntaxhighlight lang="racket">#lang racket |
||
(require math/number-theory) |
(require math/number-theory) |
||
(define attractive? (compose1 prime? prime-omega)) |
(define attractive? (compose1 prime? prime-omega)) |
||
Line 3,464: | 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. |
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= |
<syntaxhighlight lang="raku" line>use Lingua::EN::Numbers; |
||
use ntheory:from<Perl5> <factor is_prime>; |
use ntheory:from<Perl5> <factor is_prime>; |
||
Line 3,508: | Line 3,508: | ||
If the argument for the program is negative, only a ''count'' of attractive numbers up to and including '''│N│''' is shown. |
If the argument for the program is negative, only a ''count'' of attractive numbers up to and including '''│N│''' is shown. |
||
<syntaxhighlight lang=rexx>/*REXX program finds and shows lists (or counts) attractive numbers up to a specified N.*/ |
<syntaxhighlight 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. */ |
parse arg N . /*get optional argument from the C.L. */ |
||
if N=='' | N=="," then N= 120 /*Not specified? Then use the default.*/ |
if N=='' | N=="," then N= 120 /*Not specified? Then use the default.*/ |
||
Line 3,585: | Line 3,585: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
<syntaxhighlight lang=ring> |
<syntaxhighlight lang="ring"> |
||
# Project: Attractive Numbers |
# Project: Attractive Numbers |
||
Line 3,658: | Line 3,658: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
<syntaxhighlight lang=ruby>require "prime" |
<syntaxhighlight lang="ruby">require "prime" |
||
p (1..120).select{|n| n.prime_division.sum(&:last).prime? } |
p (1..120).select{|n| n.prime_division.sum(&:last).prime? } |
||
Line 3,667: | Line 3,667: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
Uses [https://crates.io/crates/primal primal] |
Uses [https://crates.io/crates/primal primal] |
||
<syntaxhighlight lang=rust>use primal::Primes; |
<syntaxhighlight lang="rust">use primal::Primes; |
||
const MAX: u64 = 120; |
const MAX: u64 = 120; |
||
Line 3,715: | Line 3,715: | ||
=={{header|Scala}}== |
=={{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)]. |
{{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)]. |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="scala">object AttractiveNumbers extends App { |
||
private val max = 120 |
private val max = 120 |
||
private var count = 0 |
private var count = 0 |
||
Line 3,745: | Line 3,745: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang=ruby>func is_attractive(n) { |
<syntaxhighlight lang="ruby">func is_attractive(n) { |
||
n.bigomega.is_prime |
n.bigomega.is_prime |
||
} |
} |
||
Line 3,755: | Line 3,755: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
<syntaxhighlight lang=swift>import Foundation |
<syntaxhighlight lang="swift">import Foundation |
||
extension BinaryInteger { |
extension BinaryInteger { |
||
Line 3,812: | Line 3,812: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="tcl">proc isPrime {n} { |
||
if {$n < 2} { |
if {$n < 2} { |
||
return 0 |
return 0 |
||
Line 3,875: | Line 3,875: | ||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang=vala>bool is_prime(int n) { |
<syntaxhighlight lang="vala">bool is_prime(int n) { |
||
var d = 5; |
var d = 5; |
||
if (n < 2) return false; |
if (n < 2) return false; |
||
Line 3,935: | Line 3,935: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="vba">Option Explicit |
||
Public Sub AttractiveNumbers() |
Public Sub AttractiveNumbers() |
||
Line 4,017: | Line 4,017: | ||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
{{trans|D}} |
{{trans|D}} |
||
<syntaxhighlight lang=vbnet>Module Module1 |
<syntaxhighlight lang="vbnet">Module Module1 |
||
Const MAX = 120 |
Const MAX = 120 |
||
Line 4,082: | Line 4,082: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<syntaxhighlight lang=vlang>fn is_prime(n int) bool { |
<syntaxhighlight lang="vlang">fn is_prime(n int) bool { |
||
if n < 2 { |
if n < 2 { |
||
return false |
return false |
||
Line 4,166: | Line 4,166: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
{{libheader|Wren-math}} |
{{libheader|Wren-math}} |
||
<syntaxhighlight lang=ecmascript>import "/fmt" for Fmt |
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt |
||
import "/math" for Int |
import "/math" for Int |
||
Line 4,192: | Line 4,192: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
<syntaxhighlight lang= |
<syntaxhighlight lang="xpl0">func IsPrime(N); \Return 'true' if N is prime |
||
int N, I; |
int N, I; |
||
[if N <= 2 then return N = 2; |
[if N <= 2 then return N = 2; |
||
Line 4,241: | Line 4,241: | ||
Using GMP (GNU Multiple Precision Arithmetic Library, probabilistic |
Using GMP (GNU Multiple Precision Arithmetic Library, probabilistic |
||
primes) because it is easy and fast to test for primeness. |
primes) because it is easy and fast to test for primeness. |
||
<syntaxhighlight lang=zkl>var [const] BI=Import("zklBigNum"); // libGMP |
<syntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP |
||
fcn attractiveNumber(n){ BI(primeFactors(n).len()).probablyPrime() } |
fcn attractiveNumber(n){ BI(primeFactors(n).len()).probablyPrime() } |
||
Line 4,248: | Line 4,248: | ||
.apply("%4d".fmt).pump(Void,T(Void.Read,19,False),"println");</syntaxhighlight> |
.apply("%4d".fmt).pump(Void,T(Void.Read,19,False),"println");</syntaxhighlight> |
||
Using [[Prime decomposition#zkl]] |
Using [[Prime decomposition#zkl]] |
||
<syntaxhighlight lang=zkl>fcn primeFactors(n){ // Return a list of factors of n |
<syntaxhighlight 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 |
acc:=fcn(n,k,acc,maxD){ // k is 2,3,5,7,9,... not optimum |
||
if(n==1 or k>maxD) acc.close(); |
if(n==1 or k>maxD) acc.close(); |