Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

A script to determine the next number in a sequence

by Anonymous Monk
on May 10, 2007 at 01:44 UTC ( [id://614520]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I like to read/answer on answers.yahoo.com a lot and today there was this question..
1 5 10 25 50? what is the next number?
I can't figure it out and there's like 10 answers and they are all different. So this lead me to the idea of having a script calculate the next number.

Well I searched google and couldn't find a script or program to do this and now I'm interested to seeing how this can be done in Perl.

Any ideas on how to get started?

Replies are listed 'Best First'.
Re: A script to determine the next number in a sequence
by sulfericacid (Deacon) on May 10, 2007 at 01:57 UTC
    Can't help you with the script suggestions but it is a nice idea.

    I think I have the answer and I don't think this is a REAL numbers problem. 1, 5, 10, 25 and 50 are all U.S. currency coins. The next coin in sequence would be the 1 dollar coin which == 100. So the next number would be 100.

    I could be wrong but I see no relations between the numbers otherwise.



    "Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

    sulfericacid
Re: A script to determine the next number in a sequence
by Limbic~Region (Chancellor) on May 10, 2007 at 12:50 UTC
    Anonymous Monk,
    There is the integer sequence database which may help but you have to understand that this is an impossible problem. For a given number of integers in a sequence, there are an infinite number of "formulas" that could produce that sequence. Each may produce a different "next number". All you could hope to accomplish with a program is to find a formula that fits the data, you have no guarantees it is the right one.

    My guess for the next number is 62.5, can you figure out why?

    Cheers - L~R

      My guess is 76, from polynomial inerpolation -- a lousy method for this but I like it. Sloane says it's either 100 or 65.

      I was unable to get 62.5.

      My only guess is

      1 x(5/1) =5 x2 =10 x(5/2) = 25 x2 =50 x(5/3) =83.3

      In other words, a sequence of two stages:

      • multiply by (5 divided by n++)
      • multiply by 2

        monarch,
        The approach I used is that each term was being multiplied by some number. There were two patterns of multiples woven together. One pattern was constant - x 2. The other depends on the previous value (prev / 2).
        1 x 5 = 5 # pattern 1, no prev value so start with 5 5 x 2 = 10 # pattern 2, constant 10 x 2.5 = 25 # pattern 1, prev value 5 / 2 25 x 2 = 50 # pattern 2, constant 50 x 1.25 = 62.5 # pattern 1, prev value 2.5 / 2

        Cheers - L~R

Re: A script to determine the next number in a sequence
by lin0 (Curate) on May 10, 2007 at 17:09 UTC
      lin0,
      Actually, what I said is that are an infinite number of "formulas" that can produce any given sequence if that sequence is not complete.

      Cheers - L~R

Re: A script to determine the next number in a sequence
by ambrus (Abbot) on May 10, 2007 at 11:04 UTC

    Start with something like this:

    sub nextelt { @s = @_; use LWP::Simple; $q = join("%2c", map int, @s); + $d = get("http://www.research.att.com/~njas/sequences/?q=$q&n=1&fmt= +3"); $s = join(",",@s); for $t ("VWX", "STU") { $l = ",".join(",", $d + =~ /^%[$t]\s+\S+\s+(.*)/mg); $l =~ /$s,(-?\d+)/ and return $1; } } warn nextelt(5, 14, 42, 132, 429, 1430);
Re: A script to determine the next number in a sequence
by Anonymous Monk on May 10, 2007 at 04:14 UTC
    This smells like math homework. See http://www.purplemath.com/modules/nextnumb.htm
Re: A script to determine the next number in a sequence
by halley (Prior) on May 10, 2007 at 17:17 UTC
    My guess is 100. As in qw/penny nickle dime quarter half-dollar silver-dollar/. Sometimes answers aren't purely mathematical, but simply pragmatic.

    --
    [ e d @ h a l l e y . c c ]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://614520]
Approved by sulfericacid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-28 19:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found