Jump to content

Weird numbers: Difference between revisions

Line 1,496:
{{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|Nim}}==
{{trans|Go}}
<lang Nim>import algorithm, math, strutils
 
func divisors(n: int): seq[int] =
var smallDivs = @[1]
for i in 2..sqrt(n.toFloat).int:
if n mod i == 0:
let j = n div i
smallDivs.add i
if i != j: result.add j
result.add reversed(smallDivs)
 
func abundant(n: int; divs: seq[int]): bool {.inline.}=
sum(divs) > n
 
func semiperfect(n: int; divs: seq[int]): bool =
if divs.len > 0:
let h = divs[0]
let t = divs[1..^1]
result = if n < h: semiperfect(n, t)
else: n == h or semiperfect(n - h, t) or semiperfect(n, t)
 
func sieve(limit: int): seq[bool] =
# False denotes abundant and not semi-perfect.
# Only interested in even numbers >= 2.
result.setLen(limit)
for i in countup(2, limit - 1, 2):
if result[i]: continue
let divs = divisors(i)
if not abundant(i, divs):
result[i] = true
elif semiperfect(i, divs):
for j in countup(i, limit - 1, i):
result[j] = true
 
 
const Max = 25
let w = sieve(17_000)
var list: seq[int]
 
echo "The first 25 weird numbers are:"
var n = 2
while list.len != Max:
if not w[n]: list.add n
inc n, 2
echo list.join(" ")</lang>
 
{{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>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.