Palindromic gapful numbers: Difference between revisions
Content deleted Content added
No edit summary Tag: Manual revert |
No edit summary |
||
Line 1,304: | Line 1,304: | ||
8 : [808696968869696808] |
8 : [808696968869696808] |
||
9 : [968787783387787869]</pre> |
9 : [968787783387787869]</pre> |
||
=={{header|Delphi}}== |
|||
{{works with|Delphi|6.0}} |
|||
{{libheader|SysUtils,StdCtrls}} |
|||
This is among the faster versions of the problem. It solves the standard and optional tasks in 0.7 seconds. |
|||
<syntaxhighlight lang="Delphi"> |
|||
function IsGapful(N: int64): boolean; |
|||
{Return true if number is "GapFul"} |
|||
{GapFul = combined first/last} |
|||
{ digits divide evenly into N} |
|||
var Digits: TIntegerDynArray; |
|||
var I: int64; |
|||
begin |
|||
Result:=False; |
|||
{Must be 3 digit number} |
|||
if N<100 then exit; |
|||
{Put digits in array} |
|||
GetDigits(N,Digits); |
|||
{Form number from first and last digit} |
|||
I:=Digits[0] + 10 * Digits[High(Digits)]; |
|||
{Does it divide evenly into N} |
|||
Result:=(N mod I)=0; |
|||
end; |
|||
function HasEndDigit(N: int64; Digit: integer): boolean; |
|||
{Return true if last digit match specified "Digit"} |
|||
var LD: integer; |
|||
begin |
|||
LD:=N mod 10; |
|||
Result:=LD=Digit; |
|||
end; |
|||
function GetGapfulPalinEndSet(Max, EndDigit: integer): string; |
|||
{Get first Max number of Gapful Palindromes with specified EndDigit} |
|||
var I,Cnt,P: integer; |
|||
begin |
|||
Result:='Ending in: '+IntToStr(EndDigit)+CRLF; |
|||
Cnt:=0; |
|||
{Get palindromes and test them} |
|||
for I:=0 to high(Integer) do |
|||
begin |
|||
{Get next palinedrome} |
|||
P:=GetNthPalindrome(I); |
|||
{Is Gapful and has specified EndDigit} |
|||
if IsGapFul(P) and HasEndDigit(P,EndDigit) then |
|||
begin |
|||
Inc(Cnt); |
|||
{Display it} |
|||
Result:=Result+Format('%8d',[P]); |
|||
if (Cnt mod 5)=0 then Result:=Result+CRLF; |
|||
{Break when finished} |
|||
if Cnt>=Max then break; |
|||
end; |
|||
end; |
|||
end; |
|||
function LastGapfulPalinEndSet(Count,Last,EndDigit: integer): string; |
|||
{Get Gapful Palindromes with specified EndDigit} |
|||
{Get "Last" number of items out of a total "Count" } |
|||
var I,Inx: integer; |
|||
var P: int64; |
|||
var IA: TInt64DynArray; |
|||
begin |
|||
SetLength(IA,Count); |
|||
Result:='Ending in: '+IntToStr(EndDigit)+CRLF; |
|||
{Get count number of items} |
|||
Inx:=0; |
|||
for I:=0 to Count-1 do |
|||
begin |
|||
{Keep getting palindromes until} |
|||
{they Gapful and have specified last digit} |
|||
repeat |
|||
begin |
|||
P:=GetNthPalindrome(Inx); |
|||
Inc(Inx); |
|||
end |
|||
until IsGapFul(P) and HasEndDigit(P,EndDigit); |
|||
{Save item} |
|||
IA[I]:=P; |
|||
end; |
|||
{Get last items} |
|||
for I:=Count-Last to Count-1 do |
|||
begin |
|||
Result:=Result+Format('%12d',[IA[I]]); |
|||
if (I mod 5)=4 then Result:=Result+CRLF; |
|||
end; |
|||
end; |
|||
procedure ShowPalindromicGapfuls(Memo: TMemo); |
|||
var S: string; |
|||
begin |
|||
Memo.Lines.Add('First 20 palindromic gapful numbers'); |
|||
Memo.Lines.Add(GetGapFulPalinEndSet(20,1)); |
|||
Memo.Lines.Add(GetGapFulPalinEndSet(20,2)); |
|||
Memo.Lines.Add(GetGapFulPalinEndSet(20,3)); |
|||
Memo.Lines.Add(GetGapFulPalinEndSet(20,4)); |
|||
Memo.Lines.Add(GetGapFulPalinEndSet(20,5)); |
|||
Memo.Lines.Add(GetGapFulPalinEndSet(20,6)); |
|||
Memo.Lines.Add(GetGapFulPalinEndSet(20,7)); |
|||
Memo.Lines.Add(GetGapFulPalinEndSet(20,8)); |
|||
Memo.Lines.Add(GetGapFulPalinEndSet(20,9)); |
|||
Memo.Lines.Add('86th to 100th'); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,1)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,2)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,3)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,4)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,5)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,6)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,7)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,8)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(100,15,9)); |
|||
Memo.Lines.Add('991st to 1000th:'); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,1)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,2)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,3)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,4)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,5)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,6)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,7)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,8)); |
|||
Memo.Lines.Add(LastGapFulPalinEndSet(1000,10,9)); |
|||
end; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
First 20 palindromic gapful numbers |
|||
Ending in: 1 |
|||
121 1001 1111 1221 1331 |
|||
1441 1551 1661 1771 1881 |
|||
1991 10901 11011 12221 13431 |
|||
14641 15851 17171 18381 19591 |
|||
Ending in: 2 |
|||
242 2002 2112 2222 2332 |
|||
2442 2552 2662 2772 2882 |
|||
2992 20702 21912 22022 23232 |
|||
24442 25652 26862 28182 29392 |
|||
Ending in: 3 |
|||
363 3003 3333 3663 3993 |
|||
31713 33033 36663 300003 303303 |
|||
306603 309903 312213 315513 318813 |
|||
321123 324423 327723 330033 333333 |
|||
Ending in: 4 |
|||
484 4004 4224 4444 4664 |
|||
4884 40304 42724 44044 46464 |
|||
48884 400004 401104 402204 403304 |
|||
404404 405504 406604 407704 408804 |
|||
Ending in: 5 |
|||
5005 5115 5225 5335 5445 |
|||
5555 5665 5775 5885 5995 |
|||
50105 51315 52525 53735 54945 |
|||
55055 56265 57475 58685 59895 |
|||
Ending in: 6 |
|||
6006 6336 6666 6996 61116 |
|||
64746 66066 69696 600006 603306 |
|||
606606 609906 612216 615516 618816 |
|||
621126 624426 627726 630036 633336 |
|||
Ending in: 7 |
|||
7007 7777 77077 700007 707707 |
|||
710017 717717 720027 727727 730037 |
|||
737737 740047 747747 750057 757757 |
|||
760067 767767 770077 777777 780087 |
|||
Ending in: 8 |
|||
8008 8448 8888 80608 86768 |
|||
88088 800008 802208 804408 806608 |
|||
808808 821128 823328 825528 827728 |
|||
829928 840048 842248 844448 846648 |
|||
Ending in: 9 |
|||
9009 9999 94149 99099 900009 |
|||
909909 918819 927729 936639 945549 |
|||
954459 963369 972279 981189 990099 |
|||
999999 9459549 9508059 9557559 9606069 |
|||
86th to 100th |
|||
Ending in: 1 |
|||
165561 166661 167761 168861 169961 |
|||
170071 171171 172271 173371 174471 |
|||
175571 176671 177771 178871 179971 |
|||
Ending in: 2 |
|||
265562 266662 267762 268862 269962 |
|||
270072 271172 272272 273372 274472 |
|||
275572 276672 277772 278872 279972 |
|||
Ending in: 3 |
|||
30366303 30399303 30422403 30455403 30488403 |
|||
30511503 30544503 30577503 30600603 30633603 |
|||
30666603 30699603 30722703 30755703 30788703 |
|||
Ending in: 4 |
|||
4473744 4485844 4497944 4607064 4619164 |
|||
4620264 4632364 4644464 4656564 4668664 |
|||
4681864 4693964 4803084 4815184 4827284 |
|||
Ending in: 5 |
|||
565565 566665 567765 568865 569965 |
|||
570075 571175 572275 573375 574475 |
|||
575575 576675 577775 578875 579975 |
|||
Ending in: 6 |
|||
60399306 60422406 60455406 60488406 60511506 |
|||
60544506 60577506 60600606 60633606 60666606 |
|||
60699606 60722706 60755706 60788706 60811806 |
|||
Ending in: 7 |
|||
72299227 72322327 72399327 72422427 72499427 |
|||
72522527 72599527 72622627 72699627 72722727 |
|||
72799727 72822827 72899827 72922927 72999927 |
|||
Ending in: 8 |
|||
80611608 80622608 80633608 80644608 80655608 |
|||
80666608 80677608 80688608 80699608 80800808 |
|||
80811808 80822808 80833808 80844808 80855808 |
|||
Ending in: 9 |
|||
95311359 95400459 95499459 95588559 95677659 |
|||
95766759 95855859 95944959 96033069 96122169 |
|||
96211269 96300369 96399369 96488469 96577569 |
|||
991st to 1000th: |
|||
Ending in: 1 |
|||
17799771 17800871 17811871 17822871 17833871 |
|||
17844871 17855871 17866871 17877871 17888871 |
|||
Ending in: 2 |
|||
27799772 27800872 27811872 27822872 27833872 |
|||
27844872 27855872 27866872 27877872 27888872 |
|||
Ending in: 3 |
|||
3084004803 3084334803 3084664803 3084994803 3085225803 |
|||
3085555803 3085885803 3086116803 3086446803 3086776803 |
|||
Ending in: 4 |
|||
482282284 482414284 482535284 482656284 482777284 |
|||
482898284 482909284 483020384 483141384 483262384 |
|||
Ending in: 5 |
|||
57800875 57811875 57822875 57833875 57844875 |
|||
57855875 57866875 57877875 57888875 57899875 |
|||
Ending in: 6 |
|||
6084004806 6084334806 6084664806 6084994806 6085225806 |
|||
6085555806 6085885806 6086116806 6086446806 6086776806 |
|||
Ending in: 7 |
|||
7452992547 7453223547 7453993547 7454224547 7454994547 |
|||
7455225547 7455995547 7456226547 7456996547 7457227547 |
|||
Ending in: 8 |
|||
8085995808 8086006808 8086116808 8086226808 8086336808 |
|||
8086446808 8086556808 8086666808 8086776808 8086886808 |
|||
Ending in: 9 |
|||
9675005769 9675995769 9676886769 9677777769 9678668769 |
|||
9679559769 9680440869 9681331869 9682222869 9683113869 |
|||
Elapsed Time: 763.788 ms. |
|||
</pre> |
|||
===Translation of F#=== |
===Translation of F#=== |