Convert algorithm to a formula

In summary, the conversation discusses converting an algorithm to a mathematical formula. The algorithm involves counting the number of times a condition is met and simplifying it to a more efficient form. The conversation also suggests splitting the problem into two cases to find an explicit formula for the sum. The individual providing help is thanked for their contribution.
  • #1
mryoussef2012
6
0
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
 
Physics news on Phys.org
  • #2
Code:
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:
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.
 
  • #3
Thanks mfb , that was great
 

FAQ: Convert algorithm to a formula

How do I convert an algorithm to a formula?

To convert an algorithm to a formula, you need to first understand the algorithm and its steps. Then, you can identify the variables and their relationships in the algorithm. Finally, you can use mathematical notation to express the algorithm as a formula.

What is the purpose of converting an algorithm to a formula?

Converting an algorithm to a formula can make it easier to understand and analyze. It allows for a more concise and mathematical representation of the algorithm, which can be useful in various fields such as computer science, engineering, and mathematics.

Is it possible to convert any algorithm to a formula?

In theory, any algorithm can be converted to a formula. However, the complexity and nature of the algorithm may make it more difficult to do so. In some cases, the algorithm may be better represented in a different form such as a flowchart or pseudocode.

Are there specific steps or guidelines for converting an algorithm to a formula?

There is no one definitive method for converting an algorithm to a formula. It involves understanding the algorithm, identifying the variables and their relationships, and using mathematical notation to express the algorithm. However, there may be specific guidelines or conventions within certain fields or for specific types of algorithms.

Can I use any mathematical notation to express an algorithm as a formula?

Generally, it is best to use standard mathematical notation when converting an algorithm to a formula. This ensures clarity and understanding for others who may be reading or using the formula. However, depending on the context and purpose of the formula, some variations or specialized notations may be appropriate.

Back
Top