# Matlab - floating point operation counts (flop count)

• MATLAB
playboy

## Main Question or Discussion Point

1. Homework Statement

So I was giving a problem to "calculate" the number of flops in class today..
Now, I read the examples in the text book, and they are hard enough to understand...I am hoping that some one here can push me onto the right track

2. Homework Equations

find the flop count for the following sequence of commands:
a=0;
..for p=1:n
....for q=p:n
......for r=q:n
........a=a+1;
......end
....end
..end
What will be the value of a after the commands are executed ?

3. The Attempt at a Solution

I am not looking for a number, but a summation formula interms of n.

I am given that it has to be in the form
sum(i,n) i = n(1-n)/2
and
sum(i,n) i^2 = n(1-n)(2n-a)/6

So far, all I can see is that their must be n + n + n = 3n flops...

I have no idea how to calculate a.

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
AlephZero
Homework Helper
So far, all I can see is that their must be n + n + n = 3n flops...
That's not correct for two reasons:
1 The loops are nested inside each other
2 The loops do not all go from 1 to n.

It might help to change this into a real computer language so you can run the program, set n equal to a small number like 4 or 5, and print out p, q, r, and a in the inside loop (just after the a = a + 1) . Then you will see what's going on.

playboy
That's not correct for two reasons:
1 The loops are nested inside each other
2 The loops do not all go from 1 to n.

It might help to change this into a real computer language so you can run the program, set n equal to a small number like 4 or 5, and print out p, q, r, and a in the inside loop (just after the a = a + 1) . Then you will see what's going on.
Perhaps that is my problem..."the loops are nested inside each other"

I tried getting this programme to run so that I can "visualize" how it works, but I can't get matlab running it.

I wrote:

function [a]=bs(n)

a=0;
for p=1:n;
for q=1:n;
for r=1:n;
a=a+1;
end
end
end

and tried running [a]=bs(n) for n=3 in the commad window. I got an error.

Let's consider n = 5

p=1:5 --> p = 1 2 3 4 5
q=1:5 --> q = 1 2 3 4 5
r=1:5 --> r = 1 2 3 4 5

So i have NO idea how this loop works

AlephZero
Homework Helper
You need to find out what a "for" loop means, in the language that is being used in these questions.

The code

for p = 1:5
(some statements)
end

doesn't mean the same as the Matlab statement

p = 1:5

playboy
I think my problem is understanding the entire code.

I've been pouring my thoughts out for the longest time on this question.

Could you please expalin to me how the function would compute a for n=2?

(I tried writing this function in matlab but failed)

AlephZero
Homework Helper
I'll repeat what I said before: if you don't understand the concept of loops in procedural programming languages, then you need to learn about that, before you can do this type of question.

If you knew a language like C, C++, Java, Pascal, Basic, Fortran, etc, then it should be obvious what the code means, even though the notation is not exactly the same as any of those languages.

If you haven't done any courses on programming, there is a tutorial on C++ here: https://www.physicsforums.com/showthread.php?t=32703. Your "for p=1:n" means the same "for (p = 1; p <= n; p++)" in C or C++.

HallsofIvy
Homework Helper
a=0;
..for p=1:n
....for q=p:n
......for r=q:n
........a=a+1;
......end
....end
..end
For n= 2.
First a is set equal to 0.
Now, p is set to 1, q is set to p= 1, r is set to q= 1
a is increased to 1

We are still in the "r" loop- now r is set to 2
a is increased to 2

Since r has reached its maximum we drop back to the q loop
q is set to 2
r is set to q= 2
a is increased to 3

Since r has reached its maximum we drop back to the q loop
Since q has reached its maximum we drop back to the p koop
p is set to 2, q is set to p= 2, r is set to q= 2
a is increased to 4

Since r has reached its maximum we drop back to the q loop
Since q has reached its maximum we drop back to the p loop
Since p has reached its maximum the program terminates.

a is equal to 4= 22 but be careful. Try n= 3 before making any hasty generaliztions!
Once you find a relation that you feel sure of, perhaps you can prove by induction on n.

Last edited by a moderator:
playboy
Thank you SOO much Hallsofivy! I really appreciate how you took the time to explain that to me..it reall helped me.

So I did this up until n=7 :zzz:

n=1 a=1 ----> n^2 + 0 = a
n=2 a=4 ----> n^2 + 0 = a
n=3 a=10 ---> n^2 + 1 = a
n=4 a=20 ---> n^2 + 4 = a
n=5 a=35 ---> n^2 + 10 = a
n=6 a=56 ---> n^2 + 20= a
n=7 a=84 ---> n^2 + 35 = a

I notice the pattern and I can see what has to be done.

I really appreciate your help. I know sincerly understand what is going on.
Thank you HallsofIvy!