Question: Homework 1: Basic Recursion Give all definitions as recursive (or indirectly recursive) C++ functions. No loops!(for , while, etc.) Some questions are substantially similar to
Homework 1: Basic Recursion Give all definitions as recursive (or indirectly recursive) C++ functions. No loops!(for , while, etc.) Some questions are substantially similar to others, so that if you get help on one problem, you can try similar problems on your own.
1. Define function sum(n)which returns the sum of the first n positive integers, 1+2+3+ . . . +n.
2. Define function sum_squares(n)which returns the sum of the first n squares, 12 +2 2 +3 2 + . . . +n 2.
3. Define function sum_cubes(n)which returns the sum of the first n cubes, 13 +2 3 +3 3 + . . . +n 3. 4. Define function print_threes(n)which prints the digit 3 to the console n times. 5. Define function print_quacks(n)which prints "Quack!" to the console n times. 6. Define function print_sequence(m,n)which prints the sequence of integers from m ton, inclusive. If m
unsigned long int x(unsigned long int n) { if (n == 0) return 1; else if (n == 1) return 2; else if (n == 2) return 3; else return x(n-1) + x(n-2) + x(n-3); } Alas, it takes forever to compute x50 using this function. Redefine this function using a recursive helper function, x_helper,so computing x50 takes milliseconds, not minutes or hours. 10. Using a tail recursive helper function, define the factorial function, factorial(n), which returns n!, the product of the first n positive integers. Note: 0! equals 1.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
