1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Convert algorithm to a formula

  1. Jan 16, 2013 #1
    Hi
    Please can someone help me convert this algorithm to a mathematical formula ?

    function fun(int x)
    {
    int c = 0 ;
    for(int i=2;i<floor(x/2);i++)
    {
    if(floor(x/i) > i-1)
    for(int j=0;j<floor(x/i)-i+1;j++)
    c++;
    }

    return c;
    }

    thanks
     
  2. jcsd
  3. Jan 16, 2013 #2

    mfb

    User Avatar
    2016 Award

    Staff: Mentor

    Code (Text):
    for(int j=0;j<floor(x/i)-i+1;j++)
      c++;
    This just counts how many times j<floor(x/i)-i+1.
    In other words, you can replace it with
    c+=floor(x/i)-i+1;

    Next:
    if(floor(x/i) > i-1)
    As the left side cannot increase (with increasing i) and the right side always increases, this is true up to some specific i. Both sides are integers, so the statement is equivalent to
    if(floor(x/i) >= i)
    Written in that way, it is possible to drop floor() completely (check this!) as i is an integer:
    if(x/i >= i)
    which is just
    if(i^2 <= x)

    Simplified code:
    Code (Text):

    function fun(int x)
    {
      int c = 0 ;
      for(int i=2;i<floor(x/2);i++)
      {
        if(i^2 <= x)
          c+=floor(x/i)-i+1;
      }
      return c;
    }
    Now you have two upper limits for i, so it is reasonable to split that in two cases. Can you find them? The border is a specific integer.
    It could be tricky (or even impossible) to find an explicit way to sum those floor(x/i), but it is certainly possible to write it as a sum in a formula.
     
  4. Jan 16, 2013 #3
    Thanks mfb , that was great
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Convert algorithm to a formula
  1. Convert to formula (Replies: 6)

  2. Algorithm Time (Replies: 4)

  3. Division Algorithm (Replies: 3)

  4. Is this a formula? (Replies: 8)

Loading...