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* (?{})!


In reply to Re: check for power of a number with regex by ikegami
in thread check for power of a number with regex by spx2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.