Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started