Talk:First perfect square in base n with n unique digits: Difference between revisions

→‎Minimum start for Pascal version: some help with the base 17 problem
(→‎Minimum start for Pascal version: some help with the base 17 problem)
Line 400:
:I try to update the procedure StartValueCreate.
:I first had to change the TestSet .Even 64 bit-freepascal 1 shl x is done in 32 Bit, so i got no solution running 1e12 Tests for base=32 upwards.Nggggrrrrrr ( Rumpelstiltskin , i have to put me togehther )
::Looking at the revised Pascal code, it seems that the inserted digits are not being calculated correctly for some bases (such as base 17). Here is some commented demo code written in Pascal that demonstrates a way to generate the correct information. (and a [https://tio.run/##hVRbdxo3EH7nV8xDzgGaNQbHcVsoPif1JaF1wLWT3nz8IFYCFGuljaQF49S/3R2NdmGJc9oXexnNfPPNN5ecuZSpvVmePj3t7wMXmXEgtRPWCw5czqV34A1kUsusyGDJVCE6nU4jt2ZuWQb4/5NIfW/Q@PJidH56dg7nlyePDYAvL95PTs/g9Ozi8t0oGia5l5l8YF4aDZNxNJ6g15uL0dtxgEqHh4kyJh8ePiLe2fh0dP7YKJxw6Hq9dh@9VG7QSI12Hi0Zu@/DGOGWYqQ9DOEIaQCkC2ZdH5y3Us/R2uz2Dl4dvj76/ocf3/x8ghTfvhv98uvF@/Hk8rer6w8ff//jz7/@ZtOUi9l8IT/dqUyb/LN1vliu7tcP/zQHjSWzCDxlTtQSDhpoA0DZrPCF1SjUQkTNmAJrjAczA51A4QKRtLBWIM2AQoGzQqckBb9q6RpueydH8JyKudT0BZjLFcpDfwjdAUjExwq7IbMGcS/LgLqbBmwEtEJa2INem6LK503o1r3yi0BC8@dlskpbv2AepyVVBRex@t3R@f/SZ1Kpkf66/Aj/X7Vfk8eVyBVLRevE5OsWtT2BXkJp2gkg1geDji1NdT8zwMuaBZ9vbtvP1SPUm4NbdI6fvfBJCaNTAq/KjDXFoJwYx20fmLVsfdPtdI4wFidi21xSFX2QjvtcMLs7PVjMUQ9bhGOOUBKzcJsA4/XxCABTAqC@1cODK9oZ51U7GpWWGGS0WmMjZ2IFhnOKdpAVzsOCLUW1@TEY9z/0ducGlOsQJHo9gNVCKhENPxFh4KbWOcwnljhlMQs3uulBC0SukcNqd4oPSm2ZBW2YWrG1Q0G2swz7cBCnBDXYrMSEc3Jpx9ku3yjILWw12kgqY3cIC0o6WtWv19dVxpnEa0BaxTa5OMTGggzIvaBQyIJFYzdvZOB2G15wsyV8B7K9SYkaesmUfBCQG@fkFHWrqUCgldB4mdNC0blMYinpQqR3dZ/IBBtdFjjYsupWrJBMYIbCELlbOB6SnRDRitHH1RPZIly0bIgHHcq1kHHXXcaUEigMDQTMrWBeBFim6f0bE4msixiMBAj5VKQtxmm2tyKNZvV0@N4OOR@ENaUOz09NcCi0Fqlwjtk1IVW1lTduZaUXStNo9A8SaPahSfsUvmHvOPwq7xFm3LJxdzLfFOTi0Vt9e4YpZKRTypHAIYHQRcA/naenfwE Try It Online! link])--[[User:Enter your username|Enter your username]] ([[User talk:Enter your username|talk]]) 19:36, 7 June 2019 (UTC)
<lang pascal>// demos inserted digits to minimum value...
program project1;
{$IFDEF FPC}
{$MODE DELPHI}
{$Optimization ON}
{$CODEALIGN proc=4,loop=4}
{$ENDIF}
uses
SysUtils;
const
max: NativeInt = 61;
chars: string = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz|';
var
base: NativeInt;
 
// returns the digital root of n, using current base
function dR(n: NativeInt): NativeInt;
begin
result := 0; if n = 0 then exit;
result := n MOD (base - 1); if result = 0 then result := base - 1;
end;
 
// returns a string that includes the inserted digit, using current base
function fillIn(n: NativeInt): string;
begin
result := StringReplace(Copy(chars, 1, base), IntToStr(n - 1), IntToStr(n - 1) + IntToStr(n), []);
result := chars[2] + chars[1] + Copy(result, 3, base);
end;
var
sdr: array[0..61] of NativeInt; // sdr - square digital roots, 61 = max
i, bdr, ad: NativeInt; // bdr - base digital root, ad - added digit
begin
// only a few odd bases must have digits added to the minimum value
base := 5; while base <= max do begin
// even bases don't need added digits, digital roots of odd bases are always = (base - 1) / 2
bdr := 0; if Odd(base) then bdr := base shr 1;
// make a list of the digital roots of the first few squares
for i := 1 to bdr do sdr[i - 1] := dR(i * i);
// initialize possible added digit for minimum calculation, then check for minimums
ad := base; for i := 0 to bdr - 1 do if sdr[i] >= bdr then if ad > sdr[i] then ad := sdr[i];
// the result is the smallest value greater than the base digital root, minus the bdr
Dec(ad, bdr);
// If the result (ad) is zero, then the inserted digit is unnecessary
if ad > 0 then writeln(base:2, ': ', ad:2, ' -> ', fillIn(ad));
// skip the bases that won't need added digits
Inc(base, 4);
end;
end.</lang>
{{out}}
<pre> 5: 2 -> 102234
13: 3 -> 10233456789ABC
17: 1 -> 10123456789ABCDEFG
21: 6 -> 10234566789ABCDEFGHIJK
29: 2 -> 10223456789ABCDEFGHIJKLMNOPQRS
37: 7 -> 10234567789ABCDEFGHIJKLMNOPQRSTUVWXYZa
45: 3 -> 10233456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghi
49: 1 -> 10123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklm
53: 3 -> 10233456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopq
61: 6 -> 10234566789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy</pre>