Weird numbers: Difference between revisions
m
→{{header|Wren}}: Minor tidy
No edit summary |
m (→{{header|Wren}}: Minor tidy) |
||
(17 intermediate revisions by 13 users not shown) | |||
Line 32:
{{trans|D}}
<
V divs = [1]
[Int] divs2
Line 80:
count++
I count == 25
L.break</
{{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).
<
take(25, weirds())
-- Gets there, but takes about 6 seconds on this system,
Line 265 ⟶ 344:
end script
end if
end mReturn</
{{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:
<
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 subsetOf:numberList sumsTo:target
script o
property
property someNegatives : false
on
repeat while (i > 1)
set n to item i of my
set i to i - 1
if ((n = target) or (((n < target) or (someNegatives)) and (
end repeat
return (target = beginning of my lst)
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
end subsetOf:sumsTo:
-- 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.
-- Using aliquotSum() to
-- needed is generally faster than calling properDivisors() in the first place and summing the result.
set sum to aliquotSum(n)
-- Check that no subset sums to the smaller (usually the latter) of n and sum - n.
tell (sum - n) to if (it < n) then set n to it
return (not (my subsetOf:divisors sumsTo:n))
else
return false
end if
end isWeird
Line 345 ⟶ 453:
end weirdNumbers
weirdNumbers(25)</
{{output}}
<
=={{header|C}}==
{{trans|D}}
<
#include "stdlib.h"
#include "stdbool.h"
Line 464 ⟶ 572:
free(w);
return 0;
}</
{{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}}
<
using System.Collections.Generic;
using System.Linq;
Line 549 ⟶ 657:
}
}
}</
{{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}}
<
#include <iostream>
#include <numeric>
Line 635 ⟶ 743:
std::cout << '\n';
return 0;
}</
{{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}}
<
divs = [1]
divs2 = [] of Int32
Line 732 ⟶ 840:
require "benchmark"
puts Benchmark.measure { main }
</syntaxhighlight>
{{out}}
Line 743 ⟶ 851:
=={{header|D}}==
{{trans|Kotlin}} Adding efficient "cut" condition in semiperfect recursive algorithm
<
import std.array;
import std.stdio;
Line 769 ⟶ 877:
bool semiperfect(int n, int[] divs) {
// 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);
}
}
}
Line 815 ⟶ 931:
}
writeln;
}</
{{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}}
<
let abundant (n:int) divs = Seq.sum(divs) > n
Line 852 ⟶ 968:
i <- i + 1
0 // return an integer exit code</
{{out}}
<pre>1 -> 70
Line 882 ⟶ 998:
=={{header|Factor}}==
The <code>has-sum?</code> word is a translation of the Haskell function.
<
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</
{{out}}
<pre>
Line 942 ⟶ 1,058:
=={{header|FreeBASIC}}==
<
Function GetFactors(n As Long,r() As Long) As Long
Redim r(0)
Line 997 ⟶ 1,113:
Print "first 25 done"
Sleep
</syntaxhighlight>
{{out}}
<pre>
Line 1,014 ⟶ 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.
<
import "fmt"
Line 1,091 ⟶ 1,207:
}
fmt.Println()
}</
{{out}}
Line 1,103 ⟶ 1,219:
=={{header|Haskell}}==
{{Trans|Python}}
<
weirds = filter abundantNotSemiperfect [1 ..]
Line 1,132 ⟶ 1,248:
main =
(putStrLn . unlines) $
zipWith (\i x -> show i ++ (" -> " ++ show x)) [1 ..] (take 25 weirds)</
{{Out}}
<pre>1 -> 70
Line 1,162 ⟶ 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,204 ⟶ 1,320:
weird
)
</syntaxhighlight>
<pre>
classify 20000 NB. the first 36 weird numbers
Line 1,211 ⟶ 1,327:
=={{header|Java}}==
<
import java.util.ArrayList;
import java.util.List;
Line 1,285 ⟶ 1,401:
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,319 ⟶ 1,435:
{{Trans|Python}}
{{Trans|Haskell}}
<
'use strict';
Line 1,433 ⟶ 1,549:
// MAIN ---
return main();
})();</
{{Out}}
<pre>1 -> 70
Line 1,460 ⟶ 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}}==
<
function nosuchsum(revsorted, num)
Line 1,506 ⟶ 1,696:
testweird(25)
</
<pre>
The first 25 weird numbers are:
Line 1,513 ⟶ 1,703:
=={{header|Kotlin}}==
{{trans|Go}}<
fun divisors(n: Int): List<Int> {
Line 1,577 ⟶ 1,767:
}
println()
}</
{{output}}
Line 1,587 ⟶ 1,777:
=={{header|Lua}}==
{{trans|C#}}
<
local a = {}
for i=1,n do
Line 1,697 ⟶ 1,887:
end
main()</
{{out}}
<pre>The first 25 weird numbers:
Line 1,703 ⟶ 1,893:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
HasSumQ[n_Integer, xs_List] := HasSumHelperQ[n, ReverseSort[xs]]
HasSumHelperQ[n_Integer, xs_List] := Module[{h, t},
Line 1,726 ⟶ 1,916:
]
]
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>
Line 1,734 ⟶ 1,928:
=={{header|Nim}}==
{{trans|Go}}
<
func divisors(n: int): seq[int] =
Line 1,778 ⟶ 1,972:
if not w[n]: list.add n
inc n, 2
echo list.join(" ")</
{{out}}
Line 1,787 ⟶ 1,981:
{{trans|Raku}}
{{libheader|ntheory}}
<
use feature 'say';
Line 1,823 ⟶ 2,017:
}
say "The first 25 weird numbers:\n" . join ' ', @weird;</
{{out}}
<pre>The first 25 weird numbers:
Line 1,831 ⟶ 2,025:
{{trans|Sidef}}
{{libheader|ntheory}}
<
use strict;
use ntheory qw(vecsum divisors divisor_sum);
Line 1,864 ⟶ 2,058:
}
say "The first 25 weird numbers:\n@weird";</
{{out}}
<pre>
Line 1,874 ⟶ 2,068:
{{trans|Go}}
Sufficiently fast that I un-optimised it a bit to make it easier to follow.
<!--<
<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,918 ⟶ 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;">"
<!--</
{{out}}
<pre>
70 836 4030 5830 7192 ... 24710 25130 25690 26110 26530 (50 weird numbers)
</pre>
Line 1,931 ⟶ 2,125:
The first 50 seem to take c. 300 ms
{{Works with|Python|3}}
<
from itertools import chain, count, islice, repeat
Line 2,173 ⟶ 2,367:
# MAIN ----------------------------------------------------
if __name__ == '__main__':
main()</
{{Out}}
<pre>First 50 weird numbers:
Line 2,189 ⟶ 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}}==
<
(require math/number-theory)
Line 2,220 ⟶ 2,466:
(require rackunit)
(check-true (weird? 70))
(check-false (weird? 12)))</
{{out}}
Line 2,228 ⟶ 2,474:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
my @l = x.is-prime ?? 1 !! flat
1, (2 .. x.sqrt.floor).map: -> \d {
Line 2,253 ⟶ 2,499:
}
put "The first 25 weird numbers:\n", @weird[^25];</
{{out}}
<pre>The first 25 weird numbers:
Line 2,260 ⟶ 2,506:
=={{header|REXX}}==
===vanilla version===
<
parse arg n cols . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 25 /*Not specified? Then use the default.*/
Line 2,329 ⟶ 2,575:
end
end /*cp*/
end /*part*/; return 1 /*no sum equal to X, so X is weird.*/</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,347 ⟶ 2,593:
This version is about '''300%''' faster than the 1<sup>st</sup> REXX version for larger amount of numbers.
<
parse arg n cols . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 400 /*Not specified? Then use the default.*/
Line 2,436 ⟶ 2,682:
end /*cp*/
end /*part*/
return 1 /*no sum equal to X, so X is weird.*/</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,487 ⟶ 2,733:
=={{header|Ruby}}==
<
divs = [1]
divs2 = []
Line 2,563 ⟶ 2,809:
end
main()</
{{out}}
<pre>The first 25 weird numbers:
Line 2,569 ⟶ 2,815:
=={{header|Sidef}}==
<syntaxhighlight lang
return false if (m < 0)
Line 2,584 ⟶ 2,830:
func is_weird(n) {
(n.sigma > 2*n) &&
}
var w = (1..Inf -> lazy.grep(is_weird).first(25))
say "The first 25 weird numbers:\n#{w.join(' ')}"</
{{out}}
<pre>
Line 2,597 ⟶ 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).
<
Dim resu As New List(Of Integer)
Line 2,682 ⟶ 2,928:
End If
End Sub
End Module</
{{out}}
Without any command line parameters:
Line 2,699 ⟶ 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-
<
import "./
var semiperfect // recursive
Line 2,753 ⟶ 3,082:
}
System.print()
System.print("\nTook %(((System.clock-start)*1000).round) milliseconds")</
{{out}}
Line 2,761 ⟶ 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}}
<
if(n==1) return(T);
( pd:=[1..(n).toFloat().sqrt()].filter('wrap(x){ n%x==0 }) )
Line 2,791 ⟶ 3,229:
}
w
}</
<
println("The first 25 weird numbers are:");
foreach n in ([2..* ,2]){
Line 2,798 ⟶ 3,236:
if(count>=max) break;
}
println();</
{{out}}
<pre>
|