Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello ....I have problem with calculate complexity for second loop
C++
f=1;
x=3;
for (int i = 1; i <= n; i*=2) 
   for (int j = 1; j <= i * i; j++) 
      if (i % j == 0) 
      for (int k = 1; k <= j; k++) 
         f=f*x;


What I have tried:

I calculate the third loop complexitiy
the result is O(j)
Posted
Updated 6-Jun-18 9:38am
Comments
OriginalGriff 6-Jun-18 10:59am    
And?
What's the problem?
What have you tried?
Where are you stuck?
What help do you need?
Member 13849940 6-Jun-18 11:02am    
I stuck with this condition
for (int j = 1; j <= i * i; j++)
if (i % j == 0)

1 solution

I suggest you an experimental approach, run the folllowing program and have a look at the output. It gives some insight and could make you able to understand the actual algorithm asymptotic behaviour.

C++
using namespace std;

int main()
{
  const int MAXN = 1000; // try different values

  for (int  n=1; n<MAXN; ++n)
  {
    int count = 0;
    cout << "********************************\nn=" << n << "\n";
    for (int i = 1; i <= n; i*=2)
      for (int j = 1; j <= i * i; j++)
        if (i % j == 0)
          for (int k = 1; k <= j; k++)
          {
            cout << i << " " <<  j  << " " << k  << "\n";
            ++count;
          }
    cout << count <<  endl;
  }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900