Penholodigital squares: Difference between revisions

m
→‎{{header|Free Pascal}}: changed to use a step size TIO.RUN from 50 s downto 7.2s ~ 7 times
(→‎jq: Fix inefficiency)
m (→‎{{header|Free Pascal}}: changed to use a step size TIO.RUN from 50 s downto 7.2s ~ 7 times)
Line 461:
=={{header|Pascal}}==
==={{header|Free Pascal}}===
nearlyNearly copy and paste of pandigital square numbers.<BR>
Now using the right step size and startvalue<BR>
Calc GCD of deltas between the roots.Mostly not 1.<BR>
I think base 16 is the limit. 1234...FG is to big for Uint64. GMP/ MPinteger is required.
base 17 none found.Base 18 starts late, base 19 no start found within 20 min.
<syntaxhighlight lang="pascal">program penholodigital;
//Find the smallest number n to base b, so that n*n includes all
//digits of base b without 0
{$IFDEF FPC}{$MODE DELPHI}{$Optimization ON,All}{$ENDIF}
{$IFDEF Windows}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
const
charSet : array[0..3616] of char ='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ABCDEF';
type
tNumtoBase = record
ntb_dgt : array[0..31-48] of byte;
ntb_cnt,
ntb_bas : WordInt32;
end;
var
dgtSqrRoot : array[0..31-8] of byte;
sl : array of string;
s2Delta : array of Uint32;
Num,
sqr2B,
deltaNumdeltaSqrNum : tNumtoBase;
 
function gcd(A, B: Uint32): Uint32;
var
Rest: Uint32;
begin
while B <> 0 do
begin
Rest := A mod B;
A := B;
B := Rest;
end;
Result := A;
end;
 
procedure Conv2num(var num:tNumtoBase;n:Uint64;base:NativeUint);
Line 618 ⟶ 607:
var
n,penHoloCnt : Uint64;
b1,i,j,TestSet,CheckSet,dgtRoot,step,dNum : NativeInt;
Begin
penHoloCnt := 0;
setlength(sl,740);
b1 := Base-1;
setlength(s2Delta,740);
dgtRoot := ((Base*b1) DIV 2) MOD b1;
//number containing 1,2..,base-1
//number containing 1,2..,base-1
n := 0;
For j := 1 to Base-1b1 do
Begin
n := n* base + j;
n dgtSqrRoot[j] := trunc(sqrt(n)j*j) MOD b1;
end;
n := trunc(sqrt(n));
 
// increment n til it fits (n*n) MOD b1 = dgtroot
j := B1;
repeat
if (n*n) MOD b1 = dgtroot then
BREAK;
inc(n);
dec(j);
until j = 0;
Conv2num(sqr2B,n*n,base);
Conv2num(Num,n,base);
deltaNum := num;
AddNum(deltaNum,deltaNum);
IncNum(deltaNum,1);
 
// calc step size. i = occurences of same dgtroot.
i := 0;
For j := 1 to Base-1 do
//all digits without 0
if dgtSqrRoot[j] = dgtRoot then
CheckSet := 0;
For j := base-1 downto 1 doinc(i);
// if i = 0 then one more digit is needed -> so no penholodigital number
CheckSet := CheckSet OR (1 shl j);
penHoloCntif :=i <> 0; then
repeatBegin
step := b1 DIV i;
//count used digits
// calc delta of square numbers for incremnt n by step
TestSet := 0;
Conv2num(deltaSqrNum,2*step*n,base);
For j := sqr2B.ntb_cnt-1 downto 0 do
IncNumBig(deltaSqrNum,step*step);
TestSet := TestSet OR (1 shl sqr2B.ntb_dgt[j]);
dNum := 2*step*step;
IF CheckSet=TestSet then
 
Begin
//all digits without 0
IncNumBig(num,i);
s2delta[penHoloCnt]CheckSet := i0;
For j := b1 downto 1 do
sl[penHoloCnt] := Format('%s^2 = %s',[OutNum(Num),OutNum(sqr2B)]);
incCheckSet := CheckSet OR (penHoloCnt1 shl j);
i := 0;
end;repeat
//nextcount squareused numberdigits
TestSet := 0;
AddNum(sqr2B,deltaNum);
For j := sqr2B.ntb_cnt-1 downto 0 do
IncNum(deltaNum,2);
TestSet := TestSet OR (1 shl sqr2B.ntb_dgt[j]);
inc(i);
IF CheckSet=TestSet then
until sqr2B.ntb_cnt >= base;
Begin
// now correct number
Writeln('There are a total of ',penHoloCnt,' penholodigital squares in base: ',base:2);
IncNumBig(num,i*step);
if (penHoloCnt > 0) AND (base < 14) then
s2delta[penHoloCnt] := i*step;
sl[penHoloCnt] := Format('%s^2 = %s',[OutNum(Num),OutNum(sqr2B)]);
inc(penHoloCnt);
i := 0;
end;
//next square number
AddNum(sqr2B,deltaSqrNum);
IncNumBig(deltaSqrNum,dNum);// dnum maybe >= base
inc(i);
until sqr2B.ntb_cnt >= base;
end;
if i = 0 then
Begin
Writeln('Penholodigital squares in base: ',base:2,' are not possible');
EXIT;
end
else
Writeln('There are a total of ',penHoloCnt,' penholodigital squares in base: ',base:2);
if (penHoloCnt > 0) AND (base < 11) then
begin
j := 0;
Line 673 ⟶ 693:
end
else
if penHoloCnt > 01 then
begin
writeln(sl[0],',',sl[penHoloCnt-1]);
end;
j := 1;
IF penHoloCnt> 4 then
Begin
//omit first delta s2delta[0], caused by estimaing first value
j := gcd(s2delta[1],s2delta[2]);
For i := penHoloCnt-1 downto 3 do
begin
j := gcd(s2delta[i],j);
IF j = 1 then
BREAK;
end;
end;
writeln('GGT of delta :',j);
 
end;
 
Line 699 ⟶ 704:
begin
T0 := now;
setlength(sl,740);
For base := 2 to 16 do
setlength(s2Delta,740);
For base := 2 to 17 do
Test(base);
writeln('Total runtime in s ',(now-T0)*86400:10:3);
Line 707 ⟶ 714:
{{out|@TIO.RUN}}
<pre style="height:40ex;overflow:scroll;">
 
There are a total of 1 penholodigital squares in base: 2
1^2 = 1
GCD of delta :1
There are a total of 0 penholodigital squares in base: 3
GCD of delta :1
There are a total of 0 penholodigital squares in base: 4
Penholodigital squares in base: 5 are not possible
GCD of delta :1
There are a total of 0 penholodigital squares in base: 5
GCD of delta :1
There are a total of 2 penholodigital squares in base: 6
122^2 = 15324,221^2 = 53241
GCD of delta :1
There are a total of 1 penholodigital squares in base: 7
645^2 = 623514
GCD of delta :1
There are a total of 1 penholodigital squares in base: 8
2453^2 = 6532471
GCD of delta :1
There are a total of 10 penholodigital squares in base: 9
3825^2 = 16328547,3847^2 = 16523874,4617^2 = 23875614
Line 731 ⟶ 730:
6844^2 = 53184267,7285^2 = 58624317,7821^2 = 68573241
8554^2 = 82314657
GCD of delta :4
There are a total of 30 penholodigital squares in base: 10
11826^2 = 139854276,12363^2 = 152843769,12543^2 = 157326849
Line 743 ⟶ 741:
26733^2 = 714653289,27129^2 = 735982641,27273^2 = 743816529
29034^2 = 842973156,29106^2 = 847159236,30384^2 = 923187456
GCD of delta :3
There are a total of 20 penholodigital squares in base: 11
42045^2 = 165742A893,43152A3A39^2 = 173A652894,44926^2 = 18792A6453983251A764
47149^2 = 1A67395824,47257^2 = 1A76392485,52071^2 = 249A758631
54457^2 = 2719634A85,55979^2 = 286A795314,59597^2 = 314672A895
632A4^2 = 3671A89245,64069^2 = 376198A254,68335^2 = 41697528A3
71485^2 = 46928A7153,81196^2 = 5A79286413,83608^2 = 632A741859
86074^2 = 6713498A25,89468^2 = 7148563A29,91429^2 = 76315982A4
93319^2 = 795186A234,A3A39^2 = 983251A764
GCD of delta :10
There are a total of 23 penholodigital squares in base: 12
117789^2 = 135B7482A69,16357B319A37^2 = 23A5B976481,16762B^2 = 24AB53798619B2573468A1
Penholodigital squares in base: 13 are not possible
16906B^2 = 25386749BA1,173434^2 = 26B859A3714,178278^2 = 2835BA17694
1A1993^2 = 34A8125B769,1A3595^2 = 354A279B681,1B0451^2 = 3824B7569A1
1B7545^2 = 3A5B2487961,2084A9^2 = 42A1583B769,235273^2 = 5287BA13469
2528B5^2 = 5B23A879641,25B564^2 = 62937B5A814,262174^2 = 63A8527B194
285A44^2 = 73B615A8294,29A977^2 = 7B9284A5361,2A7617^2 = 83AB5479261
2B0144^2 = 8617B35A294,307381^2 = 93825A67B41,310828^2 = 96528AB7314
319488^2 = 9AB65823714,319A37^2 = 9B2573468A1
GCD of delta :11
There are a total of 0 penholodigital squares in base: 13
GCD of delta :1
There are a total of 160 penholodigital squares in base: 14
1129535^2 = 126A84D79C53B,3A03226^2 = DB3962A7541C8
GCD of delta :13
There are a total of 419 penholodigital squares in base: 15
4240C58^2 = 12378DA5B6EC94,EE25E4A^2 = ED4C93285671BA
GCD of delta :14
There are a total of 740 penholodigital squares in base: 16
11156EB6^2 = 123DA7F85BCE964,3FD8F786^2 = FEC81B69573DA24
Penholodigital squares in base: 17 are not possible
GCD of delta :15
Total runtime in s 50 7.873159 @home 1.573 s
 
@home:
There are a total of 0 penholodigital squares in base: 17 ( 1min 47 )
Starting base 18 takes a lot of time
base 18 delta
11150FC0G^2 = 123CD8ABH5G79F6E4 9,026,292,072
111B9DC9B^2 = 12489573CFGBAHE6D 12,270,685
111HF0AAD^2 = 1253FCA98B6DG4EH7 11,890,820
11223514F^2 = 1258F7CA3E4BDGH69 4,435,130
112237HG2^2 = 1258FDG67B9CHE3A4 17,051
1122775FB^2 = 1259637EGF84AHBCD 416,007
....stopped // GCD of delta 17 ? )
base 19 no startvalue after 20 min..
</pre>
 
132

edits