Super-d numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: changed DO loop from FOR to TO.)
m (→‎{{header|Pascal}}: change to not so antique AnsiString searching runtime bottlenecks -> converting to base 10 string)
Line 180: Line 180:
=={{header|Pascal}}==
=={{header|Pascal}}==
{{works with|Free Pascal}}
{{works with|Free Pascal}}
gmp is fast.Same brute force as [http://rosettacode.org/mw/index.php?title=Super-d_numbers&action=submit#Go Go]
gmp is fast.Same brute force as [http://rosettacode.org/wiki/Super-d_numbers#Go Go]
<lang pascal>program Super_D;
<lang pascal>program Super_D;

uses
uses
sysutils,gmp;
sysutils,gmp;


var
var
s :ansistring;
s :string[127]; //max is 9*182557181^9 } < 100 decimal digits
s_comp : string[9];
s_comp : ansistring;
pS : pChar;
test : mpz_t;
test : mpz_t;
i,j,dgt,cnt : NativeUint;
i,j,dgt,cnt : NativeUint;
Begin
Begin
For i := 1 to 127 do
s[i] := #0;
pS := @s[1];
mpz_init(test);
mpz_init(test);

for dgt := 2 to 9 do
for dgt := 2 to 9 do
Begin
Begin
//create '22' to '999999999'
cnt := 0;
s_comp := '';
i := dgt;
For i := 1 to dgt do
For j := 2 to dgt do
s_comp := s_comp+chr(48+dgt);
i := i*10+dgt;
s_comp := IntToStr(i);
writeln('Finding ',s_comp,' in ',dgt,'*i**',dgt);
writeln('Finding ',s_comp,' in ',dgt,'*i**',dgt);

i := dgt;
cnt := 0;
repeat
repeat
mpz_ui_pow_ui(test,i,dgt);
mpz_ui_pow_ui(test,i,dgt);
mpz_mul_ui(test,test,dgt);
mpz_mul_ui(test,test,dgt);
j := mpz_sizeinbase (test,10);
setlength(s,mpz_sizeinbase(test,10));
mpz_get_str(pChar(s),10,test);
s[0] := char(j);//set length
mpz_get_str(pS,10,test);
IF Pos(s_comp,s) <> 0 then
IF Pos(s_comp,s) <> 0 then
Begin
Begin
Line 220: Line 219:
end;
end;
mpz_clear(test);
mpz_clear(test);
End.
End.</lang>
</lang>
{{out}}
{{out}}
<pre>Finding 22 in 2*i**2
<pre>Finding 22 in 2*i**2
19 31 69 81 105 106 107 119 127 131
19 31 69 81 105 106 107 119 127 131
Finding 333 in 3*i**3
Finding 333 in 3*i**3
261 462 471 481 558 753 1036 1046 1471 1645
261 462 471 481 558 753 1036 1046 1471 1645
Finding 4444 in 4*i**4
Finding 4444 in 4*i**4
1168 4972 7423 7752 8431 10267 11317 11487 11549 11680
1168 4972 7423 7752 8431 10267 11317 11487 11549 11680
Finding 55555 in 5*i**5
Finding 55555 in 5*i**5
4602 5517 7539 12955 14555 20137 20379 26629 32767 35689
4602 5517 7539 12955 14555 20137 20379 26629 32767 35689
Finding 666666 in 6*i**6
Finding 666666 in 6*i**6
27257 272570 302693 323576 364509 502785 513675 537771 676657 678146
27257 272570 302693 323576 364509 502785 513675 537771 676657 678146
Finding 7777777 in 7*i**7
Finding 7777777 in 7*i**7
140997 490996 1184321 1259609 1409970 1783166 1886654 1977538 2457756 2714763
140997 490996 1184321 1259609 1409970 1783166 1886654 1977538 2457756 2714763
Finding 88888888 in 8*i**8
Finding 88888888 in 8*i**8
185423 641519 1551728 1854230 6415190 12043464 12147605 15517280 16561735 18542300
185423 641519 1551728 1854230 6415190 12043464 12147605 15517280 16561735 18542300
Finding 999999999 in 9*i**9
Finding 999999999 in 9*i**9
17546133 32613656 93568867 107225764 109255734 113315082 121251742 175461330 180917907 182557181
17546133 32613656 93568867 107225764 109255734 113315082 121251742 175461330 180917907 182557181
real 1m11,239s


//only calc 9*i**9 [2..182557181]
real 1m7,224s
Finding 999999999 in 9*i**9
real 0m7,577s
//calc 9*i**9 [2..182557181] and convert to String with preset length 100 -> takes longer to find '999999999'
real 0m40,094s
//calc 9*i**9 [2..182557181] and convert to String and setlength
real 0m41,358s
//complete task with finding
Finding 999999999 in 9*i**9
17546133 32613656 93568867 107225764 109255734 113315082 121251742 175461330 180917907 182557181
real 1m5,134s
</pre>
</pre>