Well, this works:
my $n = ...;
my $b = ...;
my @possibles;
my $p = '1';
while ($p <= $n) {
push @possibles, ('1' x $p);
$p *= $b;
}
my ($pat) =
map qr/$_/,
join '|',
@possibles;
print( "$n: ", ('1' x $n) =~ /^$pat\z/ ?1:0, "\n" );
You can do almost all of the above inside the pattern, except for the $n <= $number check:
my $n = ...;
my $b = ...;
my $b_m1 = $b-1;
my $pat = qr/(1|((?-2))\g{-1}{$b_m1})/;
print( "$n: ", ('1' x $n) =~ /^$pat\z/ ?1:0, "\n" );
Without that check, the regex engine preemptively (and sometimes accurately) throws "Infinite recursion in regex".
Back to the drawing board.
Update: As best as I can tell, you'd need to be able to change the topic as follows:
local our $pat;
$pat = qr/
1
|
(1+)
\g{-1}{$b_m1}
(?(?{ $^N =~ $pat })(?=)|(?!))
/x;
But that deosn't even work *with* (?{})!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.