Attractive numbers: Difference between revisions

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

edits