Weird numbers: Difference between revisions

m
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
(19 intermediate revisions by 15 users not shown)
Line 32:
{{trans|D}}
 
<langsyntaxhighlight lang="11l">F divisors(n)
V divs = [1]
[Int] divs2
Line 80:
count++
I count == 25
L.break</langsyntaxhighlight>
 
{{out}}
Line 87:
70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Go}}
Translation of the untweaked Go version 1 sample. Avoids creating separate array slices in the semiperfect routine, to save memory for Algol 68G version 2.8.3.
<syntaxhighlight lang="algol68">BEGIN # find wierd numbers - abundant but not semiperfect numbers - translation of Go #
# returns the divisors of n in descending order #
PROC divisors = ( INT n )[]INT:
BEGIN
INT max divs = 2 * ENTIER sqrt( n );
[ 1 : max divs ]INT divs;
[ 1 : max divs ]INT divs2;
INT d pos := 0, d2 pos := 0;
divs[ d pos +:= 1 ] := 1;
FOR i FROM 2 WHILE i * i <= n DO
IF n MOD i = 0 THEN
INT j = n OVER i;
divs[ d pos +:= 1 ] := i;
IF i /= j THEN divs2[ d2 pos +:= 1 ] := j FI
FI
OD;
FOR i FROM d pos BY -1 WHILE i > 0 DO
divs2[ d2 pos +:= 1 ] := divs[ i ]
OD;
divs2[ 1 : d2 pos ]
END # divisors # ;
# returns TRUE if n with divisors divs, is abundant, FALSE otherwise #
PROC abundant = ( INT n, []INT divs )BOOL:
BEGIN
INT sum := 0;
FOR i FROM LWB divs TO UPB divs DO sum +:= divs[ i ] OD;
sum > n
END # abundant # ;
# returns TRUE if n with divisors divs, is semiperfect, FALSE otherwise #
PROC semiperfect = ( INT n, []INT divs, INT lb, ub )BOOL:
IF ub < lb
THEN FALSE
ELIF INT h = divs[ lb ];
n < h
THEN semiperfect( n, divs, lb + 1, ub )
ELIF n = h
THEN TRUE
ELIF semiperfect( n - h, divs, lb + 1, ub )
THEN TRUE
ELSE semiperfect( n, divs, lb + 1, ub )
FI # semiperfect # ;
# returns a sieve where FALSE = abundant and not semiperfect #
PROC sieve = ( INT limit )[]BOOL:
BEGIN # Only interested in even numbers >= 2 #
[ 1 : limit ]BOOL w; FOR i FROM 1 TO limit DO w[ i ] := FALSE OD;
FOR i FROM 2 BY 2 TO limit DO
IF NOT w[ i ] THEN
[]INT divs = divisors( i );
IF NOT abundant( i, divs ) THEN
w[ i ] := TRUE
ELIF semiperfect( i, divs, LWB divs, UPB divs ) THEN
FOR j FROM i BY i TO limit DO w[ j ] := TRUE OD
FI
FI
OD;
w
END # sieve # ;
BEGIN # task #
[]BOOL w = sieve( 17 000 );
INT count := 0;
INT max = 25;
print( ( "The first 25 weird numbers are:", newline ) );
FOR n FROM 2 BY 2 WHILE count < max DO
IF NOT w[ n ] THEN
print( ( whole( n, 0 ), " " ) );
count +:= 1
FI
OD;
print( ( newline ) )
END
END</syntaxhighlight>
{{out}}
<pre>
The first 25 weird numbers are:
70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310</pre>
 
=={{header|AppleScript}}==
Line 94 ⟶ 173:
(Though after about 6 seconds (on this system) it does yield the first 25, and intermediates can be logged in the Messages channel of macOS Script Editor).
 
<langsyntaxhighlight lang="applescript">on run
take(25, weirds())
-- Gets there, but takes about 6 seconds on this system,
Line 265 ⟶ 344:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<pre>{70, 836, 4030, 5830, 7192, 7912, 9272, 10430, 10570, 10792, 10990, 11410, 11690, 12110, 12530, 12670, 13370, 13510, 13790, 13930, 14770, 15610, 15890, 16030, 16310}</pre>
Line 273 ⟶ 352:
0.69 seconds:
 
<langsyntaxhighlight lang="applescript">on-- Sum properDivisors(n)'s proper divisors.
on aliquotSum(n)
if (n < 2) then return 0
set sum to 1
set sqrt to n ^ 0.5
set limit to sqrt div 1
if (limit = sqrt) then
set sum to sum + limit
set limit to limit - 1
end if
repeat with i from 2 to limit
if (n mod i is 0) then set sum to sum + i + n div i
end repeat
return sum
end aliquotSum
 
-- Return n's proper divisors.
on properDivisors(n)
set output to {}
Line 295 ⟶ 392:
end properDivisors
 
-- Does a subset of the given list of numbers add up to the target value?
on canSumTo(lst, target)
on subsetOf:numberList sumsTo:target
script o
property llst : lstnumberList
property someNegatives : false
on cstssp(target, i)
repeat while (i > 1)
set n to item i of my llst
if (n = target) then return true
if (i = 1) then return false
set i to i - 1
if ((n = target) or (((n < target) or (someNegatives)) and (cstssp(target - n, i)))) then return true
end repeat
return (target = beginning of my lst)
end cs
end ssp
end script
-- The search can be more efficient if it's known the list contains no negatives.
repeat with n in o's lst
if (n < 0) then
set o's someNegatives to true
exit repeat
end if
end repeat
return o's cstssp(target, count o's llst)
end subsetOf:sumsTo:
end canSumTo
 
-- Is n a weird number?
on isWeird(n)
-- Yes if its aliquot sum's greater than it and no subset of its proper divisors adds up to it.
script o
-- Using aliquotSum() to propertyget divisorsthe :divisor sum and then calling properDivisors(n) too if a list's actually
-- needed is generally faster than calling properDivisors() in the first place and summing the result.
end script
set sum to aliquotSum(n)
setif (sum to> 0n) then
repeat with thisDivisor in o'sset divisors to properDivisors(n)
-- Check that no subset sums to the smaller (usually the latter) of n and sum - n.
set sum to sum + thisDivisor
tell (sum - n) to if (it < n) then set n to it
end repeat
return (not (my subsetOf:divisors sumsTo:n))
else
return ((sum > n) and (not canSumTo(o's divisors, sum - n)))
return false
end if
end isWeird
 
Line 345 ⟶ 453:
end weirdNumbers
 
weirdNumbers(25)</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{70, 836, 4030, 5830, 7192, 7912, 9272, 10430, 10570, 10792, 10990, 11410, 11690, 12110, 12530, 12670, 13370, 13510, 13790, 13930, 14770, 15610, 15890, 16030, 16310}</langsyntaxhighlight>
 
=={{header|C}}==
{{trans|D}}
<langsyntaxhighlight lang="c">#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
Line 464 ⟶ 572:
free(w);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310</pre>
Line 470 ⟶ 578:
=={{header|C sharp|C#}}==
{{trans|D}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 549 ⟶ 657:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310</pre>
Line 555 ⟶ 663:
=={{header|C++}}==
{{trans|D}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <numeric>
Line 635 ⟶ 743:
std::cout << '\n';
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The first 25 weird numbers:70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310</pre>
Line 641 ⟶ 749:
=={{header|Crystal}}==
{{trans|Go}}
<langsyntaxhighlight lang="ruby">def divisors(n : Int32) : Array(Int32)
divs = [1]
divs2 = [] of Int32
Line 732 ⟶ 840:
require "benchmark"
puts Benchmark.measure { main }
</syntaxhighlight>
</lang>
 
{{out}}
Line 743 ⟶ 851:
 
=={{header|D}}==
{{trans|Kotlin}} Adding efficient "cut" condition in semiperfect recursive algorithm
{{trans|Kotlin}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.array;
import std.stdio;
Line 769 ⟶ 877:
 
bool semiperfect(int n, int[] divs) {
if// This algorithm is O(2^N) for N == divs.length >when 0)number {is not semiperfect.
// Comparing with (divs.sum < n) instead (divs.length==0) removes unnecessary
// recursive binary tree branches.
auto s = divs.sum;
if(s == n)
return true;
else if ( s<n )
return false;
else {
auto h = divs[0];
auto t = divs[1..$];
Line 776 ⟶ 892:
} else {
return n == h
// Supossin h is part of the sum
|| semiperfect(n - h, t)
// Supossin h is not part of the sum
|| semiperfect(n, t);
}
} else {
return false;
}
}
 
Line 815 ⟶ 931:
}
writeln;
}</langsyntaxhighlight>
{{out}}
<pre>70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310</pre>
Line 821 ⟶ 937:
=={{header|F#|F sharp}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="fsharp">let divisors n = [1..n/2] |> List.filter (fun x->n % x = 0)
 
let abundant (n:int) divs = Seq.sum(divs) > n
Line 852 ⟶ 968:
i <- i + 1
 
0 // return an integer exit code</langsyntaxhighlight>
{{out}}
<pre>1 -> 70
Line 882 ⟶ 998:
=={{header|Factor}}==
The <code>has-sum?</code> word is a translation of the Haskell function.
<langsyntaxhighlight lang="factor">USING: combinators.short-circuit io kernel lists lists.lazy
locals math math.primes.factors prettyprint sequences ;
IN: rosetta-code.weird-numbers
Line 908 ⟶ 1,024:
25 weirds ltake list>array . ;
 
MAIN: weird-numbers-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 941 ⟶ 1,057:
</pre>
 
=={{header|freebasicFreeBASIC}}==
<langsyntaxhighlight lang="freebasic">
 
 
Function GetFactors(n As Long,r() As Long) As Long
Redim r(0)
Line 986 ⟶ 1,100:
Return 1
End Function
 
 
Redim As Long u()
Line 1,000 ⟶ 1,113:
Print "first 25 done"
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,017 ⟶ 1,130:
 
When run on the same machine, the 'tweaked' version (linked to below), which was supplied by Enter your username, is almost 3 times faster than this.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,094 ⟶ 1,207:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 1,106 ⟶ 1,219:
=={{header|Haskell}}==
{{Trans|Python}}
<langsyntaxhighlight lang="haskell">weirds :: [Int]
weirds = filter abundantNotSemiperfect [1 ..]
 
Line 1,135 ⟶ 1,248:
main =
(putStrLn . unlines) $
zipWith (\i x -> show i ++ (" -> " ++ show x)) [1 ..] (take 25 weirds)</langsyntaxhighlight>
{{Out}}
<pre>1 -> 70
Line 1,165 ⟶ 1,278:
=={{header|J}}==
This algorithm uses a sieve to eliminate multiples of semiperfect numbers from future testing.
<syntaxhighlight lang="text">
factor=: [: }: [: , [: */&> [: { [: <@(^ i.@>:)/"1 [: |: __&q:
 
Line 1,207 ⟶ 1,320:
weird
)
</syntaxhighlight>
</lang>
<pre>
classify 20000 NB. the first 36 weird numbers
Line 1,214 ⟶ 1,327:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.List;
Line 1,288 ⟶ 1,401:
 
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,322 ⟶ 1,435:
{{Trans|Python}}
{{Trans|Haskell}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,436 ⟶ 1,549:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>1 -> 70
Line 1,463 ⟶ 1,576:
24 -> 16030
25 -> 16310</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Adapted from [[#Wren|Wren]]'''
 
For an explanation, see the [[#Go|Go]] entry.
 
The following also works with gojq, the Go implementation of jq, though much more slowly.
<syntaxhighlight lang=jq>
# unordered
def proper_divisors:
. as $n
| if $n > 1 then 1,
( range(2; 1 + (sqrt|floor)) as $i
| if ($n % $i) == 0 then $i,
(($n / $i) | if . == $i then empty else . end)
else empty
end)
else empty
end;
 
# Is n semiperfect given that divs are the proper divisors
def semiperfect(n; divs):
(divs|length) as $le
| if $le == 0 then false
else divs[0] as $h
| if n == $h then true
elif $le == 1 then false
else divs[1:] as $t
| if n < $h then semiperfect(n; $t)
else semiperfect(n-$h; $t) or semiperfect(n; $t)
end
end
end ;
 
def sieve(limit):
# 'false' denotes abundant and not semi-perfect.
# Only interested in even numbers >= 2
(reduce range(6; limit; 6) as $j ([]; .[$j] = true)) # eliminates multiples of 3
| reduce range(2; limit; 2) as $i (.;
if (.[$i]|not)
then [$i|proper_divisors] as $divs
| ($divs | add) as $sum
| if $sum <= $i
then .[$i] = true
elif (semiperfect($sum-$i; $divs))
then reduce range($i; limit; $i) as $j (.; .[$j] = true)
else .
end
else .
end) ;
 
# Print up to $max weird numbers based on the given sieve size, $limit.
def task($limit; $max):
sieve($limit) as $w
| def weirds:
range(2; $w|length; 2) | select($w[.]|not);
 
# collect into an array for ease of counting
[limit($max; weirds)]
| "The first \(length) weird numbers are:", . ;
 
# The parameters should be set on the command line:
task($sieve; $limit)
</syntaxhighlight>
'''Invocation:'''
 
jq -nrc --argjson sieve 16313 --argjson limit 25 -f weird.jq
{{output}}
<pre>
The first 25 weird numbers are:
[70,836,4030,5830,7192,7912,9272,10430,10570,10792,10990,11410,11690,12110,12530,12670,13370,13510,13790,13930,14770,15610,15890,16030,16310]
</pre>
 
 
=={{header|Julia}}==
<langsyntaxhighlight Julialang="julia">using Primes
 
function nosuchsum(revsorted, num)
Line 1,509 ⟶ 1,696:
 
testweird(25)
</langsyntaxhighlight>{{out}}
<pre>
The first 25 weird numbers are:
Line 1,516 ⟶ 1,703:
 
=={{header|Kotlin}}==
{{trans|Go}}<langsyntaxhighlight lang="scala">// Version 1.3.21
 
fun divisors(n: Int): List<Int> {
Line 1,580 ⟶ 1,767:
}
println()
}</langsyntaxhighlight>
 
{{output}}
Line 1,590 ⟶ 1,777:
=={{header|Lua}}==
{{trans|C#}}
<langsyntaxhighlight lang="lua">function make(n, d)
local a = {}
for i=1,n do
Line 1,700 ⟶ 1,887:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>The first 25 weird numbers:
70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[WeirdNumberQ, HasSumQ]
HasSumQ[n_Integer, xs_List] := HasSumHelperQ[n, ReverseSort[xs]]
HasSumHelperQ[n_Integer, xs_List] := Module[{h, t},
If[Length[xs] > 0,
h = First[xs];
t = Drop[xs, 1];
If[n < h,
HasSumHelperQ[n, t]
,
n == h \[Or] HasSumHelperQ[n - h, t] \[Or] HasSumHelperQ[n, t]
]
,
False
]
]
WeirdNumberQ[n_Integer] := Module[{divs},
divs = Most[Divisors[n]];
If[Total[divs] > n,
! HasSumQ[n, divs]
,
False
]
]
r = {};
n = 0;
While[
Length[r] < 25,
If[WeirdNumberQ[++n], AppendTo[r, n]]
]
Print[r]</syntaxhighlight>
{{out}}
<pre>{70, 836, 4030, 5830, 7192, 7912, 9272, 10430, 10570, 10792, 10990, 11410, 11690, 12110, 12530, 12670, 13370, 13510, 13790, 13930, 14770, 15610, 15890, 16030, 16310}</pre>
 
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import algorithm, math, strutils
 
func divisors(n: int): seq[int] =
Line 1,751 ⟶ 1,972:
if not w[n]: list.add n
inc n, 2
echo list.join(" ")</langsyntaxhighlight>
 
{{out}}
Line 1,760 ⟶ 1,981:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use feature 'say';
 
Line 1,796 ⟶ 2,017:
}
 
say "The first 25 weird numbers:\n" . join ' ', @weird;</langsyntaxhighlight>
{{out}}
<pre>The first 25 weird numbers:
Line 1,804 ⟶ 2,025:
{{trans|Sidef}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use 5.010;
use strict;
use ntheory qw(vecsum divisors divisor_sum);
Line 1,837 ⟶ 2,058:
}
 
say "The first 25 weird numbers:\n@weird";</langsyntaxhighlight>
{{out}}
<pre>
Line 1,847 ⟶ 2,068:
{{trans|Go}}
Sufficiently fast that I un-optimised it a bit to make it easier to follow.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">abundant</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">divs</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">divs</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">></span> <span style="color: #000000;">n</span>
Line 1,891 ⟶ 2,113:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<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;">"The%s\n"</span><span firststyle="color: %d#0000FF;">,{</span><span weirdstyle="color: numbers#7060A8;">join</span><span arestyle="color: %V\n#0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">,{(</span><span style="color: #000000;">MAXres</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"weird numbers"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">))})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
<pre style="font-size: 11px">
70 836 4030 5830 7192 ... 24710 25130 25690 26110 26530 (50 weird numbers)
The first 50 weird numbers are: {70,836,4030,5830,7192,7912,9272,10430,10570,10792,10990,11410,11690,12110,12530,12670,13370,13510,13790,13930,14770,15610,15890,16030,16310,
16730,16870,17272,17570,17990,18410,18830,18970,19390,19670,19810,20510,21490,21770,21910,22190,23170,23590,24290,24430,24710,25130,25690,26110,26530}
</pre>
 
Line 1,904 ⟶ 2,125:
The first 50 seem to take c. 300 ms
{{Works with|Python|3}}
<langsyntaxhighlight lang="python">'''Weird numbers'''
 
from itertools import chain, count, islice, repeat
Line 2,146 ⟶ 2,367:
# MAIN ----------------------------------------------------
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 50 weird numbers:
Line 2,162 ⟶ 2,383:
 
Approx computation time: 284 ms</pre>
 
=={{header|Quackery}}==
 
<code>properdivisors</code> is defined at [[Proper divisors#Quackery]].
 
<syntaxhighlight lang="quackery"> [ stack ] is target ( --> s )
[ stack ] is success ( --> s )
[ stack ] is makeable ( --> s )
 
[ bit makeable take
2dup & 0 !=
dip [ | makeable put ] ] is made ( n --> b )
 
[ ' [ 0 ] swap
dup target put
properdivisors
0 over witheach +
target share > not iff
[ target release
2drop false ] done
true success put
0 makeable put
witheach
[ over witheach
[ over dip
[ +
dup target share = iff
[ false success replace
drop conclude ] done
dup target share < iff
[ dup made not iff
join else drop ]
else drop ] ]
success share not if conclude
drop ]
drop
target release
makeable release
success take ] is weird ( n --> b )
 
[] 0
[ 1+
dup weird if
[ tuck join swap ]
over size 25 = until ]
drop
echo</syntaxhighlight>
 
{{out}}
 
<pre>[ 70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310 ]
</pre>
 
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 2,193 ⟶ 2,466:
(require rackunit)
(check-true (weird? 70))
(check-false (weird? 12)))</langsyntaxhighlight>
 
{{out}}
Line 2,201 ⟶ 2,474:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>sub abundant (\x) {
my @l = x.is-prime ?? 1 !! flat
1, (2 .. x.sqrt.floor).map: -> \d {
Line 2,226 ⟶ 2,499:
}
 
put "The first 25 weird numbers:\n", @weird[^25];</langsyntaxhighlight>
{{out}}
<pre>The first 25 weird numbers:
Line 2,233 ⟶ 2,506:
=={{header|REXX}}==
===vanilla version===
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays N weird numbers in a vertical format (with index).*/
parse arg n cols . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 25 /*Not specified? Then use the default.*/
Line 2,302 ⟶ 2,575:
end
end /*cp*/
end /*part*/; return 1 /*no sum equal to X, so X is weird.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,320 ⟶ 2,593:
 
This version is about &nbsp; '''300%''' &nbsp; faster than the 1<sup>st</sup> REXX version for larger amount of numbers.
<langsyntaxhighlight lang="rexx">/*REXX program finds and displays N weird numbers in a vertical format (with index).*/
parse arg n cols . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 400 /*Not specified? Then use the default.*/
Line 2,409 ⟶ 2,682:
end /*cp*/
end /*part*/
return 1 /*no sum equal to X, so X is weird.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 2,460 ⟶ 2,733:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def divisors(n)
divs = [1]
divs2 = []
Line 2,536 ⟶ 2,809:
end
 
main()</langsyntaxhighlight>
{{out}}
<pre>The first 25 weird numbers:
Line 2,542 ⟶ 2,815:
 
=={{header|Sidef}}==
<syntaxhighlight lang ="ruby">func is_pseudoperfect(n, d = n.divisors.slicefirst(0, -21), s = d.sum, m = d.end) {
 
return false if (m < 0)
Line 2,557 ⟶ 2,830:
 
func is_weird(n) {
(n.sigma > 2*n) &&  !is_pseudoperfect(n)
}
 
var w = (1..Inf -> lazy.grep(is_weird).first(25))
say "The first 25 weird numbers:\n#{w.join(' ')}"</langsyntaxhighlight>
{{out}}
<pre>
Line 2,570 ⟶ 2,843:
=={{header|Visual Basic .NET}}==
Performance is now on par with the python version, (but not quite up the Go version's performance), I applied what I could after reading the comments made by '''Hout''' on the discussion page.<br/>This program is similar to the structure of the '''Go''' example. I found a couple of tweaks here and there to help with performance. For example, the divisors list is built on a single array instead of joining two, and it calculates the sum while creating the divisors list. The divisors list is headed by the difference between "n" and the sum of the divisors. The semiperfect() function checks for equality first (rather than chopping the head from the tail list first) to save a little more time. And of course, the parallel execution.<br/><br/>A new feature is that one can calculate weird numbers up to any reasonable number, just enter a command line parameter of more than zero. Another new feature is calculating weird numbers continuously until a key is pressed (like the spigot algorithm from the [[Pi]] task) - to do so, enter a command line parameter of less than 1.<br/>This has no sieve cache, as one must "know" beforehand what number to cache up to, (for best results). Since there is no cache (runs slower), I added the parallel execution to make it run faster.<br/>I haven't let it run long enough to see how high it can get before crashing, I suspect it should happen once the weird number being tested is around Int32.MaxValue (2,147,483,647). But long before that it will slow down quite a bit. It takes around 17 minutes to get to the 10,732nd weird number, which is the first over 7 million (7,000,210).
<langsyntaxhighlight lang="vbnet">Module Module1
 
Dim resu As New List(Of Integer)
Line 2,655 ⟶ 2,928:
End If
End Sub
End Module</langsyntaxhighlight>
{{out}}
Without any command line parameters:
Line 2,672 ⟶ 2,945:
<pre>6981310 6983108 6983270 6983690 6985090 6985510 6986630 6987190 6987610 6988030 6988310 6988730 6990130 6990970 6991390 6991468 6991670 6992930 6993070 6993490 6994610 6995030 6996484 6997270 6997970 6998110 6999230 6999370 7000210 7001330 7003010 7003172 7003430 7003990 7004830 7007210 7007630 7008890 7009030
Computation time was 17m 9.0062776s for the first 10742 weird numbers.</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn divisors(n int) []int {
mut divs := [1]
mut divs2 := []int{}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
j := n / i
divs << i
if i != j {
divs2 << j
}
}
}
for i := divs.len - 1; i >= 0; i-- {
divs2 << divs[i]
}
return divs2
}
fn abundant(n int, divs []int) bool {
mut sum := 0
for div in divs {
sum += div
}
return sum > n
}
fn semiperfect(n int, divs []int) bool {
le := divs.len
if le > 0 {
h := divs[0]
t := divs[1..]
if n < h {
return semiperfect(n, t)
} else {
return n == h || semiperfect(n-h, t) || semiperfect(n, t)
}
} else {
return false
}
}
fn sieve(limit int) []bool {
// false denotes abundant and not semi-perfect.
// Only interested in even numbers >= 2
mut w := []bool{len: limit}
for i := 2; i < limit; i += 2 {
if w[i] {
continue
}
divs := divisors(i)
if !abundant(i, divs) {
w[i] = true
} else if semiperfect(i, divs) {
for j := i; j < limit; j += i {
w[j] = true
}
}
}
return w
}
fn main() {
w := sieve(17000)
mut count := 0
max := 25
println("The first 25 weird numbers are:")
for n := 2; count < max; n += 2 {
if !w[n] {
print("$n ")
count++
}
}
println('')
}</syntaxhighlight>
 
{{out}}
<pre>
The first 25 weird numbers are:
70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310
</pre>
 
=={{header|Wren}}==
{{trans|Go}}
{{libheader|Wren-math}}
{{libheader|Wren-traititerate}}
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int, Nums
import "./traititerate" for Stepped
 
var semiperfect // recursive
Line 2,726 ⟶ 3,082:
}
System.print()
System.print("\nTook %(((System.clock-start)*1000).round) milliseconds")</langsyntaxhighlight>
 
{{out}}
Line 2,734 ⟶ 3,090:
 
Took 144 milliseconds
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
This runs on a Raspberry Pi. MAlloc in other versions of XPL0 work differently.
Takes about 1.3 seconds.
<syntaxhighlight lang "XPL0">def SizeOfInt = 4;
def \IntA\ Ptr, Size;
int Array(2);
 
func Divisors(N); \Returns a list of proper divisors for N
int N;
int Divs, Divs2, Out;
int I, J, C1, C2;
[C1:= 0; C2:= 0;
Divs:= MAlloc(N * SizeOfInt / 2);
Divs2:= MAlloc(N * SizeOfInt / 2);
Divs(C1):= 1; C1:= C1+1;
I:= 2;
while I*I <= N do
[if rem(N/I) = 0 then
[J:= N/I;
Divs(C1):= I; C1:= C1+1;
if I # J then
[Divs2(C2):= J; C2:= C2+1];
];
I:= I+1;
];
Out:= MAlloc((C1+C2) * SizeOfInt);
for I:= 0 to C2-1 do
Out(I):= Divs2(I);
for I:= 0 to C1-1 do
Out(C2+I):= Divs(C1-I-1);
Array(Ptr):= Out;
Array(Size):= C1 + C2;
Release(Divs);
Release(Divs2);
return Array;
];
 
func Abundant(N, Divs); \Returns 'true' if N is abundant
int N, Divs;
int Sum, I;
[Sum:= 0;
for I:= 0 to Divs(Size)-1 do
Sum:= Sum + Divs(Ptr,I);
return Sum > N;
];
 
func Semiperfect(N, Divs); \Returns 'true' if N is semiperfect
int N, Divs;
int H, T, TA(2);
[if Divs(Size) > 0 then
[H:= Divs(Ptr,0);
T:= Divs(Ptr)+SizeOfInt;
TA(Ptr):= T;
TA(Size):= Divs(Size)-1;
if N < H then
return Semiperfect(N, TA)
else return N = H or Semiperfect(N-H, TA) or Semiperfect(N, TA);
]
else return false;
];
 
func Sieve(Limit); \Return array of weird number indexes set 'false'
int Limit; \i.e. non-abundant and non-semiperfect
int W, Divs(2), I, J;
[W:= MAlloc(Limit * SizeOfInt);
for I:= 0 to Limit-1 do W(I):= 0; \for safety
I:= 2;
while I < Limit do
[if W(I) = 0 then
[Divs:= Divisors(I);
if not Abundant(I, Divs) then
W(I):= true
else if Semiperfect(I, Divs) then
[J:= I;
while J < Limit do
[W(J):= true;
J:= J+I;
];
];
];
I:= I+2;
];
Release(Divs(Ptr));
return W;
];
 
int W, Count, Max, N;
[W:= Sieve(17000);
Count:= 0;
Max:= 25;
Text(0, "The first 25 weird numbers:^m^j");
N:= 2;
while Count < Max do
[if not W(N) then
[IntOut(0, N); ChOut(0, ^ );
Count:= Count+1;
];
N:= N+2;
];
CrLf(0);
Release(W);
]</syntaxhighlight>
{{out}}
<pre>
The first 25 weird numbers:
70 836 4030 5830 7192 7912 9272 10430 10570 10792 10990 11410 11690 12110 12530 12670 13370 13510 13790 13930 14770 15610 15890 16030 16310
</pre>
 
=={{header|zkl}}==
{{trans|Go}}
<langsyntaxhighlight lang="zkl">fcn properDivs(n){
if(n==1) return(T);
( pd:=[1..(n).toFloat().sqrt()].filter('wrap(x){ n%x==0 }) )
Line 2,764 ⟶ 3,229:
}
w
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">w,count,max := sieve(17_000), 0, 25;
println("The first 25 weird numbers are:");
foreach n in ([2..* ,2]){
Line 2,771 ⟶ 3,236:
if(count>=max) break;
}
println();</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits