Closed Thread
Results 1 to 4 of 4

Thread: Counting Operations

  1. #1
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Counting Operations

    Can anyone explain in "easy wordings" on how to count operations taken by an algorithm ?
    for example if i have an algo
    Code:
    sum = 0
     for i=1
     to N do
         for j=1 to i do
            sum = sum + i
         end
    end
    line 1 , performs just a single constant operation(1)
    line 2 , the loop it will run (n+1) (not sure ) but i am confused , 5th line will also run (n times)
    but i don't get how to actually calculate and make a one formula for all that?I have seen some material but i failed to understand how it's done and it's not clearly shown

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Counting Operations

    I'm reformatting your code so it's easier to read/reference
    Code:
    sum = 0
     for i=1 to N do
      for j=1 to i do
               sum = sum + i
          end
    end
    Line 1: executes 1 time
    Line 2: executes the following BLOCK OF CODE N times
    Line 3: (which is part of the block being executed N times) executes it's block of code i times each time it is called.

    Something important to realize is that i changes each time you hit Line 3. The first time, i = 1. The second, i = 2, etc until i=N. So 2 and 3 combined execute line 4: 1+2+3+4+...+N times.
    1+2+3+...+N = N(N+1)/2, and then add the original count on line 1 to finish.

    Note: the reason this stuff isn't shown clearly is that it varies heavily with the details of the code. With very minor changes, the above code performance can be changed radically. You have to understand what the code does. There are no simple formulas for it.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Re: Counting Operations

    I do understand the code , but the problem is how to write it as a sum up equation

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Counting Operations

    I just showed you.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Beginner C, file operations, simple database
    By irfan_s in forum C Tutorials
    Replies: 2
    Last Post: 10-04-2011, 07:59 AM
  2. IO operations
    By Apprentice123 in forum General Programming
    Replies: 0
    Last Post: 01-01-2010, 05:01 PM
  3. operations like m<<=n and m^=n ?
    By zhok in forum C and C++
    Replies: 1
    Last Post: 04-27-2009, 11:47 AM
  4. Language for File Operations
    By nrodes in forum General Programming
    Replies: 4
    Last Post: 02-08-2009, 06:54 AM
  5. An operations error occurred!!
    By reachpradeep in forum ASP, ASP.NET and Coldfusion
    Replies: 0
    Last Post: 03-04-2007, 05:35 AM

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