sum of the first n numbers
DESCRIPTION
sum of the first n numbers. sum(n) = 1 + 2+ 3+ … +n-1 +n If n = 5 sum(n) = 1+2 +3 +4 +5 =15 If n=6 sum(n) = 1 +2 +3 +4 +5 +6= 21 sum(6)=sum(5)+6 And in general sum(n) = sum(n-1)+n. sum – non recursive function. double sumiteration( int n ) { double sum1; sum1 = 0 ; - PowerPoint PPT PresentationTRANSCRIPT
sum of the first n numbers
sum(n) = 1 + 2+ 3+ … +n-1 +n
If n = 5 sum(n) = 1+2 +3 +4 +5 =15
If n=6 sum(n) = 1 +2 +3 +4 +5 +6= 21
sum(6)=sum(5)+6
And in general
sum(n) = sum(n-1)+n
sum – non recursive function
double sumiteration( int n )
{
double sum1;
sum1 = 0 ;
for ( int i =1; i <= n ; i ++)
sum1 = sum1 +i;
return ( sum1);
}
sum-recursive function
double sum( int n )
{
if ( n ==0 ) // base case
return 0;
// recursive case
return n + sum( n - 1 );
}
Product of the first n numbers
factorial(n) = 1 x 2 x 3x … x n-1 x n
If n = 5 factorial(n) = 1x2 +3 x 4 x 5 =15
If n=6 sum(n) = 1 x2 x3 x4 x 5 x 6= 21
factorial(6)=factorial(5) x 6
And in general
factorial(n) = factorial(n-1) x n
Factorial-nonrecursive
double factorial( int n )
{
double fac1;
fac1 = 1 ;
for ( int i =1; i <= n ; i ++)
fac1 =fac1 *i;
return ( fac1);
}
Factorial recursive
double factorial( int n )
{
if ( n ==1 ) // base case
return 1;
// recursive case
return n * factorial( n - 1 );
}
Sum of the first n elements of an array
Given an array A of length n,
suma(n) =A[0]+A[1]+ …A[n-1]+A[n]
suma(n-1)= A[0]+A[1]+ …A[n-1]
suma(n)=suma(n-1)+A[n]
Sum first elements of an array -nonrecursive
double suma( double A[],int n )
{
double sum1;
sum1 = A[0] ;
for ( int i =1; i <= n ; i ++)
sum1 = sum1 +A[I];
return ( sum1);
}
Sum first n elements of an array-recursive
double suma( double A[], int n )
{
if ( n ==0 ) // base case
return A[0];
// recursive case
return (A[n] + sum(A, n - 1 ));
}
Dot product of 2 arrays A and B
Dot(n, A, B)= A[0]*B[0] + A[1]*B[1]+ .. + A[n-1]*B[n-1]+A[n]*B[n]
Dot( n, A, B)= Dot(n-1, A, B) +A[n-1]*B[n-1]
Dot product- non recursive
double dotpiteration( int n , double A[], double B[] )
{
double sum1;
sum1 = 0 ;
for ( int i =n ; i >= 0; i --)
sum1 = sum1 + A[i] * B[i];
return ( sum1);
}
Dot product- recursive
double dotp( int n , double A[], double B[])
{
if ( n ==0 ) // base case
return 0;
// recursive case
return (A[n ]* B[n ]+ dotp( n - 1 , A, B));
}
Series expansion for ln(2)
ln(2)= 1-1/2+ 1/3- 1/4 + 1/5-
Let double ln2(int n) be a function that computes the first n terms of the expansion.
We note that
ln2(1) = 1
ln2(4)= 1-1/2+1/3-1/4
ln2(5)=1-1/3+1/3-1/4+1/5=ln2(4)+1/5
ln2(n) =ln2(n-1)+1./n
Nonrecursive function ln2
double ln2(int )
{ double sum=0, sign=1.;
for (int I = 1; I <= n ; I++)
{ sum =+sign/I;
sign=-sign;}
return sum;
}
Recursive function ln2
double ln2(int n)
{
if (n==1)return 1;
if (n%2 ==0)
return ln2(n-1) - 1.0/n;
return ln2(n-1)+1.0/n;
}
Maximum element of an array A
We will first assume we have the function max which takes the maximum of 2 elements.
The function maxa(n, A) will take the maximum of the first n elements of A.
Given A={2,5,3,8,6,9}
Maxa(4, A) is 8
Maxa(5, A) is 8
Maxa(6,A) is 9
so that maxa(n,A) = max(A[n],maxa(n-1,A);
Maximum element- nonrecursive
Given the function max given by
double max( double x, double y){ if (x>y)
return x;else
return y;}
The function maxa( int n, double A) would be
double maxa( int n, double A[])
{int max1=A[0];
for (int I =0; I < n; I++)
max1=max(max1, A[I]);
return max1;}
Maximum element of an array-recursive
double maxa( int n, double A[])
{
if (n==0)
return A[0]; //base case
return max(A[n],maxa(n-1, A)); //recursive case
}
Reverse the digits of an integer
Consider the number 347.
347 % 10 = 7 so print it out.
347/10 = 34
34 % 10 = 4 so print it out.
34/10 = 3
3 % 10 = 3 so print it out.
Altogether we get 743.
Reverse digits- no recursion
void reverseiter( int number )
{
while (number >0)
{
cout << number % 10 ;
number = number /10;
}
}
Reverse digits- recursion
void reverse( int number )
{
if ( number !=0 )
{
cout << number % 10 ;
reverse (number /10);
}
}
Greatest common divisor
Consider the numbers 32 and 24.
24=3 x 2 x 2 x 2; 32 = 2 x 2 x 2 x 2 x2
gcd(32,24) = 2 x 2 x 2 = 8
Algorithm for gcd (x,y):
Let z = x%y (in our case 8)
gcd(x,y)= gcd(y,z)= gcd(24,8)
z = 24%8 = 0 so gcd(24,8)=gcd(8,0)
If y is 0 return x so solution is 8.
Greatest common divisor recursive code
int gcd( int x, int y) { int z;if ( y =0) // base case return x; { // recursive case
z= x% y; z = gcd(y, z);
return z; } }
Insertion sort
Assume you have an array A= {6,2,4,3}
Look at first 2 and sort these to 2,6
Now find place for 4 by sliding 6 over to get 2 _ 6 and putting 4 in underlined space to get 2 4 6
(Note always look at rightmost of sorted list and slide over)
Now find place for the number 3 by sliding numbers over that are bigger:
2 4 _ 6
2 _ 4 6
Put in 3 in empty slot
2 3 4 6
Insertion sort- nonrecursion:
void insertsort( int n , int A[]){ int j, k, temp; for (k=1;k<n;k++) {
temp=A[k]; j=k-1; //begin at guy directly to left while (j>=0 && temp < A[j]) {
A[j+1]=A[j]; //slide numbers over j--; }
A[j+1]=temp; //insert number in empty slot }
}
Insertion sort recursion
void insertsort( int n , int A[]){
int j, temp; if ( n>0) {
insertsort(n-1, A); //recursion temp =A[n-1]; // now find place for new entry j = n-2; while (j>=0 && temp < A[j]) {
A[j+1]=A[j]; //slide bigger numbers over
j--; } A[j+1]=temp; //insert new number in empty space}
}
Tower of hanoi
To move 2 disks from peg 1 to peg 2:
move disk 1 from peg 1 to peg 3
move disk 2 from peg 1 to peg 2
move disk 1 from peg 3 to peg 2
To move 3 disks from peg 1 to peg 2:
move the top 2 disks from peg 1 to peg 3(recursion)
move disk 3 from peg 1 to peg 2
move the top 2 disks from peg 3 to peg 2(recursion)
Count the moves: moves(2)=3
moves(3)=moves(2) +1 + moves(2) = 7
moves(4)=moves(3) + 1 + moves(3) = 15
In general
moves(n)=2*moves(n-1)+1
Using recursion to count the number of moves
int moves(int n)
{
if (n==1)
return 1; //base case
return (2*moves(n-1)+1);
}