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}}===
Now using the right step size and startvalue<BR>
I think base 16 is the limit. 1234...FG is to big for Uint64. GMP/ MPinteger is required.
<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..
type
tNumtoBase = record
ntb_dgt : array[0..31-
ntb_cnt,
ntb_bas :
end;
var
dgtSqrRoot : array[0..31-8] of byte;
sl : array of string;
s2Delta : array of Uint32;
Num,
sqr2B,
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;
b1 := Base-1;
dgtRoot := ((Base*b1) DIV 2) MOD b1;
//number containing 1,2..,base-1
n := 0;
For j := 1 to
Begin
n := n* base + j;
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);
// calc step size. i = occurences of same dgtroot.
i := 0;
For j := 1 to Base-1 do
if dgtSqrRoot[j] = dgtRoot then
// if i = 0 then one more digit is needed -> so no penholodigital number
step := b1 DIV i;
// calc delta of square numbers for incremnt n by step
Conv2num(deltaSqrNum,2*step*n,base);
IncNumBig(deltaSqrNum,step*step);
dNum := 2*step*step;
//all digits without 0
For j := b1 downto 1 do
//
TestSet := 0;
For j := sqr2B.ntb_cnt-1 downto 0 do
TestSet := TestSet OR (1 shl sqr2B.ntb_dgt[j]);
IF CheckSet=TestSet then
Begin
// now correct number
IncNumBig(num,i*step);
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 >
begin
writeln(sl[0],',',sl[penHoloCnt-1]);
end;
end;
Line 699 ⟶ 704:
begin
T0 := now;
setlength(sl,740);
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
There are a total of 0 penholodigital squares in base: 3
There are a total of 0 penholodigital squares in base: 4
Penholodigital squares in base: 5 are not possible
There are a total of 2 penholodigital squares in base: 6
122^2 = 15324,221^2 = 53241
There are a total of 1 penholodigital squares in base: 7
645^2 = 623514
There are a total of 1 penholodigital squares in base: 8
2453^2 = 6532471
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
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
There are a total of 20 penholodigital squares in base: 11
42045^2 = 165742A893,
There are a total of 23 penholodigital squares in base: 12
117789^2 = 135B7482A69,
Penholodigital squares in base: 13 are not possible
There are a total of 160 penholodigital squares in base: 14
1129535^2 = 126A84D79C53B,3A03226^2 = DB3962A7541C8
There are a total of 419 penholodigital squares in base: 15
4240C58^2 = 12378DA5B6EC94,EE25E4A^2 = ED4C93285671BA
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
Total runtime in s
</pre>
|