|
|
Line 625: |
Line 625: |
|
printf "%-15s %9s power of two (2^n) that starts with %5s is at n = %s\n", "p($prefix, $nth):", |
|
printf "%-15s %9s power of two (2^n) that starts with %5s is at n = %s\n", "p($prefix, $nth):", |
|
comma($nth) . ordinal_digit($nth), "'$prefix'", comma p($prefix, $nth); |
|
comma($nth) . ordinal_digit($nth), "'$prefix'", comma p($prefix, $nth); |
⚫ |
|
|
⚫ |
|
|
⚫ |
<pre>p(12, 1): 1st power of two (2^n) that starts with '12' is at n = 7 |
|
⚫ |
p(12, 2): 2nd power of two (2^n) that starts with '12' is at n = 80 |
|
⚫ |
p(123, 45): 45th power of two (2^n) that starts with '123' is at n = 12,710 |
|
⚫ |
p(123, 12345): 12,345th power of two (2^n) that starts with '123' is at n = 3,510,491 |
|
⚫ |
p(123, 678910): 678,910th power of two (2^n) that starts with '123' is at n = 193,060,223</pre> |
|
|
|
|
⚫ |
|
|
⚫ |
{{works with|Rakudo|2019.11}} |
|
⚫ |
Uses logs similar to Go and Pascal entries. Takes advantage of patterns in the powers to cut out a bunch of calculations. |
|
|
|
|
⚫ |
<lang perl6>use Lingua::EN::Numbers; |
|
|
|
|
⚫ |
constant $ln2ln10 = log(2) / log(10); |
|
|
|
|
⚫ |
my @startswith12 = ^∞ .grep: { ( 10 ** ($_ * $ln2ln10 % 1) ).substr(0,3) eq '1.2' }; |
|
|
|
|
⚫ |
my @startswith123 = lazy gather loop { |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
$this = 485 unless ( 10 ** (n * $ln2ln10 % 1) ).substr(0,4) eq $pre; |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
$this = 485 unless ( 10 ** (n * $ln2ln10 % 1) ).substr(0,4) eq $pre; |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
|
|
|
⚫ |
multi p ($prefix where *.chars == 2, $nth) { @startswith12[$nth-1] } |
|
⚫ |
multi p ($prefix where *.chars == 3, $nth) { @startswith123[$nth-1] } |
|
|
|
|
⚫ |
|
|
⚫ |
for < 12 1 12 2 123 45 123 12345 123 678910 > -> $prefix, $nth { |
|
⚫ |
printf "%-15s %9s power of two (2^n) that starts with %5s is at n = %s\n", "p($prefix, $nth):", |
|
⚫ |
comma($nth) ~ ordinal-digit($nth).substr(*-2), "'$prefix'", comma p($prefix, $nth); |
|
|
}</lang> |
|
}</lang> |
|
{{out}} |
|
{{out}} |
Line 746: |
Line 699: |
|
p(123, 12345) = 3510491 |
|
p(123, 12345) = 3510491 |
|
p(123, 678910) = 193060223</pre> |
|
p(123, 678910) = 193060223</pre> |
|
|
|
|
⚫ |
|
|
|
(formerly Perl 6) |
|
⚫ |
{{works with|Rakudo|2019.11}} |
|
⚫ |
Uses logs similar to Go and Pascal entries. Takes advantage of patterns in the powers to cut out a bunch of calculations. |
|
|
|
|
⚫ |
<lang perl6>use Lingua::EN::Numbers; |
|
|
|
|
⚫ |
constant $ln2ln10 = log(2) / log(10); |
|
|
|
|
⚫ |
my @startswith12 = ^∞ .grep: { ( 10 ** ($_ * $ln2ln10 % 1) ).substr(0,3) eq '1.2' }; |
|
|
|
|
⚫ |
my @startswith123 = lazy gather loop { |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
$this = 485 unless ( 10 ** (n * $ln2ln10 % 1) ).substr(0,4) eq $pre; |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
$this = 485 unless ( 10 ** (n * $ln2ln10 % 1) ).substr(0,4) eq $pre; |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
⚫ |
|
|
|
|
|
⚫ |
multi p ($prefix where *.chars == 2, $nth) { @startswith12[$nth-1] } |
|
⚫ |
multi p ($prefix where *.chars == 3, $nth) { @startswith123[$nth-1] } |
|
|
|
|
⚫ |
|
|
⚫ |
for < 12 1 12 2 123 45 123 12345 123 678910 > -> $prefix, $nth { |
|
⚫ |
printf "%-15s %9s power of two (2^n) that starts with %5s is at n = %s\n", "p($prefix, $nth):", |
|
⚫ |
comma($nth) ~ ordinal-digit($nth).substr(*-2), "'$prefix'", comma p($prefix, $nth); |
|
⚫ |
|
|
⚫ |
|
|
⚫ |
<pre>p(12, 1): 1st power of two (2^n) that starts with '12' is at n = 7 |
|
⚫ |
p(12, 2): 2nd power of two (2^n) that starts with '12' is at n = 80 |
|
⚫ |
p(123, 45): 45th power of two (2^n) that starts with '123' is at n = 12,710 |
|
⚫ |
p(123, 12345): 12,345th power of two (2^n) that starts with '123' is at n = 3,510,491 |
|
⚫ |
p(123, 678910): 678,910th power of two (2^n) that starts with '123' is at n = 193,060,223</pre> |
|
|
|
|
|
=={{header|REXX}}== |
|
=={{header|REXX}}== |