Predicate transformer semantics and the Loop invariant

what is the right "Loop invariant" for this prgramm ?

Page 1 of 1

1 Replies - 1429 Views - Last Post: 28 June 2010 - 10:00 AM

#1 mind_21   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-June 10

Predicate transformer semantics and the Loop invariant

Posted 27 June 2010 - 03:33 AM

hat will be the correct Loop invariant "I" for this Program
P =n >= 1
private static final long f i b ( int n ) {
long fib1 , fib 2 ;
int fib 3 = n − 1 ;
fib 1 = fib2 = 1 ;
while ( fib3 > 0 && fib3−− > 0) / / fib3 goes t o 0
fib2 = fib1 + ( fib1 = fib2 ) ;
System .out.println ( fib3 ) ;
return fib1 ;

Q = fib1 = F(n)

i have 3 possibility
I=fib3 = fib1 + fib2
I=fib1 + fib2 = F(n) ^ n − 1 >=fib3 >=−1
or
I=fib2 = fib1 + fib2 ^ 0 <= fib3 <= n




please i really need help on this subject , please :(

Is This A Good Question/Topic? 0
  • +

Replies To: Predicate transformer semantics and the Loop invariant

#2 mojo666   User is offline

  • D.I.C Addict
  • member icon

Reputation: 409
  • View blog
  • Posts: 885
  • Joined: 27-June 09

Re: Predicate transformer semantics and the Loop invariant

Posted 28 June 2010 - 10:00 AM

I'm not sure what the loop invariant is in this case, however I do know that all of your statements are incorrect. An invariant can never be false.

1) I = fib3 = fib1 + fib2

fib3 is rarely if ever equal to fib1 + fib2. This statement is usually false.

2)I=fib1 + fib2 = F(n) ^ n − 1 >=fib3 >=−1

Where did the "F(n) ^ n − 1" come from? Anyways, fib1+fib2 is usually smaller than fib3 in the first few iterations, thus this statement can be false.

3) I=fib2 = fib1 + fib2 ^ 0 <= fib3 <= n

Again, not sure where the "fib1 + fib2 ^ 0" came from. fib3 is going to be 0 at the end of the loop. fib2 will never be smaller than 1. thus at the end of the loop "fib2<=fib3" is false.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1