Zumkeller numbers: Difference between revisions
Content deleted Content added
→{{header|Pascal}}: new recursive search: only 853112 recursions for n = 10884600 with count of divisors 72 . 205283 odd abundant numbers less than 10^8 takes now 65 s |
|||
(29 intermediate revisions by 16 users not shown) | |||
Line 1:
{{task|Prime Numbers}}
Zumkeller numbers are the set of numbers whose divisors can be partitioned into two disjoint sets that sum to the same value. Each sum must contain divisor values that are not in the other sum, and all of the divisors must be in one or the other. There are no restrictions on ''how'' the divisors are partitioned, only that the two partition sums are equal.
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}}
<
/* 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>
{{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.
<
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 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 Zumkeller number?
on isZumkeller(n)
-- Yes if its aliquot sum is greater than or equal to it, the difference between them is even, and
-- either n is odd or a subset of its proper divisors sums to half the sum of the divisors and it.
-- Using aliquotSum() to get the divisor sum and then calling properDivisors() too if a list's actually
-- 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 ¬
((n mod 2 = 1) or (my subsetOf:(properDivisors(n)) sumsTo:((sum + n) div 2))))
end isZumkeller
-- Task code:
-- Find and return q Zumkeller numbers, starting the search at n and continuing at the
on zumkellerNumbers(q, n, interval, filter)
script o
property zumkellers : {}
Line 615 ⟶ 747:
set counter to 0
repeat until (counter =
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)
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 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)
end repeat
end formatForDisplay
on doTask(cheating)
set output to {}
script
on OK(n)
return true
Line 654 ⟶ 791:
end script
set header to "1st 220 Zumkeller numbers:"
set end of output to
set header to "1st 40 odd Zumkeller numbers:"
set end of output to
-- Stretch goal:
set header to "1st 40 odd Zumkeller numbers not ending with 5:"
script
on OK(n)
return (n mod 5 > 0)
end OK
end script
if (cheating) then
-- Knowing that the HCF of the first 203 odd Zumkellers not ending with 5
-- is 63, just check 63 and each 126th number thereafter.
-- For the 204th - 907th such numbers, the HCF reduces to 21, so adjust accordingly.
-- (See Horsth's comments on the Talk page.)
set zumkellers to zumkellerNumbers(40, 63, 126, no5Multiples)
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
set cheating to false
doTask(cheating)</syntaxhighlight>
{{output}}
<
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"</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<
/* ARM assembly Raspberry PI */
/* program zumkeller4.s */
Line 1,379 ⟶ 1,527:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
Program start
Line 1,412 ⟶ 1,560:
=={{header|C sharp|C#}}==
{{trans|Go}}
<
using System.Collections.Generic;
using System.Linq;
Line 1,507 ⟶ 1,655:
}
}
}</
{{out}}
<pre>The first 220 Zumkeller numbers are:
Line 1,536 ⟶ 1,684:
=={{header|C++}}==
<
#include <cmath>
#include <vector>
Line 1,678 ⟶ 1,826:
// if we get here it ain't no zum
return false;
}</
{{out}}
<pre>
Line 1,723 ⟶ 1,871:
=={{header|D}}==
{{trans|C#}}
<
import std.stdio;
Line 1,813 ⟶ 1,961:
}
}
}</
{{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]
<
// 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>
{{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}}
<
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</
{{out}}
<pre>
Line 1,937 ⟶ 2,164:
=={{header|Go}}==
<
import "fmt"
Line 2,027 ⟶ 2,254:
}
fmt.Println()
}</
{{out}}
Line 2,060 ⟶ 2,287:
=={{header|Haskell}}==
{{Trans|Python}}
<
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 <>)</
{{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}}==
<
import java.util.ArrayList;
import java.util.Collections;
Line 2,278 ⟶ 2,535:
}
</syntaxhighlight>
{{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}}==
<
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)
</
<pre>
First 220 Zumkeller numbers:
Line 2,391 ⟶ 2,750:
=={{header|Kotlin}}==
{{trans|Java}}
<
import kotlin.math.sqrt
Line 2,499 ⟶ 2,858:
return divisors
}
}</
{{out}}
<pre>First 220 Zumkeller numbers:
Line 2,527 ⟶ 2,886:
=={{header|Lobster}}==
<
// Derived from Julia and Python versions
Line 2,587 ⟶ 2,946:
print "\n\n40 odd Zumkeller numbers:"
printZumkellers(40, true)
</syntaxhighlight>
{{out}}
<pre>
Line 2,623 ⟶ 2,982:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
ZumkellerQ[n_] := Module[{d = Divisors[n], t, ds, x},
ds = Total[d];
Line 2,649 ⟶ 3,008:
i += 2;
];
res</
{{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}}
<
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</
{{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
<
//https://oeis.org/A083206/a083206.txt
{$IFDEF FPC}
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$COPERATORS ON}
// {$O+,I+}
{$ELSE}
{$APPTYPE CONSOLE}
Line 2,771 ⟶ 3,181:
//######################################################################
//prime decomposition
const
//HCN(86) > 1.2E11 = 128,501,493,120 count of divs = 4096 7 3 1 1 1 1 1 1 1
HCN_DivCnt = 4096;
//stop never ending recursion
RECCOUNTMAX = 100*1000*1000;
DELTAMAX = 1000*1000;
type
tItem =
tDivisors = array [0..
tpDivisor =
const
SizePrDeFe = 12697;//*72 <= 1 or 2 Mb ~ level 2 cache -32kB for DIVS
type
tdigits = packed
end;
//the first number with 11 different divisors =
// 2*3*5*7*11*13*17*19*23*29*31 = 2E11
tprimeFac = packed record
pfRemain : Uint64; //n div (p[0]^[pPot[0] *...) can handle primes <=821641^2 = 6.7e11
pfpotMax : array[0..9] of byte; //10 = 66
pfMaxIdx : Uint16; //68
pfDivCnt : Uint32; //72
end;
tPrimeDecompField = array[0..SizePrDeFe-1] of tprimeFac;
tPrimes = array[0..65535] of Uint32;
var
SmallPrimes:
//######################################################################
//prime decomposition
procedure InitSmallPrimes;
//only odd numbers
const
MAXLIMIT = (821641-1) shr 1;
var
pr : array[0..MAXLIMIT] of byte;
p,j,d,flipflop :NativeUInt;
Begin
SmallPrimes[0] := 2;
fillchar(pr[0],SizeOf(pr),#0);
p := 0;
repeat
repeat
j :=
if
j += d;
until j>MAXLIMIT;
until false;
SmallPrimes[1] := 3;
SmallPrimes[2] := 5;
j := 3;
d := 7;
flipflop := 3-1;
p := 3;
repeat
if pr[p] = 0 then
begin
SmallPrimes[j] := d;
inc(j);
d += 2*flipflop;
p+=flipflop;
flipflop := 3-flipflop;
until (p > MAXLIMIT) OR (j>High(SmallPrimes));
end;
function OutPots(const pD:tprimeFac;n:NativeInt):Ansistring;
var
s: String[31];
Begin
str(n,s);
result := s+' :';
with pd do
begin
str(pfDivCnt:3,s);
result += s+' : ';
For n := 0 to pfMaxIdx-1 do
Begin
str(pFpotPrim[n],s);
result += s;
if pfpotMax[n] >1 then
Begin
str(pfpotMax[n],s);
result += '^'+s;
end;
end;
If pfRemain >1 then
Begin
str(pfRemain,s);
result += '*'+s;
end;
str(pfSumOfDivs,s);
result += '_SoD_'+s+'<';
end;
end;
function CnvtoBASE(var dgt:tDigits;n:Uint64;base:NativeUint):NativeInt;
//n must be multiple of base
var
q,r: Uint64;
i : NativeInt;
Begin
with dgt do
Begin
fillchar(dgtDgts,SizeOf(dgtDgts),#0);
i := 0;
// dgtNum:= n;
n := n div base;
result := 0;
repeat
r := n;
q := n div base;
r -= q*base;
n := q;
dgtDgts[i] := r;
inc(i);
until (q = 0);
result := 0;
while (result<i) AND (dgtDgts[result] = 0) do
inc(result);
inc(result);
end;
end;
function IncByBaseInBase(var dgt:tDigits;base:NativeInt):NativeInt;
var
q :NativeInt;
Begin
with dgt do
Begin
q := dgtDgts[result]+1;
//
if q
begin
repeat
dgtDgts[result] := 0;
inc(result);
q := dgtDgts[result]+1;
until q <> base;
end;
result +=1;
end;
procedure SieveOneSieve(var pdf:tPrimeDecompField;n:nativeUInt);
var
dgt:tDigits;
i, j, k,pr,fac : NativeUInt;
begin
//init
for i := 0 to High(pdf) do
with pdf[i] do
Begin
pfSumOfDivs := 1;
pfRemain :=
end;
//first 2
i :=
repeat
begin
end;
i +=
until i
// i now index in SmallPrimes
i := 0;
repeat
//search next prime that is in bounds of sieve
repeat
if i >= High(SmallPrimes) then
pr :=
k :=
if (k = pr)
if k < SizePrDeFe
until
if i >= High(SmallPrimes) then
//no need to use higher
j := CnvtoBASE(dgt,n+k,pr);
repeat
Begin
pfDivCnt
fac
until j<=
end;
j := IncByBaseInBase(dgt,pr);
until k >= SizePrDeFe;
until false;
//correct sum of & count of divisors
for i := 0 to High(pdf) do
Begin
begin
pfDivCnt
end;
end;
end;
end;
//prime decomposition
//######################################################################
procedure Init_Check_rec(const pD:tprimeFac;var Divs,SumOfDivs:tDivisors);forward;
var
{$ALIGN 32}
PrimeDecompField:tPrimeDecompField;
{$ALIGN 32}
Divs :tDivisors;
SumOfDivs : tDivisors;
DivUsedIdx : tDivisors;
pDiv :tpDivisor;
T0: Int64;
count,rec_Cnt: NativeInt;
depth : Int32;
finished :Boolean;
procedure Check_rek_depth(SoD : Int64;i: NativeInt);
var
sum : Int64;
begin
if finished then
EXIT;
inc(rec_Cnt);
WHILE (i>0) AND (pDiv[i]>SoD) do
dec(i);
while i >= 0
Begin
DivUsedIdx[depth] := pDiv[i];
if sum
begin
finished :=
end;
if (i>= 0) AND (sum <=
if finished then
// DivUsedIdx[depth] := 0;
end;
procedure Out_One_Sol(const pd:tprimefac;n:NativeUInt;isZK : Boolean);
var
Begin
if n< 7 then
exit;
with pd do
begin
writeln(OutPots(pD,n));
if isZK then
Begin
Init_Check_rec(pD,Divs,SumOfDivs);
Check_rek_depth(pfSumOfDivs shr 1-n,pFDivCnt-1);
write(pfSumOfDivs shr 1:10,' = ');
sum := n;
while depth >= 0 do
Begin
end;
write(n,' = ',sum);
end
else
write(' no zumkeller ');
end;
end;
Line 3,097 ⟶ 3,526:
end;
procedure GetDivs(const pD:tprimeFac;var Divs,SumOfDivs:tDivisors);
var
pDivs : tpDivisor;
i,len,j,l,p,k: Int32;
Begin
i :=
pDivs := @Divs[0];
pDivs[0] := 1;
len := 1;
l := 1;
Begin
For i := 0 to pfMaxIdx-1 do
begin
//Multiply every divisor before with the new primefactors
//and append them to the list
k :=
p :=
pPot :=1;
repeat
Line 3,123 ⟶ 3,554:
end;
dec(k);
until k<=0;
len := l;
end;
p := pfRemain;
If p >1 then
begin
For j := 0 to len-1 do
Begin
end;
len := l;
end;
end;
//Sort. Insertsort much faster than QuickSort in this special case
InsertSort(pDivs,0,len-1);
pPot := 0;
For i := 0 to len-1 do
begin
SumOfDivs[i] := pPot;
end;
end;
procedure Init_Check_rec(const pD:tprimeFac;var Divs,SumOfDivs:tDivisors);
begin
GetDivs(pD,Divs,SUmOfDivs);
finished := false;
depth := 0;
pDiv := @Divs[0];
end;
Line 3,217 ⟶ 3,591:
sum : Int64;
begin
if finished then
EXIT;
if rec_Cnt >RECCOUNTMAX then
begin
rec_Cnt := -1;
finished := true;
exit;
end;
inc(rec_Cnt);
WHILE (i>0) AND (pDiv[i]>SoD) do
dec(i);
while i >= 0 do
Begin
sum := SoD-pDiv[i];
if sum = 0 then
begin
finished := true;
EXIT;
end;
dec(i);
if
Check_rek(sum,i);
if finished then
EXIT;
end;
end;
function GetZumKeller(n:
var
SoD,sum : Int64;
Div_cnt,i,pracLmt: NativeInt;
begin
//sum must be even and n not deficient
if Odd(SoD) or (SoD<2*n) THEN
Line 3,260 ⟶ 3,635:
If SoD < 2 then //0,1 is always true
Exit(true);
Div_cnt :=
if Not(odd(n)) then
Line 3,268 ⟶ 3,643:
//Now one needs to get the divisors
pracLmt:= 0;
if Not(odd(n)) then
begin
For i := 1 to Div_Cnt do
Begin
sum := SumOfDivs[i];
If (sum+1<Divs[i+1]) AND (sum<SoD) then
IF (sum>=SoD) then break;
end;
if pracLmt = 0 then
Exit(true);
end;
//number is practical followed by one big prime
if pracLmt = (Div_Cnt-1) shr 1 then
begin
i := SoD mod Divs[pracLmt+1];
with pD do
begin
if pfRemain > 1 then
EXIT((pfRemain<=i) OR (i<=sum))
else
EXIT((pfpotPrim[pfMaxIdx-1]<=i)OR (i<=sum));
end;
end;
Begin
IF Div_cnt <= HCN_DivCnt then
Check_rek(SoD,Div_cnt-1);
IF rec_Cnt = -1 then
exit(
exit(finished);
end;
end;
result := false;
end;
var
Max: NativeUInt;
procedure Init_Sieve(n:NativeUint);
//Init Sieve i,oFs are Global
begin
i := n MOD SizePrDeFe;
Ofs := (n DIV SizePrDeFe)*SizePrDeFe;
SieveOneSieve(PrimeDecompField,Ofs);
end;
procedure GetSmall(MaxIdx:Int32);
var
ZK: Array of Uint32;
Begin
If MaxIdx<1 then
EXIT;
writeln('The first ',MaxIdx,' zumkeller numbers');
setlength(ZK,MaxIdx);
idx := Low(ZK);
repeat
if GetZumKeller(n,
inc(
end;
inc(i);
inc(n);
If i > High(PrimeDecompField) then
begin
inc(
SieveOneSieve(PrimeDecompField,Ofs);
end;
until idx
For idx := 0 to MaxIdx-1 do
begin
if idx MOD 20 = 0 then
writeln;
write(ZK[idx]:4);
end;
setlength(ZK,0);
writeln;
writeln;
end;
procedure GetOdd(MaxIdx:Int32);
var
ZK: Array of Uint32;
idx: UInt32;
Begin
If MaxIdx<1 then
EXIT;
writeln('The first odd 40 zumkeller numbers');
n := 1;
Init_Sieve(n);
setlength(ZK,MaxIdx);
idx := Low(ZK);
repeat
if GetZumKeller(n,PrimeDecompField[i]) then
Begin
ZK[idx] := n;
inc(idx);
end;
inc(i,2);
inc(n,2);
begin
inc(
SieveOneSieve(PrimeDecompField,Ofs);
end;
until
For idx := 0 to MaxIdx-1 do
begin
if idx MOD (80 DIV 8) = 0 then
writeln;
write(ZK[idx]:8);
end;
setlength(ZK,0);
writeln;
writeln;
end;
procedure GetOddNot5(MaxIdx:Int32);
var
ZK: Array of Uint32;
idx: UInt32;
Begin
If MaxIdx<1 then
EXIT;
writeln('The first odd 40 zumkeller numbers not ending in 5');
n := 1;
Init_Sieve(n);
setlength(
idx := Low(ZK);
repeat
if GetZumKeller(n,
inc(
end;
inc(i,2);
inc(n,2);
begin
inc(i,2);
inc(n,2);
end;
If i > High(PrimeDecompField) then
begin
dec(i,SizePrDeFe);
inc(ofs,SizePrDeFe);
SieveOneSieve(PrimeDecompField,Ofs);
end;
until idx >= MaxIdx;
For idx := 0 to MaxIdx-1 do
begin
if idx MOD (80 DIV 8) = 0 then
writeln;
write(ZK[idx]:8);
end;
setlength(ZK,0);
writeln;
writeln;
end;
BEGIN
InitSmallPrimes;
T0 := GetTickCount64;
GetSmall(220);
GetOdd(40);
GetOddNot5(40);
writeln;
n := 1;//8996229720;//1;
Init_Sieve(n);
writeln('Start ',n,' at ',i);
T0 := GetTickCount64;
MAX := (n DIV DELTAMAX+1)*DELTAMAX;
count := 0;
repeat
writeln('Count of zumkeller numbers up to ',MAX:12);
repeat
if GetZumKeller(n,PrimeDecompField[i]) then
inc(count);
inc(i);
inc(n);
If i > High(PrimeDecompField) then
begin
dec(i,SizePrDeFe);
inc(ofs,SizePrDeFe);
SieveOneSieve(PrimeDecompField,Ofs);
end;
until n > MAX;
writeln(n-1:10,' tested found ',count:10,' ratio ',count/n:10:7);
MAX += DELTAMAX;
until MAX>10*DELTAMAX;
writeln('runtime ',(GetTickCount64-T0)/1000:8:3,' s');
writeln;
writeln('Count of recursion 59,641,327 for 8,996,229,720');
n := 8996229720;
Init_Sieve(n);
T0 := GetTickCount64;
Out_One_Sol(PrimeDecompField[i],n,true);
writeln;
writeln('runtime ',(GetTickCount64-T0)/1000:8:3,' s');
END.
</syntaxhighlight>
{{out}}
<pre>
TIO.RUN
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 3,414 ⟶ 3,875:
906 910 912 918 920 924 928 930 936 940 942 945 948 952 960 966 972 978 980 984
The first odd 40 zumkeller numbers
945 1575 2205 2835 3465 4095 4725 5355 5775 5985
6435
9135
14805
The first odd 40 zumkeller numbers not ending in 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
Start 1 at 1
Count of zumkeller numbers up to 1000000
1000000 tested found 229026 ratio 0.2290258
Count of zumkeller numbers up to 2000000
2000000 tested found 457658 ratio 0.2288289
Count of zumkeller numbers up to 3000000
3000000 tested found 686048 ratio 0.2286826
Count of zumkeller numbers up to 4000000
4000000 tested found 914806 ratio 0.2287014
Count of zumkeller numbers up to 5000000
5000000 tested found 1143521 ratio 0.2287042
Count of zumkeller numbers up to 6000000
6000000 tested found 1372208 ratio 0.2287013
Count of zumkeller numbers up to 7000000
7000000 tested found 1600977 ratio 0.2287110
Count of zumkeller numbers up to 8000000
8000000 tested found 1829932 ratio 0.2287415
Count of zumkeller numbers up to 9000000
9000000 tested found 2058883 ratio 0.2287648
Count of zumkeller numbers up to 10000000
10000000 tested found 2287889 ratio 0.2287889
runtime 1.268 s
//zumkeller number with highest recursion count til 1e11
Count of recursion 59,641,327 for 8,996,229,720
8996229720 : 96 : 2^3*3^2*5*2237*11171_SoD_29253435120<
14626717560 = 36+45+60+72+90+120+180+360+201330+804312+805320+2010780+4021560+1124528715+4498114860+8996229720 = 14626717560
runtime 7.068 s // at home 9.5s
Real time: 8.689 s CPU share: 98.74 %
//at home til 1e11 with 85 numbers with recursion count > 1e8
9900000000 tested found 2262797501 ratio 0.2285654 recursion 10.479
runtime 48.805 s
Count of zumkeller numbers up to 10000000000
rek_ -1 @ 9998443080 : 96 : 2^3*3^2*5*3041*9133_SoD_32509184760<
10000000000 tested found 2285655276 ratio 0.2285655 recursion 10.520
runtime 28.976 s
real 40m7,478s user 40m7,039s sys 0m0,057s
only 4 til 4,512,612,672
out_1e10.txt:104: rek_ -1 @ 584818848 : 72 : 2^5*3^2*1423*1427_SoD_1665413568<
out_1e10.txt:105: rek_ -1 @ 589754016 : 72 : 2^5*3^2*1429*1433_SoD_1679457780<
out_1e10.txt:174: rek_ -1 @ 1956249450 : 72 : 2*3^2*5^2*2083*2087_SoD_5260832928<
out_1e10.txt:291: rek_ -1 @ 4512612672 : 84 : 2^6*3^2*2797*2801_SoD_12943833396<
</pre>
=={{header|Perl}}==
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 3,669 ⟶ 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);</
{{out}}
Line 3,699 ⟶ 4,012:
=={{header|Phix}}==
{{trans|Go}}
<!--
<syntaxhighlight lang="Phix">
with javascript_semantics
function isPartSum(sequence f, integer l, t)
if t=0 then return true end if
if l=0 then return false end if
integer last = f[l]
return (t>=last and isPartSum(f, l-1, t-last))
or isPartSum(f, l-1, t)
end function
function isZumkeller(integer n)
sequence f = factors(n,1)
integer t = sum(f)
-- an odd sum cannot be split into two equal sums
if odd(t) then return false end if
-- if n is odd use 'abundant odd number' optimization
if odd(n) then
integer abundance := t - 2*n
return abundance>0 and even(abundance)
end if
-- if n and t both even check for any partition of t/2
return isPartSum(f, length(f), t/2)
end function
sequence tests = {{220,1,0,20,"%3d %n"},
{40,2,0,10,"%5d %n"},
{40,2,5,8,"%7d %n"}}
integer lim, step, rem, cr; string fmt
for t=1 to length(tests) do
{lim, step, rem, cr, fmt} = tests[t]
string o = iff(step=1?"":"odd "),
w = iff(rem=0?"":"which don't end in 5 ")
printf(1,"The first %d %sZumkeller numbers %sare:\n",{lim,o,w})
integer i = step+1, count = 0
while count<lim do
if (rem=0 or remainder(i,10)!=rem)
and isZumkeller(i) then
count += 1
printf(1,fmt,{i,remainder(count,cr)=0})
end if
i += step
end while
printf(1,"\n")
end for
</syntaxhighlight>
{{out}}
<pre>
Line 3,780 ⟶ 4,094:
=={{header|PicoLisp}}==
<
(make
(for I N
Line 3,831 ⟶ 4,145:
(and
(=0 (% C 8))
(prinl) ) ) )</
{{out}}
<pre>
Line 3,862 ⟶ 4,176:
===Procedural===
Modified from a footnote at OEIS A083207 (see reference in problem text) by Charles R Greathouse IV.
<
from sympy.combinatorics.subsets import Subset
Line 3,894 ⟶ 4,208:
print("\n\n40 odd Zumkeller numbers:")
printZumkellers(40, True)
</
<pre>
220 Zumkeller numbers:
Line 3,932 ⟶ 4,246:
Relying on the standard Python libraries, as an alternative to importing SymPy:
<
from itertools import (
Line 4,134 ⟶ 4,448:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>First 220 Zumkeller numbers:
Line 4,171 ⟶ 4,485:
{{trans|Zkl}}
<
(require math/number-theory)
Line 4,211 ⟶ 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)))))</
{{out}}
Line 4,234 ⟶ 4,548:
=={{header|Raku}}==
(formerly Perl 6)
{{libheader|ntheory}}
<syntaxhighlight lang="raku" line>use ntheory:from<Perl5> <factor is_prime>;
sub zumkeller ($range) {
$range.grep: -> $maybe {
next if $maybe < 3 or $maybe.&is_prime;
next unless [and] @divisors > 2, @divisors %% 2, (my $sum = @divisors.sum) %% 2, ($sum /= 2) ≥ $maybe;
my $zumkeller = False;
if $maybe % 2 {
$zumkeller = True
} else {
TEST:
@divisors.combinations($c).map: -> $d {
next if
$zumkeller = True and last TEST
}
}
Line 4,263 ⟶ 4,575:
put "\nFirst 40 odd Zumkeller numbers:\n" ~
zumkeller((^Inf).map: *
# 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";</
{{out}}
<pre>First 220 Zumkeller numbers:
Line 4,296 ⟶ 4,608:
=={{header|REXX}}==
The construction of the partitions were created in the order in which the most likely partitions would match.
<
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,386 ⟶ 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*/</
{{out|output|text= when using the default inputs:}}
<pre>
Line 4,409 ⟶ 4,721:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 4,574 ⟶ 4,886:
last -= 1
end
</syntaxhighlight>
Output:
<pre>
Line 4,600 ⟶ 4,912:
=={{header|Ruby}}==
<
def divisors
Line 4,636 ⟶ 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>
{{out}}
<pre>220 Zumkeller numbers:
Line 4,670 ⟶ 4,982:
=={{header|Rust}}==
<
use std::convert::TryInto;
Line 4,755 ⟶ 5,067:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 4,773 ⟶ 5,085:
=={{header|Sidef}}==
<
return false if n.is_prime
Line 4,806 ⟶ 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(' '))</
{{out}}
<pre>
Line 4,820 ⟶ 5,132:
=={{header|Standard ML}}==
<
exception Found of string ;
Line 4,873 ⟶ 5,185:
end;
</syntaxhighlight>
call loop and output - interpreter
<
- val Zumkellerlist = fn step => fn no5 =>
let
Line 4,908 ⟶ 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>
=={{header|Swift}}==
Line 4,914 ⟶ 5,226:
{{trans|Go}}
<
extension BinaryInteger {
Line 4,974 ⟶ 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)))")</
{{out}}
Line 4,981 ⟶ 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#}}
<
Function GetDivisors(n As Integer) As List(Of Integer)
Dim divs As New List(Of Integer) From {
Line 5,083 ⟶ 5,561:
End While
End Sub
End Module</
{{out}}
<pre>The first 220 Zumkeller numbers are:
Line 5,110 ⟶ 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,116 ⟶ 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.
<
import "./fmt" for Fmt
import "io" for Stdout
Line 5,182 ⟶ 5,780:
i = i + 2
}
System.print()</
{{out}}
Line 5,215 ⟶ 5,813:
=={{header|zkl}}==
{{trans|Julia}} {{trans|Go}}
<
// 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,238 ⟶ 5,836:
}
canSum(sum/2,ds) and n or Void.Skip // sum is even
}</
<
zw:=[2..].tweak(isZumkellerW);
do(11){ zw.walk(20).pump(String,"%4d ".fmt).println() }
Line 5,249 ⟶ 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() }</
{{out}}
<pre style="font-size:83%">
Line 5,278 ⟶ 5,876:
1145529 1162161 1198197 1224531 1270269 1307691 1324323 1378377
</pre>
[[Link title]]
|