Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class RecursionSum {
- public static void main(String[] args) throws Exception{
- System.out.println ("Computing Sum number at 1 Terms is "+sum(1));
- System.out.println ("Computing Sum number at 3 Terms is "+sum(3));
- System.out.println ("Computing Sum number at 7 Terms is "+sum(7));
- }
- //Create the Recursive Sum Class. Each term is i/2i+1
- public static double sum(int index){
- System.out.println("In sum Calcuating for terms " +index);
- double b;
- //Stopping condition needed for each recursive function
- if(index==1){
- System.out.println("In sum returning Num = 1 dem = 3");
- return 1.0/3.0;
- }
- else{
- //Recursie call before the return
- b=sum(index-1);
- System.out.println("Sum of "+index+" terms for terms "+ (index-1)+" "+b+"+ "+index+ " / (" + (2*index)+"+1) ");
- return b+index/(2.0*index+1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement