Super-d numbers: Difference between revisions
Content added Content deleted
(added =={{header|Pascal}}==) |
|||
Line 176: | Line 176: | ||
17546133 32613656 93568867 107225764 109255734 113315082 121251742 175461330 180917907 182557181 |
17546133 32613656 93568867 107225764 109255734 113315082 121251742 175461330 180917907 182557181 |
||
found in 135616 ms |
found in 135616 ms |
||
</pre> |
|||
=={{header|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] |
|||
<lang pascal>program Super_D; |
|||
uses |
|||
sysutils,gmp; |
|||
var |
|||
s :string[127]; //max is 9*182557181^9 } < 100 decimal digits |
|||
s_comp : string[9]; |
|||
pS : pChar; |
|||
test : mpz_t; |
|||
i,j,dgt,cnt : NativeUint; |
|||
Begin |
|||
For i := 1 to 127 do |
|||
s[i] := #0; |
|||
pS := @s[1]; |
|||
mpz_init(test); |
|||
for dgt := 2 to 9 do |
|||
Begin |
|||
cnt := 0; |
|||
s_comp := ''; |
|||
For i := 1 to dgt do |
|||
s_comp := s_comp+chr(48+dgt); |
|||
writeln('Finding ',s_comp,' in ',dgt,'*i**',dgt); |
|||
repeat |
|||
mpz_ui_pow_ui(test,i,dgt); |
|||
mpz_mul_ui(test,test,dgt); |
|||
j := mpz_sizeinbase (test,10); |
|||
s[0] := char(j);//set length |
|||
mpz_get_str(pS,10,test); |
|||
IF Pos(s_comp,s) <> 0 then |
|||
Begin |
|||
write(i,' '); |
|||
inc(cnt); |
|||
end; |
|||
inc(i); |
|||
until cnt = 10; |
|||
writeln; |
|||
end; |
|||
mpz_clear(test); |
|||
End. |
|||
</lang> |
|||
{{out}} |
|||
<pre>Finding 22 in 2*i**2 |
|||
19 31 69 81 105 106 107 119 127 131 |
|||
Finding 333 in 3*i**3 |
|||
261 462 471 481 558 753 1036 1046 1471 1645 |
|||
Finding 4444 in 4*i**4 |
|||
1168 4972 7423 7752 8431 10267 11317 11487 11549 11680 |
|||
Finding 55555 in 5*i**5 |
|||
4602 5517 7539 12955 14555 20137 20379 26629 32767 35689 |
|||
Finding 666666 in 6*i**6 |
|||
27257 272570 302693 323576 364509 502785 513675 537771 676657 678146 |
|||
Finding 7777777 in 7*i**7 |
|||
140997 490996 1184321 1259609 1409970 1783166 1886654 1977538 2457756 2714763 |
|||
Finding 88888888 in 8*i**8 |
|||
185423 641519 1551728 1854230 6415190 12043464 12147605 15517280 16561735 18542300 |
|||
Finding 999999999 in 9*i**9 |
|||
17546133 32613656 93568867 107225764 109255734 113315082 121251742 175461330 180917907 182557181 |
|||
real 1m7,224s |
|||
</pre> |
</pre> |
||
Line 285: | Line 221: | ||
{{works with|Rakudo|2019.07.1}} |
{{works with|Rakudo|2019.07.1}} |
||
===Simple=== |
===Simple=== |
||
2 - 6 |
2 - 6 take a few seconds, 7 about 17 seconds, 8 about 90... 9, bleh... around 700 seconds. |
||
<lang perl6>sub super ( |
<lang perl6>sub super (\d) { |
||
my |
my \run = d x d; |
||
^∞ .grep: ( |
^∞ .hyper.grep: -> \n { (d * n ** d).Str.contains: run } |
||
} |
} |
||
(2.. |
(2..9).race(:1batch).map: { |
||
my $now = now; |
|||
put "\nFirst 10 super-$_ numbers:\n{.&super[^10]}\n{(now - $now).round(.1)} sec." |
|||
}</lang> |
|||
<pre>First 10 super-2 numbers: |
<pre>First 10 super-2 numbers: |
||
19 31 69 81 105 106 107 119 127 131 |
19 31 69 81 105 106 107 119 127 131 |
||
0.1 sec. |
|||
First 10 super-3 numbers: |
First 10 super-3 numbers: |
||
261 462 471 481 558 753 1036 1046 1471 1645 |
261 462 471 481 558 753 1036 1046 1471 1645 |
||
0.1 sec. |
|||
First 10 super-4 numbers: |
First 10 super-4 numbers: |
||
1168 4972 7423 7752 8431 10267 11317 11487 11549 11680 |
1168 4972 7423 7752 8431 10267 11317 11487 11549 11680 |
||
0.3 sec. |
|||
First 10 super-5 numbers: |
First 10 super-5 numbers: |
||
4602 5517 7539 12955 14555 20137 20379 26629 32767 35689 |
4602 5517 7539 12955 14555 20137 20379 26629 32767 35689 |
||
0.6 sec. |
|||
First 10 super-6 numbers: |
First 10 super-6 numbers: |
||
27257 272570 302693 323576 364509 502785 513675 537771 676657 678146 |
27257 272570 302693 323576 364509 502785 513675 537771 676657 678146 |
||
5.2 sec. |
|||
First 10 super-7 numbers: |
First 10 super-7 numbers: |
||
140997 490996 1184321 1259609 1409970 1783166 1886654 1977538 2457756 2714763 |
140997 490996 1184321 1259609 1409970 1783166 1886654 1977538 2457756 2714763 |
||
17.1 sec. |
|||
First 10 super-8 numbers: |
First 10 super-8 numbers: |
||
185423 641519 1551728 1854230 6415190 12043464 12147605 15517280 16561735 18542300 |
185423 641519 1551728 1854230 6415190 12043464 12147605 15517280 16561735 18542300 |
||
92.1 sec. |
|||
First 10 super-9 numbers: |
|||
17546133 32613656 93568867 107225764 109255734 113315082 121251742 175461330 180917907 182557181 |
|||
704.7 sec.</pre> |
|||
===Concurrent=== |
===Concurrent=== |
||
Line 404: | Line 354: | ||
17546133 <killed> |
17546133 <killed> |
||
</pre> |
</pre> |
||
=={{header|Python}}== |
|||
<lang python>from itertools import islice, count |
|||
def superg(d): |
|||
if d != int(d) or not 2 <= d <= 9: |
|||
raise ValueError("argument must be integer from 2 to 9 inclusive") |
|||
tofind = str(d) * d |
|||
for n in count(2): |
|||
if tofind in str(d * n ** d): |
|||
yield n |
|||
if __name__ == '__main__': |
|||
for d in range(2, 9): |
|||
print(f"{d}:", ', '.join(str(n) for n in islice(superg(d), 10)))</lang> |
|||
{{out}} |
|||
<pre>2: 19, 31, 69, 81, 105, 106, 107, 119, 127, 131 |
|||
3: 261, 462, 471, 481, 558, 753, 1036, 1046, 1471, 1645 |
|||
4: 1168, 4972, 7423, 7752, 8431, 10267, 11317, 11487, 11549, 11680 |
|||
5: 4602, 5517, 7539, 12955, 14555, 20137, 20379, 26629, 32767, 35689 |
|||
6: 27257, 272570, 302693, 323576, 364509, 502785, 513675, 537771, 676657, 678146 |
|||
7: 140997, 490996, 1184321, 1259609, 1409970, 1783166, 1886654, 1977538, 2457756, 2714763 |
|||
8: 185423, 641519, 1551728, 1854230, 6415190, 12043464, 12147605, 15517280, 16561735, 18542300</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program computes and displays the first N |
<lang rexx>/*REXX program computes and displays the first N super─d numbers for D from LO to HI.*/ |
||
numeric digits 100 /*ensure enough decimal digs for calc. */ |
numeric digits 100 /*ensure enough decimal digs for calc. */ |
||
parse arg n LO HI . /*obtain optional arguments from the CL*/ |
parse arg n LO HI . /*obtain optional arguments from the CL*/ |
||
if n=='' | n=="," then n= 10 /*the number of |
if n=='' | n=="," then n= 10 /*the number of super─d numbers to calc*/ |
||
if LO=='' | LO=="," then LO= 2 /*low end of D for the |
if LO=='' | LO=="," then LO= 2 /*low end of D for the super─d nums.*/ |
||
if HI=='' | HI=="," then HI= 9 /*high " " " " " " " */ |
if HI=='' | HI=="," then HI= 9 /*high " " " " " " " */ |
||
/* [↓] process D from LO ──► HI. */ |
|||
do d=LO for HI-1 |
do d=LO for HI-1; #= 0; $= /*count & list of super─d nums (so far)*/ |
||
#= 0 /*count of the super-d numbers (so far)*/ |
|||
$= /* list " " " " " " */ |
|||
z= copies(d, d) /*the string that is being searched for*/ |
z= copies(d, d) /*the string that is being searched for*/ |
||
do j=2 until #==n |
do j=2 until #==n /*search for super─d numbers 'til found*/ |
||
if pos(z, d * j**d)==0 then iterate |
if pos(z, d * j**d)==0 then iterate /*does product have the required reps? */ |
||
#= # + 1; $= $ j |
#= # + 1; $= $ j /*bump counter; add the number to list*/ |
||
end /*j*/ |
end /*j*/ |
||
say |
say |
||
say center(' the first ' n " super-"d 'numbers ', digits(), "═") |
say center(' the first ' n " super-"d 'numbers ', digits(), "═") |