SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Geometric progression in PHP?

    Hi,

    I'm working on a referral system - the formula is exactly like the pyramid/ponzi scheme.

    The system works like this:

    The initial user signs up (tier 1)
    The initial user refers 3 friends (tier 2)
    Each of those 3 friends refer another 3, (tier3)
    etc.

    What would be the mathematical formula for that? (I think it's called geometric progression)

    How could I code up something in PHP where I could enter a number and it will then give me the number of tiers it has gone down and a semi-visual.

    ie: I enter 13 - it displays the text "3 tiers" and then displays

    Code:
         o
         |
        ooo
       / | \
     ooooooooo

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Mathematically, each level goes in powers of 3. The first line is 3^0, second = 3^1, third = 3^2 etc.

    The sum of these is 3^0 + 3^1 + 3^2 + ... 3^n. The general formula for the sum of n tiers is given as something like:
    Code:
    s        = (1 - 3^n)(1 - 3)
             = (3^n - 1)/2
    => 2s = 3^n - 1
    => 2s + 1 = 3^n
    => n = log(2s + 1) / log(3)
           = log(2s + 1, 3) //php notation
    If the number isn't the exact total, it will return a number lower than the row they're on but greater than the row above, so ceil() it:
    PHP Code:
    function getLevel($Number){
        return 
    ceil(log(2s 13));

    As for the diagram bit, I'll leave that to you. It depends on how you want it displayed; with ascii or with graphics and the diagram itself would be a fair amount of code to be giving away for free.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect! Thanks Jake Arkinstall
    Did the visual with ease after that..

    Thanks again!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •