Magic numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎Python: some code cleanup)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(18 intermediate revisions by 10 users not shown)
Line 34:
;* [[oeis:A143671|OEIS:A143671 - Count of "Magic" numbers with n digits]]
 
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Uses Algol 68G's LONG LONG INT, which has programmer definable precision, the default is sufficient for this task.
<syntaxhighlight lang="algol68">
BEGIN # count magic numbers: numbers divisible by the count of digits and #
# each left substring is also divisible by the count of its digits #
# returns the magic numbers of length d count that can be formed by #
# adding a digit to the elements of prev m #
PROC next magic = ( []LONG LONG INT prev m, INT d count )[]LONG LONG INT:
BEGIN
FLEX[ 1 : 2 000 ]LONG LONG INT m;
INT m pos := 0;
FOR i FROM LWB prev m TO UPB prev m DO
LONG LONG INT n := prev m[ i ] * 10;
FOR d FROM 0 BY IF ODD d count THEN 1 ELSE 2 FI TO 9 DO
IF ( n + d ) MOD d count = 0 THEN
IF m pos >= UPB m THEN
# need a bigger magic number buffer #
[ 1 : UPB m + 1000 ]LONG LONG INT new m;
new m[ 1 : UPB m ] := m;
m := new m
FI;
m[ m pos +:= 1 ] := n + d
FI
OD
OD;
m[ 1 : m pos ]
END # next magic # ;
# returns TRUE if n is pandigital (1-9), FALSE otherwise #
PROC is pandigital1 = ( LONG LONG INT n )BOOL:
BEGIN
INT v := SHORTEN SHORTEN n;
[ 0 : 9 ]INT digit count := []INT( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 )[ AT 0 ];
WHILE digit count[ v MOD 10 ] +:= 1;
( v OVERAB 10 ) > 0
DO SKIP OD;
BOOL result := digit count[ 0 ] = 0;
FOR i FROM 1 TO 9 WHILE result := digit count[ i ] = 1 DO SKIP OD;
result
END # is pandigital1 # ;
# find the magic numbers #
print( ( "Magic number counts by number of digits:", newline ) );
INT m count := 0; # total number of magic numbers #
INT d count := 1; # number of digits #
FLEX[ 1 : 10 ]LONG LONG INT magic := ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 );
FLEX[ 1 : 0 ]LONG LONG INT magic9;
LONG LONG INT max magic := -1;
WHILE UPB magic >= LWB magic DO
print( ( whole( d count, -5 ), ": ", whole( UPB magic, -9 ), newline ) );
m count +:= UPB magic;
max magic := magic[ UPB magic ];
IF d count = 1 THEN
# exclude 0 from the 1 digit magic numbers to find the 2 digit #
magic := ( 1, 2, 3, 4, 5, 6, 7, 8, 9 )
ELIF d count = 9 THEN
# save the 9 digit magic numbers so we can find the pandigitals #
magic9 := magic
FI;
magic := next magic( magic, d count +:= 1 )
OD;
print( ( "Total:", whole( m count, -10 ), " magic numbers", newline ) );
print( ( "Largest is ", whole( max magic, 0 ), newline ) );
# find the minimally pandigital magic numbers #
[ 1 : UPB magic9 ]LONG LONG INT pd magic;
INT pd pos := 0;
FOR i FROM LWB pd magic TO UPB pd magic DO
IF is pandigital1( magic9[ i ] ) THEN
pd magic[ pd pos +:= 1 ] := magic9[ i ]
FI
OD;
print( ( "Minimally pandigital 1-9 magic numbers: " ) );
FOR i TO pd pos DO
print( ( whole( pd magic[ i ], 0 ) ) )
OD;
print( ( newline ) );
print( ( "Minimally pandigital 0-9 magic numbers: " ) );
FOR i TO pd pos DO
print( ( whole( pd magic[ i ] * 10, 0 ) ) )
OD;
print( ( newline ) )
END
</syntaxhighlight>
{{out}}
<pre>
Magic number counts by number of digits:
1: 10
2: 45
3: 150
4: 375
5: 750
6: 1200
7: 1713
8: 2227
9: 2492
10: 2492
11: 2225
12: 2041
13: 1575
14: 1132
15: 770
16: 571
17: 335
18: 180
19: 90
20: 44
21: 18
22: 12
23: 6
24: 3
25: 1
Total: 20457 magic numbers
Largest is 3608528850368400786036725
Minimally pandigital 1-9 magic numbers: 381654729
Minimally pandigital 0-9 magic numbers: 3816547290
</pre>
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">function lmod(n, d, i, l, r) {
l = 15 - length(d - 1)
for (i = 1; i <= d; i += l) r = (r substr(n, i, l)) % d
return r
}
 
function is_pandigital(d, n) {
while (d < 10) if (!index(n, d++)) return 0
return 1
}
 
BEGIN {
do {
l = length(n = res[++i]) + 1
for (d = (l - lmod(n "0", l)) % l; d < 10; d += l)
res[++o] = n d
} while (i != o)
 
print "found " o " magic numbers"
print "the largest one is " res[o]
 
print "count by number of digits:"
l = 0
for (i = 1; i <= o; ++i) {
if ((d = length(m = res[i])) == l) {
++n
} else {
if (l) printf "%u:%u ", l, n
n = 1
l = d
}
if (l == 9 && is_pandigital(1, m))
pd1 = pd1 " " m
if (l == 10 && is_pandigital(0, m))
pd0 = pd0 " " m
}
printf "%u:%u\n", l, n
print "minimally pandigital in 1..9:" pd1
print "minimally pandigital in 0..9:" pd0
}</syntaxhighlight>
{{out}}
<pre>
found 20457 magic numbers
the largest one is 3608528850368400786036725
count by number of digits:
1:10 2:45 3:150 4:375 5:750 6:1200 7:1713 8:2227 9:2492 10:2492 11:2225 12:2041 13:1575 14:1132 15:770 16:571 17:335 18:180 19:90 20:44 21:18 22:12 23:6 24:3 25:1
minimally pandigital in 1..9: 381654729
minimally pandigital in 0..9: 3816547290
</pre>
 
=={{header|C++}}==
Line 171 ⟶ 338:
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
// Magic numbers. Nigel Galloway: February 1010th., 2023
let digs=[|0..10|]|>Array.map(System.UInt128.CreateChecked)
let fN n g=n|>List.collect(fun n->let n=n*digs[10] in [for g in digs[0..9]->n+g]|>List.filter(fun n->n%g=digs[0]))
let fG (n:int []) g=let rec fN g=if g<digs[10] then n[int g]<-n[int g]-1 else n[int(g%digs[10])]<-n[int(g%digs[10])]-1; fN (g/digs[10])
fN g; Array.forall ((=)0) n
let magic=Array.append [|[digs[0]..digs[9]]|] (Array.unfold(fun(n,g)->match n with []->None |n->let n=fN n g in Some(n,(n,g+digs[1])))([digs[1]..digs[9]],digs[2]))
magic|>Array.iteri(fun n g->printfn $"There are %d {magic numbers of length %d" |>Array.sumBy(List.length g)} Magic (n+2))numbers"
magic|>Array.iteri(fun n g->printfn "There are %d magic numbers of length %d" (List.length g) (n+1))
printfn $"Largest magic number is %A{magic.[magic.Length-2]|>List.max}"
let fG=fG [|0;1;1;1;1;1;1;1;1;1|] in printf "Minimally pan-digital(1..9) magic numbers are: "; magic[78]|>List.filter(fun n->fG [|0;1;1;1;1;1;1;1;1;1|] n)|>List.iter(printf "%A ");printfn ""
let fG=fG [|1;1;1;1;1;1;1;1;1;1|] in printf "Minimally pan-digital(01..9) magic numbers are: "; magic[89]|>List.filter(fun n->fG [|1;1;1;1;1;1;1;1;1;1|] n)|>List.iter(printf "%A ");printfn ""9
</syntaxhighlight>
{{out}}
<pre>
There are 20457 Magic numbers
There are 10 magic numbers of length 1
There are 45 magic numbers of length 2
There are 150 magic numbers of length 3
Line 213 ⟶ 383:
Minimally pan-digital(0..9) magic numbers are: 3816547290
</pre>
 
=={{header|J}}==
 
Implementation:
 
<syntaxhighlight lang=J>ispdiv=: {{0= +/(# | 10 #. ])\ 10&#.inv y}}
 
{{
if. 0>nc<'pdivs' do.
pdivs=: {{
r=. 0,d=. 1 2 3 4 5 6 7 8 9x
while. #d do.
r=. r,d=. (#~ ispdiv"0), (10*d)+/i.10
end.
}}0
end.
}}0</syntaxhighlight>
 
Task:
 
<syntaxhighlight lang=J> #pdivs NB. quantity of these "magic' numbers"
20457
>./pdivs NB. largest of these "magic numbers"
3608528850368400786036725
(~.,. #/.~) #@":@> pdivs NB. tallies by digit count
1 10
2 45
3 150
4 375
5 750
6 1200
7 1713
8 2227
9 2492
10 2492
11 2225
12 2041
13 1575
14 1132
15 770
16 571
17 335
18 180
19 90
20 44
21 18
22 12
23 6
24 3
25 1
(#~ '123456789'&-:@(/:~)@":@>)pdivs
381654729
(#~ '0123456789'&-:@(/:~)@":@>)pdivs
3816547290</syntaxhighlight>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
Line 302 ⟶ 527:
All magic numbers that are pan-digital in 0 through 9 with no repeats:
3816547290
</pre>
 
=={{header|Julia}}==
<syntaxhighlight lang="julia">function findmagics(maxdig = 26)
magics = [Int128[] for _ in 1:maxdig-1]
pushfirst!(magics, collect(one(Int128):9))
for n in 2:maxdig, i in magics[n - 1], j in 0:9
k = 10i + j
k % n == 0 && push!(magics[n], k)
end
pushfirst!(first(magics), 0) # zero is a one-digit magic number?
return magics
end
 
const magics = findmagics()
 
for (n, arr) in enumerate(magics)
println("There are $(length(arr)) magic numbers with $n digits",
isempty(arr) ? "." : " with the largest $(last(arr)).")
end
println("\nIn all, there are $(sum(map(length, magics))) magic numbers.\n")
 
println("Magic number(s) pan-digital in 1 through 9 with no repeats: ",
join(filter(n -> (d = digits(n); all(i -> count(==(i), d) == 1, 1:9)), magics[9])))
println("Magic number(s) pan-digital in 0 through 9 with no repeats: ",
join(filter(n -> (d = digits(n); all(i -> count(==(i), d) == 1, 0:9)), magics[10])))
 
</syntaxhighlight>{{out}}
<pre>
There are 10 magic numbers with 1 digits with the largest 9.
There are 45 magic numbers with 2 digits with the largest 98.
There are 150 magic numbers with 3 digits with the largest 987.
There are 375 magic numbers with 4 digits with the largest 9876.
There are 750 magic numbers with 5 digits with the largest 98765.
There are 1200 magic numbers with 6 digits with the largest 987654.
There are 1713 magic numbers with 7 digits with the largest 9876545.
There are 2227 magic numbers with 8 digits with the largest 98765456.
There are 2492 magic numbers with 9 digits with the largest 987654564.
There are 2492 magic numbers with 10 digits with the largest 9876545640.
There are 2225 magic numbers with 11 digits with the largest 98765456405.
There are 2041 magic numbers with 12 digits with the largest 987606963096.
There are 1575 magic numbers with 13 digits with the largest 9876069630960.
There are 1132 magic numbers with 14 digits with the largest 98760696309604.
There are 770 magic numbers with 15 digits with the largest 987606963096045.
There are 571 magic numbers with 16 digits with the largest 9876062430364208.
There are 335 magic numbers with 17 digits with the largest 98485872309636009.
There are 180 magic numbers with 18 digits with the largest 984450645096105672.
There are 90 magic numbers with 19 digits with the largest 9812523240364656789.
There are 44 magic numbers with 20 digits with the largest 96685896604836004260.
There are 18 magic numbers with 21 digits with the largest 966858966048360042609.
There are 12 magic numbers with 22 digits with the largest 9668589660483600426096.
There are 6 magic numbers with 23 digits with the largest 72645656402410567240820.
There are 3 magic numbers with 24 digits with the largest 402852168072900828009216.
There are 1 magic numbers with 25 digits with the largest 3608528850368400786036725.
There are 0 magic numbers with 26 digits.
 
In all, there are 20457 magic numbers.
 
Magic number(s) pan-digital in 1 through 9 with no repeats: 381654729
Magic number(s) pan-digital in 0 through 9 with no repeats: 3816547290
</pre>
 
=={{header|Nim}}==
{{libheader|Nim-Integers}}
<syntaxhighlight lang=Nim>import std/[algorithm, sequtils, strformat, strutils]
import integers
 
iterator magicNumbers(): tuple[length: int; value: Integer] =
## Yield the lengths and values of magic numbers.
var magics = toSeq(newInteger(1)..newInteger(9)) # Ignore 0 for now.
yield (1, newInteger(0))
var length = 1
while magics.len != 0:
for n in magics: yield (length, n)
var newMagics: seq[Integer]
inc length
for m in magics:
for d in 0..9:
let n = 10 * m + d
if n mod length == 0:
newMagics.add n
magics = move(newMagics)
 
func isMinimallyPandigital(n: Integer; start: char): bool =
## Return true if "n" is minimally pandigital in "start" through 9.
sorted($n) == toSeq(start..'9')
 
 
# Build list of magic numbers distributed by length.
var magicList: seq[seq[Integer]] = @[@[]]
var total = 0
for (length, n) in magicNumbers():
if length > magicList.high:
magicList.add @[]
magicList[^1].add n
inc total
 
echo &"Number of magic numbers: {insertSep($total)}"
echo &"Largest magic number: {insertSep($magicList[^1][^1])}"
 
echo "\nMagic number counts by number of digits:"
for length in 1..magicList.high:
echo &"{length:2}: {magicList[length].len}"
echo()
 
stdout.write "Minimally pandigital 1-9 magic numbers: "
for n in magicList[9]:
if n.isMinimallyPandigital('1'):
stdout.write insertSep($n), ' '
echo()
 
stdout.write "Minimally pandigital 0-9 magic numbers: "
for n in magicList[10]:
if n.isMinimallyPandigital('0'):
stdout.write insertSep($n), ' '
echo()
</syntaxhighlight>
 
{{out}}
<pre>Number of magic numbers: 20_457
Largest magic number: 3_608_528_850_368_400_786_036_725
 
Magic number counts by number of digits:
1: 10
2: 45
3: 150
4: 375
5: 750
6: 1200
7: 1713
8: 2227
9: 2492
10: 2492
11: 2225
12: 2041
13: 1575
14: 1132
15: 770
16: 571
17: 335
18: 180
19: 90
20: 44
21: 18
22: 12
23: 6
24: 3
25: 1
 
Minimally pandigital 1-9 magic numbers: 381_654_729
Minimally pandigital 0-9 magic numbers: 3_816_547_290
</pre>
 
=={{header|Pascal}}==
==={{header|Free Pascal}}===
Only Using UInt64. Therefore 18 digits are the limit.
<syntaxhighlight lang="pascal">
program MagicNUmbers;
{$IFDEF FPC}{$MODE DELPHI}{$Optimization ON,All}{$ENDIF}
{$IFDEF Windows}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;// TDatetime
const
CntMagicNUmbers = 2492;
var
MagicNumbs : array[0..CntMagicNUmbers] of Uint64;
MagicCnt : Int32;
function checkpanX_9(MinDigit : Int32):UInt64;
var
n,q : Uint64;
idx: Uint32;
testDigits,
AllDigits : set of 0..9;
begin
AllDigits := [];
For idx := 9 downto MinDigit do
include(AllDigits,idx);
For idx := 1 to MagicCnt do
begin
n := MagicNumbs[idx];
testDigits := [];
repeat
q := n DIV 10;
include(TestDigits,n-10*q);
n:= q;
until q = 0;
if TestDigits = AllDigits then
EXIT(MagicNumbs[idx]);
end;
end;
 
function ExtendMagic(dgtcnt:Int32):Boolean;
var
newMg : Uint64;
i,j,k : Int32;
begin
i := 1;
j := CntMagicNUmbers-MagicCnt+1;
Move(MagicNumbs[i],MagicNumbs[j],SizeOf(MagicNumbs[0])*MagicCnt);
if dgtcnt = 2 then //Jump over zero
inc(j);
repeat
newMg := MagicNumbs[j]*10;
k := newMg MOD dgtcnt;
IF k > 0 then
k := dgtCnt-k;
newMg += k;
while k in [0..9] do
Begin
MagicNumbs[i] := newMg;
k += dgtCnt;
newMg +=dgtcnt;
inc(i);
end;
inc(j);
until j > CntMagicNUmbers;
MagicCnt := i-1;
result := true;
end;
 
var
PAN1_9,PAN0_9: Int64;
i,sum : Int32;
Begin
MagicNumbs[1] := 0;
MagicCnt := 0;
sum := 0;
writeln('Magic number counts by number of digits and max value: ');
For i := 1 to 18 do
begin
ExtendMagic(i);
IF i = 9 then
PAN1_9 := checkpanX_9(1);
IF i = 10 then
PAN0_9 := checkpanX_9(0);
inc(sum,MagicCnt);
writeln(i:4,MagicCnt:10,MagicNumbs[MagicCnt]:19);
end;
Writeln(' Sum of MagicCnt: ',sum);
Writeln(' Pandigital number with 1..9: ', PAN1_9);
Writeln(' Pandigital number with 0..9: ', PAN0_9);
end.
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre>
Magic number counts by number of digits and max value:
1 10 9
2 45 98
3 150 987
4 375 9876
5 750 98765
6 1200 987654
7 1713 9876545
8 2227 98765456
9 2492 987654564
10 2492 9876545640
11 2225 98765456405
12 2041 987606963096
13 1575 9876069630960
14 1132 98760696309604
15 770 987606963096045
16 571 9876062430364208
17 335 98485872309636009
18 180 984450645096105672
Sum of MagicCnt: 20283
Pandigital number with 1..9: 381654729
Pandigital number with 0..9: 3816547290
</pre>
 
=={{header|Perl}}==
<syntaxhighlight lang="perl" line>
use strict;
use warnings;
use bigint;
 
my $dcnt = 1;
my @ok = my @magic = 0..9; shift @ok;
while () {
$dcnt++;
my @candidates = ();
for my $d (0..9) { push @candidates, map { 10*$_ + $d } @ok }
(@ok = grep { 0 == $_ % $dcnt } @candidates) ? push(@magic, @ok) : last;
}
 
printf "There are %d magic numbers in total.\nThe largest is %s.\n\n", scalar(@magic), $magic[-1];
 
my %M; $M{length $_}++ for @magic;
for my $k (sort { $a <=> $b } keys %M) {
printf " %6d with %3d digit%s\n", $M{$k}, $k, $k>1?'s':'';
}
 
for my $i (1,0) {
my $digits = join '', $i..9;
printf "\nMagic number(s) pan-digital in $i through 9 with no repeats: %s\n",
grep { length $_ == 10-$i and $digits eq join '', sort split '', $_ } @magic;
}
</syntaxhighlight>
{{out}}
<pre>
There are 20457 magic numbers in total.
The largest is 3608528850368400786036725.
 
10 with 1 digit
45 with 2 digits
150 with 3 digits
375 with 4 digits
750 with 5 digits
1200 with 6 digits
1713 with 7 digits
2227 with 8 digits
2492 with 9 digits
2492 with 10 digits
2225 with 11 digits
2041 with 12 digits
1575 with 13 digits
1132 with 14 digits
770 with 15 digits
571 with 16 digits
335 with 17 digits
180 with 18 digits
90 with 19 digits
44 with 20 digits
18 with 21 digits
12 with 22 digits
6 with 23 digits
3 with 24 digits
1 with 25 digits
 
Magic number(s) pan-digital in 1 through 9 with no repeats: 381654729
 
Magic number(s) pan-digital in 0 through 9 with no repeats: 3816547290
</pre>
 
=={{header|Phix}}==
 
Using strings
<!--<syntaxhighlight lang="phix">(phixonline)-->
Line 331 ⟶ 888:
<span style="color: #000000;">digits</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">&=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">"0"</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 346 ⟶ 903:
<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: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rc</span><span style="color: #0000FF;">),</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[$][$]})</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">c</span> <span style="color: #008080;">in</span> <span style="color: #000000;">rc</span> <span style="color: #008080;">do</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%,5d with %2d digit%s\n"</span><span style="color: #0000FF;">,{</span><span style="color:the #000000;">c</span><spanlargest style="color:being #0000FF;%s\n">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</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;">"s"</span><span style="color: #0000FF;">)})</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</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;">"s"</span><span style="color: #0000FF;">),</span><span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">][$]})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pandigital</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">p</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
Line 379 ⟶ 937:
 
There are:
10 with 1 digit, the largest being 9
45 with 2 digits, the largest being 98
150 with 3 digits, the largest being 987
375 with 4 digits, the largest being 9876
750 with 5 digits, the largest being 98765
1,200 with 6 digits, the largest being 987654
1,713 with 7 digits, the largest being 9876545
2,227 with 8 digits, the largest being 98765456
2,492 with 9 digits, the largest being 987654564
2,492 with 10 digits, the largest being 9876545640
2,225 with 11 digits, the largest being 98765456405
2,041 with 12 digits, the largest being 987606963096
1,575 with 13 digits, the largest being 9876069630960
1,132 with 14 digits, the largest being 98760696309604
770 with 15 digits, the largest being 987606963096045
571 with 16 digits, the largest being 9876062430364208
335 with 17 digits, the largest being 98485872309636009
180 with 18 digits, the largest being 984450645096105672
90 with 19 digits, the largest being 9812523240364656789
44 with 20 digits, the largest being 96685896604836004260
18 with 21 digits, the largest being 966858966048360042609
12 with 22 digits, the largest being 9668589660483600426096
6 with 23 digits, the largest being 72645656402410567240820
3 with 24 digits, the largest being 402852168072900828009216
1 with 25 digits, the largest being 3608528850368400786036725
 
 
All magic numbers that are pan-digital in 1 through 9 with no repeats:
Line 416 ⟶ 975:
The longest palindromic polydivisible number is:
30000600003
</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">from itertools import groupby
 
def magic_numbers(base):
hist = []
n = l = i = 0
while True:
l += 1
hist.extend((n + digit, l) for digit in range(-n % l, base, l))
i += 1
if i == len(hist):
return hist
n, l = hist[i]
n *= base
 
mn = magic_numbers(10)
print("found", len(mn), "magic numbers")
print("the largest one is", mn[-1][0])
 
print("count by number of digits:")
print(*(f"{l}:{sum(1 for _ in g)}" for l, g in groupby(l for _, l in mn)))
 
print(end="minimally pandigital in 1..9: ")
print(*(m for m, l in mn if l == 9 == len(set(str(m)) - {"0"})))
print(end="minimally pandigital in 0..9: ")
print(*(m for m, l in mn if l == 10 == len(set(str(m)))))</syntaxhighlight>
{{out}}
<pre>
found 20457 magic numbers
the largest one is 3608528850368400786036725
count by number of digits:
1:10 2:45 3:150 4:375 5:750 6:1200 7:1713 8:2227 9:2492 10:2492 11:2225 12:2041 13:1575 14:1132 15:770 16:571 17:335 18:180 19:90 20:44 21:18 22:12 23:6 24:3 25:1
minimally pandigital in 1..9: 381654729
minimally pandigital in 0..9: 3816547290
</pre>
 
Line 468 ⟶ 1,063:
 
All magic numbers that are pan-digital in 0 through 9 with no repeats: 3816547290</pre>
 
=={{header|Python}}==
<syntaxhighlight lang="python">from itertools import groupby
 
def magic_numbers(base):
hist = []
n = l = i = 0
while True:
l += 1
hist.extend((n + digit, l) for digit in range(-n % l, base, l))
i += 1
if i == len(hist):
return hist
n, l = hist[i]
n *= base
 
mn = magic_numbers(10)
print("found", len(mn), "magic numbers")
print("the largest one is", mn[-1][0])
 
print("count by number of digits:")
print(*(f"{l}:{sum(1 for _ in g)}" for l, g in groupby(l for _, l in mn)))
 
print(end="minimally pandigital in 1..9: ")
print(*(m for m, l in mn if l == 9 == len(set(str(m)) - {"0"})))
print(end="minimally pandigital in 0..9: ")
print(*(m for m, l in mn if l == 10 == len(set(str(m)))))</syntaxhighlight>
{{out}}
<pre>
found 20457 magic numbers
the largest one is 3608528850368400786036725
count by number of digits:
1:10 2:45 3:150 4:375 5:750 6:1200 7:1713 8:2227 9:2492 10:2492 11:2225 12:2041 13:1575 14:1132 15:770 16:571 17:335 18:180 19:90 20:44 21:18 22:12 23:6 24:3 25:1
minimally pandigital in 1..9: 381654729
minimally pandigital in 0..9: 3816547290
</pre>
 
=={{header|Rust}}==
Line 634 ⟶ 1,193:
{{libheader|Wren-fmt}}
This is based on the Python code in the Wikipedia article.
<syntaxhighlight lang="ecmascriptwren">import "./big" for BigInt
import "./fmt" for Fmt
 
9,476

edits