Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Develop recursive methods for the Sum of the Squares of the first N positive Integers Develop the Recursive Method for n factorial PROVIDED CODE: public

Develop recursive methods for the Sum of the Squares of the first N positive Integers

Develop the Recursive Method for n factorial

PROVIDED CODE:

public class Recursion1 { public static int sumOfFirstNIntsIter(int n) { //Pre: n must be a postive integer //Post: Return the sum of the first n positive integers //if n= 5 return 1+2+3+4+5 -> 15 //sum of the first n ints is n(n+1)/2 -> 5(6)/2=15 //1+2+ 3+..........+98+99+100-> 50(101)->5050 int sum=0; for(int i=1;i<=n;i++) { sum=sum+i; } return sum; } public static int sumOfFirstNIntsRec(int n) { //Pre: n must be a postive integer //Recursively //Post: Return the sum of the first n positive integers //if n= 5 return 1+2+3+4+5 -> 15 //sum of the first n ints is n(n+1)/2 -> 5(6)/2=15 //1+2+ 3+..........+98+99+100-> 50(101)->5050 if(n==1) return 1; //base case is the way out of recursion else return n+sumOfFirstNIntsRec(n-1); } public static int sumOfSquares1(int n) { //Pre: n must be a positive integer //Post: return the sum of the squares of the first n postive integers //This will be an iterative approach //n=5 return 1^2+2^2+3^2+4^2+5^2=1+4+9+16+25=55 int sum=0; for(int i=1;i<=n;i++) { sum=sum +(int)Math.pow(i, 2); }//end for loop return sum; }

public static int sumOfSquares2(int n) { //Pre: n must be a positive integer //Post: return the sum of the squares of the first n postive integers //This will be an RECURSIVE approach //n=5 return 1^2+2^2+3^2+4^2+5^2=1+4+9+16+25=55 return 0; } public static int factorial1(int n) { //Pre: n must be a postive integer //Post return n!-> n factorial-> 5!=5*4*3*2*1=120 //Iterative Approach int sum=1; for(int i=1;i<=n;i++) { sum=sum*i; } return sum; } public static int factorial2(int n) { //Pre: n must be a postive integer //Post return n!-> n factorial-> 5!=5*4*3*2*1=120 //Recursive Approach return 1; } public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(sumOfFirstNIntsIter(5)); System.out.println(sumOfFirstNIntsIter(100)); System.out.println(sumOfFirstNIntsRec(5)); System.out.println(sumOfFirstNIntsRec(100)); System.out.println("Sum of the squares of the first n positive integers"); System.out.println("Iteratively"); System.out.println(sumOfSquares1(5)); System.out.println("Recursively"); System.out.println(sumOfSquares1(5)); //change the 1 to 2 System.out.println("n!-> n factorial"); System.out.println("Iteratively"); System.out.println(factorial1(5)); System.out.println("Recursively"); System.out.println(factorial1(5)); //change the 1 to 2 }

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle 11G SQL

Authors: Joan Casteel

2nd Edition

1133947360, 978-1133947363

More Books

Students also viewed these Databases questions

Question

| Who are people who model the values that I want to live?

Answered: 1 week ago