Zumkeller numbers: Difference between revisions

m
(→‎{{header|Pascal}}: improved sieve for prime decompostion runtime for 1e9 down from 474 s to 170 s tested til 1e11)
 
(24 intermediate revisions by 14 users not shown)
Line 33:
:* '''[[Abundant, deficient and perfect number classifications]]'''
:* '''[[Proper divisors]]''' , '''[[Factors of an integer]]'''
 
=={{header|11l}}==
{{trans|D}}
 
<syntaxhighlight lang="11l">F getDivisors(n)
V divs = [1, n]
V i = 2
L i * i <= n
I n % i == 0
divs [+]= i
 
V j = n I/ i
I i != j
divs [+]= j
i++
R divs
 
F isPartSum(divs, sum)
I sum == 0
R 1B
 
V le = divs.len
I le == 0
R 0B
 
V last = divs.last
[Int] newDivs
L(i) 0 .< le - 1
newDivs [+]= divs[i]
 
I last > sum
R isPartSum(newDivs, sum)
E
R isPartSum(newDivs, sum) | isPartSum(newDivs, sum - last)
 
F isZumkeller(n)
V divs = getDivisors(n)
V s = sum(divs)
 
I s % 2 == 1
R 0B
 
I n % 2 == 1
V abundance = s - 2 * n
R abundance > 0 & abundance % 2 == 0
 
R isPartSum(divs, s I/ 2)
 
print(‘The first 220 Zumkeller numbers are:’)
V i = 2
V count = 0
L count < 220
I isZumkeller(i)
print(‘#3 ’.format(i), end' ‘’)
count++
I count % 20 == 0
print()
i++
 
print("\nThe first 40 odd Zumkeller numbers are:")
i = 3
count = 0
L count < 40
I isZumkeller(i)
print(‘#5 ’.format(i), end' ‘’)
count++
I count % 10 == 0
print()
i += 2
 
print("\nThe first 40 odd Zumkeller numbers which don't end in 5 are:")
i = 3
count = 0
L count < 40
I i % 10 != 5 & isZumkeller(i)
print(‘#7 ’.format(i), end' ‘’)
count++
I count % 8 == 0
print()
i += 2</syntaxhighlight>
 
{{out}}
<pre>
The first 220 Zumkeller numbers are:
6 12 20 24 28 30 40 42 48 54 56 60 66 70 78 80 84 88 90 96
102 104 108 112 114 120 126 132 138 140 150 156 160 168 174 176 180 186 192 198
204 208 210 216 220 222 224 228 234 240 246 252 258 260 264 270 272 276 280 282
294 300 304 306 308 312 318 320 330 336 340 342 348 350 352 354 360 364 366 368
372 378 380 384 390 396 402 408 414 416 420 426 432 438 440 444 448 456 460 462
464 468 474 476 480 486 490 492 496 498 500 504 510 516 520 522 528 532 534 540
544 546 550 552 558 560 564 570 572 580 582 588 594 600 606 608 612 616 618 620
624 630 636 640 642 644 650 654 660 666 672 678 680 684 690 696 700 702 704 708
714 720 726 728 732 736 740 744 750 756 760 762 768 770 780 786 792 798 804 810
812 816 820 822 828 832 834 836 840 852 858 860 864 868 870 876 880 888 894 896
906 910 912 918 920 924 928 930 936 940 942 945 948 952 960 966 972 978 980 984
 
The first 40 odd Zumkeller numbers are:
945 1575 2205 2835 3465 4095 4725 5355 5775 5985
6435 6615 6825 7245 7425 7875 8085 8415 8505 8925
9135 9555 9765 10395 11655 12285 12705 12915 13545 14175
14805 15015 15435 16065 16695 17325 17955 18585 19215 19305
 
The first 40 odd Zumkeller numbers which don't end in 5 are:
81081 153153 171171 189189 207207 223839 243243 261261
279279 297297 351351 459459 513513 567567 621621 671517
729729 742203 783783 793611 812889 837837 891891 908523
960687 999999 1024947 1054053 1072071 1073709 1095633 1108107
1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377
</pre>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<langsyntaxhighlight lang="AArch64 Assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program zumkellex641.s */
Line 524 ⟶ 633:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{Output:}}
<pre>
Line 550 ⟶ 659:
 
=={{header|AppleScript}}==
On my machine, this takes about 0.28 seconds to perform the two main searches and a further 107 to do the stretch task. However, the latter time can be dramatically reduced to 1.7 seconds with the cheat of knowing beforehand that the first 200 or so odd Zumkellers not ending with 5 are divisible by 63. The "abundant number" optimisation's now used with odd numbers, but the cheat-free running time was only two to three seconds longer without it.
This takes about 0.38 seconds to execute the two main searches — then a further 109 for the stretch goal!
 
<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 574 ⟶ 701:
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 Zumkeller number?
on isZumkeller(n)
-- Yes if its aliquot sum is greater than or equal to it, the difference between them is even, and
script o
-- either n is odd or a subset of its proper divisors sums to half the sum of the divisors and it.
property divisors : properDivisors(n)
-- Using aliquotSum() to get the divisor sum and then calling properDivisors() too if a list's actually
end script
-- needed is generally faster than using properDivisors() in the first place and summing the result.
set sum to aliquotSum(n)
return ((sum ≥ n) and ((sum - n) mod 2 = 0) and ¬
repeat with thisDivisor in o's divisors
((n mod 2 = 1) or (my subsetOf:(properDivisors(n)) sumsTo:((sum + n) div 2))))
set sum to sum + thisDivisor
end repeat
set halfSum to sum / 2
return ((halfSum ≥ n) and (halfSum as integer = halfSum) and (canSumTo(o's divisors, halfSum)))
end isZumkeller
 
-- Task code:
-- Find and return q Zumkeller numbers, starting the search at n and continuing at the
on zumkellerNumbers(target, n, interval, filter)
-- Params:given how many requiredinterval, firstapplying numberthe toZumkeller test, intervalonly betweento numbers tested,passing the given filter.
on zumkellerNumbers(q, n, interval, filter)
-- script object imposing any further restrictions on the numbers.
script o
property zumkellers : {}
Line 615 ⟶ 747:
set counter to 0
repeat until (counter = targetq)
if ((filter's OK(n)) and (isZumkeller(n))) then
set end of o's zumkellers to n
Line 626 ⟶ 758:
end zumkellerNumbers
 
on joinText(textList, delimiter)
on format(resultList, heading, resultsPerLine, separator)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to delimiter
set txt to textList as text
set AppleScript's text item delimiters to astid
return txt
end joinText
 
on formatForDisplay(resultList, heading, resultsPerLine, separator)
script o
property input : resultList
Line 632 ⟶ 773:
end script
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to separator
set len to (count o's input)
repeat with i from 1 to len by resultsPerLine
set j to i + resultsPerLine - 1
if (j > len) then set j to len
set end of o's output to joinText(items i thru j of o's input, separator) as text
end repeat
set AppleScript's text item delimiters to linefeed
set output toreturn joinText(o's output, as textlinefeed)
end formatForDisplay
set AppleScript's text item delimiters to astid
return output
end format
 
on doTask(cheating)
set output to {}
script defaultnoFilter
on OK(n)
return true
Line 654 ⟶ 791:
end script
set header to "1st 220 Zumkeller numbers:"
set end of output to formatformatForDisplay(zumkellerNumbers(220, 1, 1, defaultnoFilter), header, 20, " ")
set header to "1st 40 odd Zumkeller numbers:"
set end of output to formatformatForDisplay(zumkellerNumbers(40, 1, 2, defaultnoFilter), header, 10, " ")
-- Stretch goal:
set header to "1st 40 odd Zumkeller numbers not ending with 5:"
script notDivisibleBy5no5Multiples
on OK(n)
return (n mod 5 > 0)
end OK
end script
if (cheating) then
set end of output to format(zumkellerNumbers(40, 1, 2, notDivisibleBy5), header, 10, " ")
-- Knowing that the HCF of the first 203 odd Zumkellers not ending with 5
set astid to AppleScript's text item delimiters
-- is 63, just check 63 and each 126th number thereafter.
set AppleScript's text item delimiters to linefeed & linefeed
-- For the 204th - 907th such numbers, the HCF reduces to 21, so adjust accordingly.
set output to output as text
-- (See Horsth's comments on the Talk page.)
set AppleScript's text item delimiters to astid
set zumkellers to zumkellerNumbers(40, 63, 126, no5Multiples)
return output
else
-- Otherwise check alternate numbers from 1.
set zumkellers to zumkellerNumbers(40, 1, 2, no5Multiples)
end if
set end of output to formatForDisplay(zumkellers, header, 10, " ")
return joinText(output, linefeed & linefeed)
end doTask
 
local cheating
doTask()</lang>
set cheating to false
doTask(cheating)</syntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">"1st 220 Zumkeller numbers:
6 12 20 24 28 30 40 42 48 54 56 60 66 70 78 80 84 88 90 96
102 104 108 112 114 120 126 132 138 140 150 156 160 168 174 176 180 186 192 198
Line 697 ⟶ 845:
351351 459459 513513 567567 621621 671517 729729 742203 783783 793611
812889 837837 891891 908523 960687 999999 1024947 1054053 1072071 1073709
1095633 1108107 1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377"</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<langsyntaxhighlight lang="ARM Assembly">
/* ARM assembly Raspberry PI */
/* program zumkeller4.s */
Line 1,379 ⟶ 1,527:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
Program start
Line 1,412 ⟶ 1,560:
=={{header|C sharp|C#}}==
{{trans|Go}}
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 1,507 ⟶ 1,655:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>The first 220 Zumkeller numbers are:
Line 1,536 ⟶ 1,684:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp>#include <iostream">
#include <cmath>
#include <vector>
Line 1,678 ⟶ 1,826:
// if we get here it ain't no zum
return false;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,723 ⟶ 1,871:
=={{header|D}}==
{{trans|C#}}
<langsyntaxhighlight lang="d">import std.algorithm;
import std.stdio;
 
Line 1,813 ⟶ 1,961:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>The first 220 Zumkeller numbers are:
Line 1,840 ⟶ 1,988:
960687 999999 1024947 1054053 1072071 1073709 1095633 1108107
1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377</pre>
 
=={{header|EasyLang}}==
<syntaxhighlight>
proc divisors n . divs[] .
divs[] = [ 1 n ]
for i = 2 to sqrt n
if n mod i = 0
j = n / i
divs[] &= i
if i <> j
divs[] &= j
.
.
.
.
func ispartsum divs[] sum .
if sum = 0
return 1
.
if len divs[] = 0
return 0
.
last = divs[len divs[]]
len divs[] -1
if last > sum
return ispartsum divs[] sum
.
if ispartsum divs[] sum = 1
return 1
.
return ispartsum divs[] (sum - last)
.
func iszumkeller n .
divisors n divs[]
for v in divs[]
sum += v
.
if sum mod 2 = 1
return 0
.
if n mod 2 = 1
abund = sum - 2 * n
return if abund > 0 and abund mod 2 = 0
.
return ispartsum divs[] (sum / 2)
.
#
print "The first 220 Zumkeller numbers are:"
i = 2
repeat
if iszumkeller i = 1
write i & " "
count += 1
.
until count = 220
i += 1
.
print "\n\nThe first 40 odd Zumkeller numbers are:"
count = 0
i = 3
repeat
if iszumkeller i = 1
write i & " "
count += 1
.
until count = 40
i += 2
.
</syntaxhighlight>
 
{{out}}
<pre>
The first 220 Zumkeller numbers are:
6 12 20 24 28 30 40 42 48 54 56 60 66 70 78 80 84 88 90 96 102 104 108 112 114 120 126 132 138 140 150 156 160 168 174 176 180 186 192 198 204 208 210 216 220 222 224 228 234 240 246 252 258 260 264 270 272 276 280 282 294 300 304 306 308 312 318 320 330 336 340 342 348 350 352 354 360 364 366 368 372 378 380 384 390 396 402 408 414 416 420 426 432 438 440 444 448 456 460 462 464 468 474 476 480 486 490 492 496 498 500 504 510 516 520 522 528 532 534 540 544 546 550 552 558 560 564 570 572 580 582 588 594 600 606 608 612 616 618 620 624 630 636 640 642 644 650 654 660 666 672 678 680 684 690 696 700 702 704 708 714 720 726 728 732 736 740 744 750 756 760 762 768 770 780 786 792 798 804 810 812 816 820 822 828 832 834 836 840 852 858 860 864 868 870 876 880 888 894 896 906 910 912 918 920 924 928 930 936 940 942 945 948 952 960 966 972 978 980 984
 
The first 40 odd Zumkeller numbers are:
945 1575 2205 2835 3465 4095 4725 5355 5775 5985 6435 6615 6825 7245 7425 7875 8085 8415 8505 8925 9135 9555 9765 10395 11655 12285 12705 12915 13545 14175 14805 15015 15435 16065 16695 17325 17955 18585 19215 19305
</pre>
 
=={{header|F_Sharp|F#}}==
This task uses [https://rosettacode.org/wiki/Sum_of_divisors#F.23]
<langsyntaxhighlight lang="fsharp">
// Zumkeller numbers: Nigel Galloway. May 16th., 2021
let rec fG n g=match g with h::_ when h>=n->h=n |h::t->fG n t || fG(n-h) t |_->false
Line 1,854 ⟶ 2,080:
Seq.initInfinite((*)2>>(+)1)|>Seq.map(fun n->(n,sod n))|>Seq.filter(fun(n,g)->fN n g)|>Seq.take 40|>Seq.iter(fun(n,_)->printf "%d " n); printfn "\n"
Seq.initInfinite((*)2>>(+)1)|>Seq.filter(fun n->n%10<>5)|>Seq.map(fun n->(n,sod n))|>Seq.filter(fun(n,g)->fN n g)|>Seq.take 40|>Seq.iter(fun(n,_)->printf "%d " n); printfn "\n"
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,863 ⟶ 2,089:
81081 153153 171171 189189 207207 223839 243243 261261 279279 297297 351351 459459 513513 567567 621621 671517 729729 742203 783783 793611 812889 837837 891891 908523 960687 999999 1024947 1054053 1072071 1073709 1095633 1108107 1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377
</pre>
 
=={{header|Factor}}==
{{works with|Factor|0.99 2019-10-06}}
<langsyntaxhighlight lang="factor">USING: combinators grouping io kernel lists lists.lazy math
math.primes.factors memoize prettyprint sequences ;
 
Line 1,906 ⟶ 2,133:
 
"First 40 odd Zumkeller numbers not ending with 5:" print
40 odd-zumkellers-no-5 8 show</langsyntaxhighlight>
{{out}}
<pre>
Line 1,937 ⟶ 2,164:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 2,027 ⟶ 2,254:
}
fmt.Println()
}</langsyntaxhighlight>
 
{{out}}
Line 2,060 ⟶ 2,287:
=={{header|Haskell}}==
{{Trans|Python}}
<langsyntaxhighlight lang="haskell">import Data.List (group, sort)
import Data.List.Split (chunksOf)
import Data.Numbers.Primes (primeFactors)
Line 2,132 ⟶ 2,359:
 
justifyRight :: Int -> Char -> String -> String
justifyRight n c = (drop . length) <*> (replicate n c <>)</langsyntaxhighlight>
{{Out}}
<pre>First 220 Zumkeller numbers:
Line 2,164 ⟶ 2,391:
14805 15015 15435 16065 16695 17325 17955 18585 19215 19305</pre>
 
=={{header|J}}==
Implementation:<syntaxhighlight lang="J>divisors=: {{ \:~ */@>,{ (^ i.@>:)&.">/ __ q: y }}
zum=: {{
if. 2|s=. +/divs=. divisors y do. 0
elseif. 2|y do. (0<k) * 0=2|k=. s-2*y
else. s=. -:s for_d. divs do. if. d<:s do. s=. s-d end. end. s=0
end.
}}@></syntaxhighlight>
 
Task examples:<syntaxhighlight lang="J"> 10 22$1+I.zum 1+i.1000 NB. first 220 Zumkeller numbers
6 12 20 24 28 30 40 42 48 54 56 60 66 70 78 80 84 88 90 96 102 104
108 112 114 120 126 132 138 140 150 156 160 168 174 176 180 186 192 198 204 208 210 216
220 222 224 228 234 240 246 252 258 260 264 270 272 276 280 282 294 300 304 306 308 312
318 320 330 336 340 342 348 350 352 354 360 364 366 368 372 378 380 384 390 396 402 408
414 416 420 426 432 438 440 444 448 456 460 462 464 468 474 476 480 486 490 492 496 498
500 504 510 516 520 522 528 532 534 540 544 546 550 552 558 560 564 570 572 580 582 588
594 600 606 608 612 616 618 620 624 630 636 640 642 644 650 654 660 666 672 678 680 684
690 696 700 702 704 708 714 720 726 728 732 736 740 744 750 756 760 762 768 770 780 786
792 798 804 810 812 816 820 822 828 832 834 836 840 852 858 860 864 868 870 876 880 888
894 896 906 910 912 918 920 924 928 930 936 940 942 945 948 952 960 966 972 978 980 984
4 10$1+2*I.zum 1+2*i.1e4 NB. first 40 odd Zumkeller numbers
945 1575 2205 2835 3465 4095 4725 5355 5775 5985
6435 6615 6825 7245 7425 7875 8085 8415 8505 8925
9135 9555 9765 10395 11655 12285 12705 12915 13545 14175
14805 15015 15435 16065 16695 17325 17955 18585 19215 19305
4 10$(#~ 0~:5|])1+2*I.zum 1+2*i.1e6 NB. first 40 odd Zumkeller numbers not divisible by 5
81081 153153 171171 189189 207207 223839 243243 261261 279279 297297
351351 459459 513513 567567 621621 671517 729729 742203 783783 793611
812889 837837 891891 908523 960687 999999 1024947 1054053 1072071 1073709
1095633 1108107 1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377</syntaxhighlight>
=={{header|Java}}==
<langsyntaxhighlight lang="java">
import java.util.ArrayList;
import java.util.Collections;
Line 2,278 ⟶ 2,535:
 
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,307 ⟶ 2,564:
1095633 1108107 1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377
</pre>
 
=={{header|jq}}==
{{Works with|jq|1.5}}
 
From a practical point of view, jq is not well-suited to these tasks,
e.g. using the program shown here, the first task (computing the first 220 Zumkeller numbers)
takes about 1 second.
 
The main point of interest here, therefore, is the partitioning
function, or rather how a generic partitioning function that
generates a stream of partitions is easily transformed into a
specialized function that prunes irrelevant partitions efficiently.
<syntaxhighlight lang="jq"># The factors, sorted
def factors:
. as $num
| reduce range(1; 1 + sqrt|floor) as $i
([];
if ($num % $i) == 0 then
($num / $i) as $r
| if $i == $r then . + [$i] else . + [$i, $r] end
else .
end
| sort) ;
 
# If the input is a sorted array of distinct non-negative integers,
# then the output will be a stream of [$x,$y] arrays,
# where $x and $y are non-empty arrays that partition the
# input, and where ($x|add) == $sum.
# If [$x,$y] is emitted, then [$y,$x] will not also be emitted.
# The items in $x appear in the same order as in the input, and similarly
# for $y.
#
def distinct_partitions($sum):
# input: [$array, $n, $lim] where $n>0
# output: a stream of arrays, $a, each with $n distinct items from $array,
# preserving the order in $array, and such that
# add == $lim
def p:
. as [$in, $n, $lim]
| if $n==1 # this condition is very common so it saves time to check early on
then ($in | bsearch($lim)) as $ix
| if $ix < 0 then empty
else [$lim]
end
else ($in|length) as $length
| if $length <= $n then empty
elif $length==$n then $in | select(add == $lim)
elif ($in[-$n:]|add) < $lim then empty
else ($in[:$n]|add) as $rsum
| if $rsum > $lim then empty
elif $rsum == $lim then "amazing" | debug | $in[:$n]
else range(0; 1 + $length - $n) as $i
| [$in[$i]] + ([$in[$i+1:], $n-1, $lim - $in[$i]]|p)
end
end
end;
range(1; (1+length)/2) as $i
| ([., $i, $sum]|p) as $pi
| [ $pi, (. - $pi)]
| select( if (.[0]|length) == (.[1]|length) then (.[0] < .[1]) else true end) #1
;
 
def zumkellerPartitions:
factors
| add as $sum
| if $sum % 2 == 1 then empty
else distinct_partitions($sum / 2)
end;
 
def is_zumkeller:
first(factors
| add as $sum
| if $sum % 2 == 1 then empty
else distinct_partitions($sum / 2)
| select( (.[0]|add) == (.[1]|add)) // ("internal error: \(.)" | debug | empty) #2
end
| true)
// false;</syntaxhighlight><syntaxhighlight lang="jq">## The tasks:
 
"First 220:", limit(220; range(2; infinite) | select(is_zumkeller)),
""
"First 40 odd:", limit(40; range(3; infinite; 2) | select(is_zumkeller))</syntaxhighlight>
{{out}}
<pre>
First 220:
6
12
20
24
28
...
984
 
First 40 odd:
945
1575
2205
2835
3465
...
19305</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function factorize(n)
Line 2,357 ⟶ 2,716:
println("\n\nFirst 40 odd Zumkeller numbers not ending with 5:")
printconditionalnum((n) -> isodd(n) && (string(n)[end] != '5') && iszumkeller(n), 40, 8)
</langsyntaxhighlight>{{out}}
<pre>
First 220 Zumkeller numbers:
Line 2,391 ⟶ 2,750:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import java.util.ArrayList
import kotlin.math.sqrt
 
Line 2,499 ⟶ 2,858:
return divisors
}
}</langsyntaxhighlight>
{{out}}
<pre>First 220 Zumkeller numbers:
Line 2,527 ⟶ 2,886:
 
=={{header|Lobster}}==
<langsyntaxhighlight lang="Lobster">import std
 
// Derived from Julia and Python versions
Line 2,587 ⟶ 2,946:
print "\n\n40 odd Zumkeller numbers:"
printZumkellers(40, true)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,623 ⟶ 2,982:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="Mathematica">ClearAll[ZumkellerQ]
ZumkellerQ[n_] := Module[{d = Divisors[n], t, ds, x},
ds = Total[d];
Line 2,649 ⟶ 3,008:
i += 2;
];
res</langsyntaxhighlight>
{{out}}
<pre>{6,12,20,24,28,30,40,42,48,54,56,60,66,70,78,80,84,88,90,96,102,104,108,112,114,120,126,132,138,140,150,156,160,168,174,176,180,186,192,198,204,208,210,216,220,222,224,228,234,240,246,252,258,260,264,270,272,276,280,282,294,300,304,306,308,312,318,320,330,336,340,342,348,350,352,354,360,364,366,368,372,378,380,384,390,396,402,408,414,416,420,426,432,438,440,444,448,456,460,462,464,468,474,476,480,486,490,492,496,498,500,504,510,516,520,522,528,532,534,540,544,546,550,552,558,560,564,570,572,580,582,588,594,600,606,608,612,616,618,620,624,630,636,640,642,644,650,654,660,666,672,678,680,684,690,696,700,702,704,708,714,720,726,728,732,736,740,744,750,756,760,762,768,770,780,786,792,798,804,810,812,816,820,822,828,832,834,836,840,852,858,860,864,868,870,876,880,888,894,896,906,910,912,918,920,924,928,930,936,940,942,945,948,952,960,966,972,978,980,984}
Line 2,656 ⟶ 3,015:
=={{header|Nim}}==
{{trans|Go}}
<langsyntaxhighlight lang="Nim">import math, strutils
 
template isEven(n: int): bool = (n and 1) == 0
Line 2,726 ⟶ 3,085:
inc count
stdout.write if count mod 8 == 0: '\n' else: ' '
inc n, 2</langsyntaxhighlight>
 
{{out}}
Line 2,754 ⟶ 3,113:
960687 999999 1024947 1054053 1072071 1073709 1095633 1108107
1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377</pre>
 
=={{header|PARI/GP}}==
{{trans|Mathematica_/_Wolfram_Language}}
<syntaxhighlight lang="PARI/GP">
\\ Define a function to check if a number is Zumkeller
isZumkeller(n) = {
my(d = divisors(n));
my(ds = sum(i=1, #d, d[i])); \\ Total of divisors
if (ds % 2, return(0)); \\ If sum of divisors is odd, return false
my(coeffs = vector(ds+1, i, 0)); \\ Create a vector to store coefficients
coeffs[1] = 1;
for(i=1, #d, coeffs = Pol(coeffs) * (1 + x^d[i]); coeffs = Vecrev(coeffs); if(#coeffs > ds + 1, coeffs = coeffs[^1])); \\ Generate coefficients
coeffs[ds \ 2 + 1] > 0; \\ Check if the middle coefficient is positive
}
 
\\ Generate a list of Zumkeller numbers
ZumkellerList(limit) = {
my(res = List(), i = 1);
while(#res < limit,
if(isZumkeller(i), listput(res, i));
i++;
);
Vec(res); \\ Convert list to vector
}
 
\\ Generate a list of odd Zumkeller numbers
OddZumkellerList(limit) = {
my(res = List(), i = 1);
while(#res < limit,
if(isZumkeller(i), listput(res, i));
i += 2; \\ Only check odd numbers
);
Vec(res); \\ Convert list to vector
}
 
\\ Call the functions to get the lists
zumkeller220 = ZumkellerList(220);
oddZumkeller40 = OddZumkellerList(40);
 
\\ Print the results
print(zumkeller220);
print(oddZumkeller40);
</syntaxhighlight>
 
{{out}}
<pre>
[6, 12, 20, 24, 28, 30, 40, 42, 48, 54, 56, 60, 66, 70, 78, 80, 84, 88, 90, 96, 102, 104, 108, 112, 114, 120, 126, 132, 138, 140, 150, 156, 160, 168, 174, 176, 180, 186, 192, 198, 204, 208, 210, 216, 220, 222, 224, 228, 234, 240, 246, 252, 258, 260, 264, 270, 272, 276, 280, 282, 294, 300, 304, 306, 308, 312, 318, 320, 330, 336, 340, 342, 348, 350, 352, 354, 360, 364, 366, 368, 372, 378, 380, 384, 390, 396, 402, 408, 414, 416, 420, 426, 432, 438, 440, 444, 448, 456, 460, 462, 464, 468, 474, 476, 480, 486, 490, 492, 496, 498, 500, 504, 510, 516, 520, 522, 528, 532, 534, 540, 544, 546, 550, 552, 558, 560, 564, 570, 572, 580, 582, 588, 594, 600, 606, 608, 612, 616, 618, 620, 624, 630, 636, 640, 642, 644, 650, 654, 660, 666, 672, 678, 680, 684, 690, 696, 700, 702, 704, 708, 714, 720, 726, 728, 732, 736, 740, 744, 750, 756, 760, 762, 768, 770, 780, 786, 792, 798, 804, 810, 812, 816, 820, 822, 828, 832, 834, 836, 840, 852, 858, 860, 864, 868, 870, 876, 880, 888, 894, 896, 906, 910, 912, 918, 920, 924, 928, 930, 936, 940, 942, 945, 948, 952, 960, 966, 972, 978, 980, 984]
[945, 1575, 2205, 2835, 3465, 4095, 4725, 5355, 5775, 5985, 6435, 6615, 6825, 7245, 7425, 7875, 8085, 8415, 8505, 8925, 9135, 9555, 9765, 10395, 11655, 12285, 12705, 12915, 13545, 14175, 14805, 15015, 15435, 16065, 16695, 17325, 17955, 18585, 19215, 19305]
</pre>
 
=={{header|Pascal}}==
Using sieve for primedecomposition<BR>
Now using the trick, that one partition sum must include n and improved recursive search.<BR>
Limit is ~1.2e11
<langsyntaxhighlight lang="pascal">program zumkeller;
//https://oeis.org/A083206/a083206.txt
{$IFDEF FPC}
Line 3,448 ⟶ 3,857:
writeln('runtime ',(GetTickCount64-T0)/1000:8:3,' s');
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,529 ⟶ 3,938:
=={{header|Perl}}==
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 3,573 ⟶ 3,982:
$n = 0; $z = '';
$z .= do { $n < 40 ? (!!($_%2 and $_%5) and is_Zumkeller($_) and ++$n and "$_ ") : last } for 1 .. Inf;
in_columns(10, $z);</langsyntaxhighlight>
 
{{out}}
Line 3,603 ⟶ 4,012:
=={{header|Phix}}==
{{trans|Go}}
<!--<lang Phix>(phixonline)-->
<syntaxhighlight lang="Phix">
<span style="color: #008080;">function</span> <span style="color: #000000;">isPartSum</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
with javascript_semantics
<span style="color: #008080;">if</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
function isPartSum(sequence f, integer l, t)
<span style="color: #008080;">if</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if t=0 then return true end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">last</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">f</span><span style="color: #0000FF;">[</span><span style="color: #000000;">l</span><span style="color: #0000FF;">]</span>
if l=0 then return false end if
<span style="color: #008080;">return</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">last</span> <span style="color: #008080;">and</span> <span style="color: #000000;">isPartSum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">-</span><span style="color: #000000;">last</span><span style="color: #0000FF;">))</span>
integer last = f[l]
<span style="color: #008080;">or</span> <span style="color: #000000;">isPartSum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">l</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
return (t>=last and isPartSum(f, l-1, t-last))
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
or isPartSum(f, l-1, t)
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">isZumkeller</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;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">factors</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
function isZumkeller(integer n)
<span style="color: #004080;">integer</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">)</span>
sequence f = factors(n,1)
<span style="color: #000080;font-style:italic;">-- an odd sum cannot be split into two equal sums</span>
integer t = sum(f)
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span> <span style="color: #008080;">return</span> <span style="color: #004600;">false</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
-- an odd sum cannot be split into two equal sums
<span style="color: #000080;font-style:italic;">-- if n is odd use 'abundant odd number' optimization</span>
if odd(t) then return false end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
-- if n is odd use 'abundant odd number' optimization
<span style="color: #004080;">integer</span> <span style="color: #000000;">abundance</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #000000;">n</span>
if odd(n) then
<span style="color: #008080;">return</span> <span style="color: #000000;">abundance</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">abundance</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span>
integer abundance := t - 2*n
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return abundance>0 and even(abundance)
<span style="color: #000080;font-style:italic;">-- if n and t both even check for any partition of t/2</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">isPartSum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
-- if n and t both even check for any partition of t/2
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return isPartSum(f, length(f), t/2)
end function
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">220</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%3d "</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">40</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%5d "</span><span style="color: #0000FF;">},</span>
sequence tests = {{220,1,0,20,"%3d %n"},
<span style="color: #0000FF;">{</span><span style="color: #000000;">40</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%7d "</span><span style="color: #0000FF;">}}</span>
{40,2,0,10,"%5d %n"},
<span style="color: #004080;">integer</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rem</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cr</span><span style="color: #0000FF;">;</span> <span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span>
{40,2,5,8,"%7d %n"}}
<span style="color: #008080;">for</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer lim, step, rem, cr; string fmt
<span style="color: #0000FF;">{</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">rem</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">cr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">fmt</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">t</span><span style="color: #0000FF;">]</span>
for t=1 to length(tests) do
<span style="color: #004080;">string</span> <span style="color: #000000;">odd</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">step</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"odd "</span><span style="color: #0000FF;">),</span>
{lim, step, rem, cr, fmt} = tests[t]
<span style="color: #000000;">wch</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rem</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"which don't end in 5 "</span><span style="color: #0000FF;">)</span>
string o = iff(step=1?"":"odd "),
<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 first %d %sZumkeller numbers %sare:\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span><span style="color: #000000;">odd</span><span style="color: #0000FF;">,</span><span style="color: #000000;">wch</span><span style="color: #0000FF;">})</span>
w = iff(rem=0?"":"which don't end in 5 ")
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">step</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
printf(1,"The first %d %sZumkeller numbers %sare:\n",{lim,o,w})
<span style="color: #008080;">while</span> <span style="color: #000000;">count</span><span style="color: #0000FF;"><</span><span style="color: #000000;">lim</span> <span style="color: #008080;">do</span>
integer i = step+1, count = 0
<span style="color: #008080;">if</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">rem</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">or</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">rem</span><span style="color: #0000FF;">)</span>
while count<lim do
<span style="color: #008080;">and</span> <span style="color: #000000;">isZumkeller</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
if (rem=0 or remainder(i,10)!=rem)
<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: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
and isZumkeller(i) then
<span style="color: #000000;">count</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
count += 1
<span style="color: #008080;">if</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cr</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
printf(1,fmt,{i,remainder(count,cr)=0})
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #000000;">i</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">step</span>
i += step
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end while
<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;">"\n"</span><span style="color: #0000FF;">)</span>
printf(1,"\n")
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end for
<!--</lang>-->
</syntaxhighlight>
{{out}}
<pre>
Line 3,684 ⟶ 4,094:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang="PicoLisp">(de propdiv (N)
(make
(for I N
Line 3,735 ⟶ 4,145:
(and
(=0 (% C 8))
(prinl) ) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 3,766 ⟶ 4,176:
===Procedural===
Modified from a footnote at OEIS A083207 (see reference in problem text) by Charles R Greathouse IV.
<langsyntaxhighlight lang="python">from sympy import divisors
 
from sympy.combinatorics.subsets import Subset
Line 3,798 ⟶ 4,208:
print("\n\n40 odd Zumkeller numbers:")
printZumkellers(40, True)
</langsyntaxhighlight>{{out}}
<pre>
220 Zumkeller numbers:
Line 3,836 ⟶ 4,246:
Relying on the standard Python libraries, as an alternative to importing SymPy:
 
<langsyntaxhighlight lang="python">'''Zumkeller numbers'''
 
from itertools import (
Line 4,038 ⟶ 4,448:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 220 Zumkeller numbers:
Line 4,075 ⟶ 4,485:
{{trans|Zkl}}
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory)
Line 4,115 ⟶ 4,525:
(newline)
(tabulate "First 40 odd Zumkeller numbers not ending in 5:"
(first-n-matching-naturals 40 (λ (n) (and (odd? n) (not (= 5 (modulo n 10))) (zum? n)))))</langsyntaxhighlight>
 
{{out}}
Line 4,138 ⟶ 4,548:
=={{header|Raku}}==
(formerly Perl 6)
{{libheader|ntheory}}
{{works with|Rakudo|2019.07.1}}
<syntaxhighlight lang="raku" line>use ntheory:from<Perl5> <factor is_prime>;
 
<lang perl6>use ntheory:from<Perl5> <factor is_prime>;
 
sub zumkeller ($range) {
$range.grep: -> $maybe {
next if $maybe < 3 or $maybe.&is_prime;
nextmy if@divisors = $maybe.&is_primefactor.combinations».reduce( &[×] ).unique.reverse;
next unless [and] @divisors > 2, @divisors %% 2, (my $sum = @divisors.sum) %% 2, ($sum /= 2) ≥ $maybe;
my @divisors = $maybe.&factor.combinations».reduce( &[*] ).unique.reverse;
next unless [&&] +@divisors > 2, +@divisors %% 2, (my $sum = sum @divisors) %% 2, ($sum /= 2) >= $maybe;
my $zumkeller = False;
if $maybe % 2 {
$zumkeller = True
} else {
TEST: loop (my $c =for 1; $c <..^ @divisors /2 2;-> ++$c) {
@divisors.combinations($c).map: -> $d {
next if (sum $d).sum != $sum;
$zumkeller = True and last TEST;
}
}
Line 4,167 ⟶ 4,575:
 
put "\nFirst 40 odd Zumkeller numbers:\n" ~
zumkeller((^Inf).map: * *× 2 + 1)[^40].rotor(10)».fmt('%7d').join: "\n";
 
# Stretch. Slow to calculate. (minutes)
put "\nFirst 40 odd Zumkeller numbers not divisible by 5:\n" ~
zumkeller(flat (^Inf).map: {my \p = 10 * $_; p+1, p+3, p+7, p+9} )[^40].rotor(10)».fmt('%7d').join: "\n";</langsyntaxhighlight>
{{out}}
<pre>First 220 Zumkeller numbers:
Line 4,200 ⟶ 4,608:
=={{header|REXX}}==
The construction of the partitions were created in the order in which the most likely partitions would match.
<langsyntaxhighlight lang="rexx">/*REXX pgm finds & shows Zumkeller numbers: 1st N; 1st odd M; 1st odd V not ending in 5.*/
parse arg n m v . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 220 /*Not specified? Then use the default.*/
Line 4,290 ⟶ 4,698:
if p1==p2 then return 1 /*Partition sums equal? Then X is Zum.*/
end /*part*/
return 0 /*no partition sum passed. X isn't Zum*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 4,313 ⟶ 4,721:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 4,478 ⟶ 4,886:
last -= 1
end
</syntaxhighlight>
</lang>
Output:
<pre>
Line 4,504 ⟶ 4,912:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Integer
def divisors
Line 4,540 ⟶ 4,948:
puts "\n#{n=40} odd Zumkeller numbers not ending with 5:"
p_enum 1.step(by: 2).lazy.select{|x| x % 5 > 0 && x.zumkeller?}.take(n)
</syntaxhighlight>
</lang>
{{out}}
<pre>220 Zumkeller numbers:
Line 4,574 ⟶ 4,982:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use std::convert::TryInto;
 
Line 4,659 ⟶ 5,067:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,677 ⟶ 5,085:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_Zumkeller(n) {
 
return false if n.is_prime
Line 4,710 ⟶ 5,118:
 
say "\nFirst 40 odd Zumkeller numbers not divisible by 5: "
say (1..Inf `by` 2 -> lazy.grep { _ % 5 != 0 }.grep(is_Zumkeller).first(40).join(' '))</langsyntaxhighlight>
{{out}}
<pre>
Line 4,724 ⟶ 5,132:
 
=={{header|Standard ML}}==
<langsyntaxhighlight lang="Standard ML">
exception Found of string ;
 
Line 4,777 ⟶ 5,185:
 
end;
</syntaxhighlight>
</lang>
call loop and output - interpreter
<langsyntaxhighlight lang="Standard ML">
- val Zumkellerlist = fn step => fn no5 =>
let
Line 4,812 ⟶ 5,220:
742203, 783783, 793611, 812889, 837837, 891891, 908523, 960687, 999999, 1024947, 1054053, 1072071, 1073709, 1095633, 1108107, 1145529,
1162161, 1198197, 1224531, 1270269, 1307691, 1324323, 1378377
</syntaxhighlight>
</lang>
 
=={{header|Swift}}==
Line 4,818 ⟶ 5,226:
{{trans|Go}}
 
<langsyntaxhighlight lang="swift">import Foundation
 
extension BinaryInteger {
Line 4,878 ⟶ 5,286:
print("First 220 zumkeller numbers are \(Array(zums.prefix(220)))")
print("First 40 odd zumkeller numbers are \(Array(oddZums.prefix(40)))")
print("First 40 odd zumkeller numbers that don't end in a 5 are: \(Array(oddZumsWithout5.prefix(40)))")</langsyntaxhighlight>
 
{{out}}
Line 4,885 ⟶ 5,293:
First 40 odd zumkeller numbers are: [945, 1575, 2205, 2835, 3465, 4095, 4725, 5355, 5775, 5985, 6435, 6615, 6825, 7245, 7425, 7875, 8085, 8415, 8505, 8925, 9135, 9555, 9765, 10395, 11655, 12285, 12705, 12915, 13545, 14175, 14805, 15015, 15435, 16065, 16695, 17325, 17955, 18585, 19215, 19305]
First 40 odd zumkeller numbers that don't end in a 5 are: [81081, 153153, 171171, 189189, 207207, 223839, 243243, 261261, 279279, 297297, 351351, 459459, 513513, 567567, 621621, 671517, 729729, 742203, 783783, 793611, 812889, 837837, 891891, 908523, 960687, 999999, 1024947, 1054053, 1072071, 1073709, 1095633, 1108107, 1145529, 1162161, 1198197, 1224531, 1270269, 1307691, 1324323, 1378377]</pre>
 
=={{header|Typescript}}==
{{trans|Go}}
<syntaxhighlight lang="typescript">
/**
* return an array of divisors of a number(n)
* @params {number} n The number to find divisors from
* @return {number[]} divisors of n
*/
function getDivisors(n: number): number[] {
//initialize divisors array
let divisors: number[] = [1, n]
//loop through all numbers from 2 to sqrt(n)
for (let i = 2; i*i <= n; i++) {
// if i is a divisor of n
if (n % i == 0) {
// add i to divisors array
divisors.push(i);
// quotient of n/i is also a divisor of n
let j = n/i;
// if quotient is not equal to i
if (i != j) {
// add quotient to divisors array
divisors.push(j);
}
}
}
 
return divisors
}
/**
* return sum of an array of number
* @param {number[]} arr The array we need to sum
* @return {number} sum of arr
*/
function getSum(arr: number[]): number {
return arr.reduce((prev, curr) => prev + curr, 0)
}
/**
* check if there is a subset of divisors which sums to a specific number
* @param {number[]} divs The array of divisors
* @param {number} sum The number to check if there's a subset of divisors which sums to it
* @return {boolean} true if sum is 0, false if divisors length is 0
*/
function isPartSum(divs: number[], sum: number): boolean {
// if sum is 0, the partition is sum up to the number(sum)
if (sum == 0) return true;
//get length of divisors array
let len = divs.length;
// if divisors array is empty the partion doesnt not sum up to the number(sum)
if (len == 0) return false;
//get last element of divisors array
let last = divs[len - 1];
//create a copy of divisors array without the last element
const newDivs = [...divs];
newDivs.pop();
// if last element is greater than sum
if (last > sum) {
// recursively check if there's a subset of divisors which sums to sum using the new divisors array
return isPartSum(newDivs, sum);
}
// recursively check if there's a subset of divisors which sums to sum using the new divisors array
// or if there's a subset of divisors which sums to sum - last using the new divisors array
return isPartSum(newDivs, sum) || isPartSum(newDivs, sum - last);
}
/**
* check if a number is a Zumkeller number
* @param {number} n The number to check if it's a Zumkeller number
* @returns {boolean} true if n is a Zumkeller number, false otherwise
*/
function isZumkeller(n: number): boolean {
// get divisors of n
let divs = getDivisors(n);
// get sum of divisors of n
let sum = getSum(divs);
// if sum is odd can't be split into two partitions with equal sums
if (sum % 2 == 1) return false;
// if n is odd use 'abundant odd number' optimization
if (n % 2 == 1) {
let abundance = sum - 2 * n
return abundance > 0 && abundance%2 == 0;
}
// if n and sum are both even check if there's a partition which totals sum / 2
return isPartSum(divs, sum/2);
}
/**
* find x zumkeller numbers
* @param {number} x The number of zumkeller numbers to find
* @returns {number[]} array of x zumkeller numbers
*/
function getXZumkelers(x: number): number[] {
let zumkellers: number[] = [];
let i = 2;
let count= 0;
while (count < x) {
if (isZumkeller(i)) {
zumkellers.push(i);
count++;
}
i++;
}
 
return zumkellers;
}
 
/**
* find x Odd Zumkeller numbers
* @param {number} x The number of odd zumkeller numbers to find
* @returns {number[]} array of x odd zumkeller numbers
*/
function getXOddZumkelers(x: number): number[] {
let oddZumkellers: number[] = [];
let i = 3;
let count = 0;
while (count < x) {
if (isZumkeller(i)) {
oddZumkellers.push(i);
count++;
}
i += 2;
}
 
return oddZumkellers;
}
 
/**
* find x odd zumkeller number which are not end with 5
* @param {number} x The number of odd zumkeller numbers to find
* @returns {number[]} array of x odd zumkeller numbers
*/
function getXOddZumkellersNotEndWith5(x: number): number[] {
let oddZumkellers: number[] = [];
let i = 3;
let count = 0;
while (count < x) {
if (isZumkeller(i) && i % 10 != 5) {
oddZumkellers.push(i);
count++;
}
i += 2;
}
 
return oddZumkellers;
}
 
//get the first 220 zumkeller numbers
console.log("First 220 Zumkeller numbers: ", getXZumkelers(220));
 
//get the first 40 odd zumkeller numbers
console.log("First 40 odd Zumkeller numbers: ", getXOddZumkelers(40));
 
//get the first 40 odd zumkeller numbers which are not end with 5
console.log("First 40 odd Zumkeller numbers which are not end with 5: ", getXOddZumkellersNotEndWith5(40));
</syntaxhighlight>
 
{{out}}
<pre>
"First 220 Zumkeller numbers: ", [6, 12, 20, 24, 28, 30, 40, 42, 48, 54, 56, 60, 66, 70, 78, 80, 84, 88, 90, 96, 102, 104, 108, 112, 114, 120, 126, 132, 138, 140, 150, 156, 160, 168, 174, 176, 180, 186, 192, 198, 204, 208, 210, 216, 220, 222, 224, 228, 234, 240, 246, 252, 258, 260, 264, 270, 272, 276, 280, 282, 294, 300, 304, 306, 308, 312, 318, 320, 330, 336, 340, 342, 348, 350, 352, 354, 360, 364, 366, 368, 372, 378, 380, 384, 390, 396, 402, 408, 414, 416, 420, 426, 432, 438, 440, 444, 448, 456, 460, 462, 464, 468, 474, 476, 480, 486, 490, 492, 496, 498, 500, 504, 510, 516, 520, 522, 528, 532, 534, 540, 544, 546, 550, 552, 558, 560, 564, 570, 572, 580, 582, 588, 594, 600, 606, 608, 612, 616, 618, 620, 624, 630, 636, 640, 642, 644, 650, 654, 660, 666, 672, 678, 680, 684, 690, 696, 700, 702, 704, 708, 714, 720, 726, 728, 732, 736, 740, 744, 750, 756, 760, 762, 768, 770, 780, 786, 792, 798, 804, 810, 812, 816, 820, 822, 828, 832, 834, 836, 840, 852, 858, 860, 864, 868, 870, 876, 880, 888, 894, 896, 906, 910, 912, 918, 920, 924, 928, 930, 936, 940, 942, 945, 948, 952, 960, 966, 972, 978, 980, 984]
"First 40 odd Zumkeller numbers: ", [945, 1575, 2205, 2835, 3465, 4095, 4725, 5355, 5775, 5985, 6435, 6615, 6825, 7245, 7425, 7875, 8085, 8415, 8505, 8925, 9135, 9555, 9765, 10395, 11655, 12285, 12705, 12915, 13545, 14175, 14805, 15015, 15435, 16065, 16695, 17325, 17955, 18585, 19215, 19305]
"First 40 odd Zumkeller numbers which are not end with 5: ", [81081, 153153, 171171, 189189, 207207, 223839, 243243, 261261, 279279, 297297, 351351, 459459, 513513, 567567, 621621, 671517, 729729, 742203, 783783, 793611, 812889, 837837, 891891, 908523, 960687, 999999, 1024947, 1054053, 1072071, 1073709, 1095633, 1108107, 1145529, 1162161, 1198197, 1224531, 1270269, 1307691, 1324323, 1378377]
</pre>
 
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module Module1
Function GetDivisors(n As Integer) As List(Of Integer)
Dim divs As New List(Of Integer) From {
Line 4,987 ⟶ 5,561:
End While
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>The first 220 Zumkeller numbers are:
Line 5,014 ⟶ 5,588:
960687 999999 1024947 1054053 1072071 1073709 1095633 1108107
1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn get_divisors(n int) []int {
mut divs := [1, n]
for i := 2; i*i <= n; i++ {
if n%i == 0 {
j := n / i
divs << i
if i != j {
divs << j
}
}
}
return divs
}
fn sum(divs []int) int {
mut sum := 0
for div in divs {
sum += div
}
return sum
}
fn is_part_sum(d []int, sum int) bool {
mut divs := d.clone()
if sum == 0 {
return true
}
le := divs.len
if le == 0 {
return false
}
last := divs[le-1]
divs = divs[0 .. le-1]
if last > sum {
return is_part_sum(divs, sum)
}
return is_part_sum(divs, sum) || is_part_sum(divs, sum-last)
}
fn is_zumkeller(n int) bool {
divs := get_divisors(n)
s := sum(divs)
// if sum is odd can't be split into two partitions with equal sums
if s%2 == 1 {
return false
}
// if n is odd use 'abundant odd number' optimization
if n%2 == 1 {
abundance := s - 2*n
return abundance > 0 && abundance%2 == 0
}
// if n and sum are both even check if there's a partition which totals sum / 2
return is_part_sum(divs, s/2)
}
fn main() {
println("The first 220 Zumkeller numbers are:")
for i, count := 2, 0; count < 220; i++ {
if is_zumkeller(i) {
print("${i:3} ")
count++
if count%20 == 0 {
println('')
}
}
}
println("\nThe first 40 odd Zumkeller numbers are:")
for i, count := 3, 0; count < 40; i += 2 {
if is_zumkeller(i) {
print("${i:5} ")
count++
if count%10 == 0 {
println('')
}
}
}
println("\nThe first 40 odd Zumkeller numbers which don't end in 5 are:")
for i, count := 3, 0; count < 40; i += 2 {
if (i % 10 != 5) && is_zumkeller(i) {
print("${i:7} ")
count++
if count%8 == 0 {
println('')
}
}
}
println('')
}</syntaxhighlight>
 
{{out}}
<pre>
The first 220 Zumkeller numbers are:
6 12 20 24 28 30 40 42 48 54 56 60 66 70 78 80 84 88 90 96
102 104 108 112 114 120 126 132 138 140 150 156 160 168 174 176 180 186 192 198
204 208 210 216 220 222 224 228 234 240 246 252 258 260 264 270 272 276 280 282
294 300 304 306 308 312 318 320 330 336 340 342 348 350 352 354 360 364 366 368
372 378 380 384 390 396 402 408 414 416 420 426 432 438 440 444 448 456 460 462
464 468 474 476 480 486 490 492 496 498 500 504 510 516 520 522 528 532 534 540
544 546 550 552 558 560 564 570 572 580 582 588 594 600 606 608 612 616 618 620
624 630 636 640 642 644 650 654 660 666 672 678 680 684 690 696 700 702 704 708
714 720 726 728 732 736 740 744 750 756 760 762 768 770 780 786 792 798 804 810
812 816 820 822 828 832 834 836 840 852 858 860 864 868 870 876 880 888 894 896
906 910 912 918 920 924 928 930 936 940 942 945 948 952 960 966 972 978 980 984
 
The first 40 odd Zumkeller numbers are:
945 1575 2205 2835 3465 4095 4725 5355 5775 5985
6435 6615 6825 7245 7425 7875 8085 8415 8505 8925
9135 9555 9765 10395 11655 12285 12705 12915 13545 14175
14805 15015 15435 16065 16695 17325 17955 18585 19215 19305
 
The first 40 odd Zumkeller numbers which don't end in 5 are:
81081 153153 171171 189189 207207 223839 243243 261261
279279 297297 351351 459459 513513 567567 621621 671517
729729 742203 783783 793611 812889 837837 891891 908523
960687 999999 1024947 1054053 1072071 1073709 1095633 1108107
1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377
</pre>
 
=={{header|Wren}}==
Line 5,020 ⟶ 5,714:
{{libheader|Wren-fmt}}
I've reversed the order of the recursive calls in the last line of the ''isPartSum'' function which, as noted in the Phix entry, seems to make little difference to Go but (as one might have expected) speeds up this Wren script enormously. The first part is now near instant but was taking several minutes previously. Overall it's now only about 5.5 times slower than Go itself which is a good result for the Wren interpreter.
<langsyntaxhighlight ecmascriptlang="wren">import "./math" for Int, Nums
import "./fmt" for Fmt
import "io" for Stdout
 
Line 5,086 ⟶ 5,780:
i = i + 2
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 5,119 ⟶ 5,813:
=={{header|zkl}}==
{{trans|Julia}} {{trans|Go}}
<langsyntaxhighlight lang="zkl">fcn properDivs(n){ // does not include n
// if(n==1) return(T); // we con't care about this case
( pd:=[1..(n).toFloat().sqrt()].filter('wrap(x){ n%x==0 }) )
Line 5,142 ⟶ 5,836:
}
canSum(sum/2,ds) and n or Void.Skip // sum is even
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println("First 220 Zumkeller numbers:");
zw:=[2..].tweak(isZumkellerW);
do(11){ zw.walk(20).pump(String,"%4d ".fmt).println() }
Line 5,153 ⟶ 5,847:
println("\nThe first 40 odd Zumkeller numbers which don't end in 5 are:");
zw:=[3..*, 2].tweak(fcn(n){ if(n%5) isZumkellerW(n) else Void.Skip });
do(5){ zw.walk(8).pump(String,"%7d ".fmt).println() }</langsyntaxhighlight>
{{out}}
<pre style="font-size:83%">
2,083

edits