Palindromic gapful numbers: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 1,311: | Line 1,311: | ||
<syntaxhighlight lang="Delphi"> |
<syntaxhighlight lang="Delphi"> |
||
{-------------Library Routines ----------------------------------------------------------------} |
|||
function GetNKPalindrome(N,K: int64): int64; |
|||
{Get Nth Palindrome with K-number of digits} |
|||
{N = Left half, Right = Reversed(left) a } |
|||
var Temp,H1,H2,I: int64; |
|||
begin |
|||
{Get left digit count - depends on K being odd/even} |
|||
if (K and 1)<>0 then Temp:=K div 2 else Temp:=K div 2 - 1; |
|||
{Get power of 10 digits} |
|||
H1:=trunc(Power(10, Temp)); |
|||
{Add in N} |
|||
H1:=H1 + N - 1; |
|||
H2:=H1; |
|||
{ If K is odd, truncate the last digit} |
|||
if (k and 1)<>0 then H2:=H2 div 10; |
|||
{Reverse H2 and add to H1} |
|||
while H2>0 do |
|||
begin |
|||
H1:=H1 * 10 + (H2 mod 10); |
|||
H2:=H2 div 10; |
|||
end; |
|||
Result:=H1; |
|||
end; |
|||
function GetPalDigits(N: int64; var Offset: int64): integer; |
|||
{Get number of digits and offset for Nth Palindrome} |
|||
{Used to feed GetNKPalindrome to find Nth Palindrome} |
|||
var R1,R2,Step: int64; |
|||
begin |
|||
R1:=0; |
|||
{Step through number of digits} |
|||
for Result:=1 to 36 do |
|||
begin |
|||
{Calculate new Range step: 9,9,90,90,90,900,900...} |
|||
if (Result and 1)<>0 then Step:=9 * Trunc(Power(10,Result div 2)); |
|||
{Calculate R2} |
|||
R2:=(R1 + Step)-1; |
|||
{See if N falls between R1 and R2} |
|||
if (N>=R1) and (N<=R2) then |
|||
begin |
|||
{Calculate offset and exit} |
|||
Offset:=(N - R1)+1; |
|||
exit; |
|||
end; |
|||
R1:=R2+1; |
|||
end; |
|||
end; |
|||
function GetNthPalindrome(N: integer): int64; |
|||
{Get the Nth Palindrome number} |
|||
var D,Off: int64; |
|||
begin |
|||
D:=GetPalDigits(N,Off); |
|||
Result:=GetNKPalindrome(Off,D); |
|||
end; |
|||
procedure GetPalindromeList(Count: integer; var Pals: TInt64DynArray); |
|||
{Get a list of the first "Count"-number of palinedromes (Fast)} |
|||
var D,I,Inx,Max: integer; |
|||
begin |
|||
{Set array length} |
|||
SetLength(Pals,Count); |
|||
Inx:=0; |
|||
{Handle palindromes up to 18 digits} |
|||
for D:=1 to 18 do |
|||
begin |
|||
{Get maximum count for palindrom of D digits} |
|||
if (D and 1)=1 then Max:=Trunc(Power(10,(D + 1) div 2)) |
|||
else Max:=Trunc(Power(10,D div 2)); |
|||
{Step through all the numbers half the size of the number of digits} |
|||
for I:=1 to Max-Max div 10 do |
|||
begin |
|||
{Store palindrome} |
|||
Pals[Inx]:=GetNKPalindrome(I,D); |
|||
Inc(Inx); |
|||
{Exit when array is full} |
|||
if Inx>=Count then break; |
|||
end; |
|||
end; |
|||
end; |
|||
{------------------------------------------------------------------------------------------------} |
|||
function IsGapful(N: int64): boolean; |
function IsGapful(N: int64): boolean; |
||
Line 1,580: | Line 1,672: | ||
9675005769 9675995769 9676886769 9677777769 9678668769 |
9675005769 9675995769 9676886769 9677777769 9678668769 |
||
9679559769 9680440869 9681331869 9682222869 9683113869 |
9679559769 9680440869 9681331869 9682222869 9683113869 |
||
Elapsed Time: 763.788 ms. |
Elapsed Time: 763.788 ms. |