Magic numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|ALGOL 68}}: Avoid counting the single digit magic numbers twice)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(10 intermediate revisions by 6 users not shown)
Line 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}}==
Line 473 ⟶ 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 502 ⟶ 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 517 ⟶ 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 550 ⟶ 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 805 ⟶ 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