Question: Problem 2 ( 1 2 pts . ) : Sum of natural numbers Below we give, in Java syntax, the sumn method which computes the
Problem pts: Sum of natural numbers
Below we give, in Java syntax, the sumn method which computes the sum of the n natural numbers
from to n For example, sumn sumn sumn and sumn
P r e c o n d i t i o n : n
int sumn int n
int i t ;
while i n
i i ;
t t i ;
return t ;
P o s t c o n d i t i o n : t n n
a Find a suitable loop invariant. pt
b Show that the invariant holds before the loop base case pt
c Show by induction that if the invariant holds after kth iteration, and execution takes a kst
iteration, the invariant still holds inductive step pts
d Show that the loop exit condition and the loop invariant imply the postcondition t nn
pts
e Find a suitable decrementing function. Show that the function is nonnegative before loop starts,
that it decreases at each iteration and that when it reaches the loop is exited. pts
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
