Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Numerically generate the perl sequence 1, 11, 111, ....

by Narveson (Chaplain)
on Oct 12, 2008 at 09:22 UTC ( #716664=note: print w/ replies, xml ) Need Help??


in reply to Generate the perl sequence 1, 11, 111, ....

What if

1, 11, 111, 1111, 11111, ....

is meant to be a sequence of numbers?

use POSIX; my $number = 1; while ($number <= POSIX::ULONG_MAX) { print "$number, "; $number *= 10; $number++; } print '...';


Comment on Numerically generate the perl sequence 1, 11, 111, ....
Download Code
Re: Numerically generate the perl sequence 1, 11, 111, ....
by moritz (Cardinal) on Oct 12, 2008 at 09:44 UTC
    What if 1, 11, 111, 1111, 11111, .... is meant to be a sequence of numbers?

    Then there's nothing wrong with generating them as strings, because perl will automatically convert between these two as needed.

      Well, but the conversion costs something. So in Perl 6, if you're worried about efficiency, you'd probably write it like this:
      1,11,111,1111 ... { $^prev * 10 + 1 }
      Unless we can make the "whatever" version intuit such an algebraic relationship, in which case it would just be:
      1,11,111,111 ... *
      Just how far we could/should drive the intuition of the series operator is an interesting question.
        It shouldn't be too hard to make this work.

        By simply looking at the sequence of the pair-wise difference and ratio and correlating them to the original sequence or a constant, you can get good guesses for most commonly used sequences, if you just allow a few levels of recursion:

        1, 2, 3, 4, 5 differences 1, 1, 1, 1 # constant 1, 2, 4, 8, 16 ratios: 2, 2, 2, 2 # constant 1, 2, 3, 5, 8 differences: 1, 2, 3, 5 # sames as original shifted by one 1, 11, 111, 1111, 11111 differences: 10, 100, 1000, 10000 ratios: 10, 10, 10 # constant

        The fibonacci sequence is the only one example that needs autocorrelating. Other ones that could use the autocorrelation are -1, 1, -1, 1, ... and 0, 1, 0, 1, ....

        In case of ambiguousness the solution with the shallowest recursion would win.

        I'll try to come up with a prototype implementation that can be used as basis for a specification. But it won't be this or the next week, so have a little patience ;)

        Ideally there would be some sub or method that can be overridden to detect sequences if the built-in mechanism fails..

        Just how far we could/should drive the intuition of the series operator is an interesting question.
        It would be nice if it knew as much as the On-Line Encyclopedia of Integer Sequences in some way or another. The perl5 sources carry the Unicode database around - perhaps the perl6 source could carry the OEIS database. ;-)
        Unless we can make the "whatever" version intuit such an algebraic relationship, in which case it would just be:
        1,11,111,111 ... *
        Just how far we could/should drive the intuition of the series operator is an interesting question.

        I personally believe that however appealing at first sight, it would be kinda too much! When the layman, or even the Physics doctor asks: "which is the next term after 1,1,2,3,5,8,13,21?" expecting his audience either to recocgnize the Fibonacci numbers or not to know about them, and then to blow their minds with explanations about them... the mathematician would answer: "42" In fact, given any number one can devise a rule by which the latter can be obtained from the former ones. Of course, one would want an "easy" rule. But the problem is that that "easy" is just as much as psychologically appealing as difficult to be mathematically defined.

        Thus, the question is either devoid of any sensible answer, or one should restrict himself a priori to some predefined class of rules, e.g. to linear recurrencies, and to establish an unambiguous relationship of simplicity within that class, to allow one to be picked up automatically.

        Alternatively, or better, in addition, one may allow other classes to be specified by means of a suitable adverb, e.g.

        1,11,111,1111, ... * :polyinomially

        But do we really want to make such a big subset of Perl 6 into what that seems a generic Mathematics tool?

        --
        If you can't understand the incipit, then please check the IPB Campaign.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://716664]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (16)
As of 2014-10-21 13:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (103 votes), past polls