Attractive numbers: Difference between revisions

Add ABC
m (spelling correction)
(Add ABC)
 
(9 intermediate revisions by 8 users not shown)
Line 171:
 
<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!}}==
Line 1,289 ⟶ 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#}}==
Line 1,531 ⟶ 1,599:
 
{{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}}==
Line 2,434 ⟶ 2,566:
 
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]
Line 2,771 ⟶ 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 3,859 ⟶ 4,116:
.foreach { case (_, row) => println(row.map(_._1).mkString) }
}</syntaxhighlight>
 
=={{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}}==
Line 4,282 ⟶ 4,574:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
var max = 120
Line 4,291 ⟶ 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()
2,115

edits