Advertisement
mrkarp

Progression

Oct 24th, 2013
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Progression
  4. {
  5. public static void main(String [] args)
  6. {
  7.  
  8. Scanner keyboard = new Scanner (System.in);
  9. System.out.println(
  10. "This program will calculate the geometric and ");
  11. System.out.println(
  12. "harmonic progression for the number you enter.");
  13. System.out.print(
  14. "Enter an integer that is greater than or equal to 1: ");
  15. int input = keyboard.nextInt();
  16. int geomAnswer = geometricRecursive (input);
  17. double harmAnswer = harmonicRecursive (input);
  18. System.out.println("Using recursion:");
  19. System.out.println("The geometric progression of " + input + " is " + geomAnswer + "\n");
  20. System.out.println("The harmonic progression of " + input + " is " + harmAnswer + "\n");
  21.  
  22. geomAnswer = geometricIterative (input);
  23. harmAnswer = harmonicIterative (input);
  24. System.out.println("Using iteration:");
  25. System.out.println("The geometric progression of " + input + " is " + geomAnswer + "\n");
  26. System.out.println("The harmonic progression of " + input + " is " + harmAnswer + "\n");
  27.  
  28. } // end main method
  29.  
  30. static int geometricIterative(int baseCase){
  31.  
  32. int result = 1;
  33. for(int i = 1; i < baseCase; i++){
  34.  
  35. int tempMulti;
  36. tempMulti = i + 1;
  37. result =+ result * tempMulti;
  38. // remove comment // to visually check the results case by case
  39. System.out.println(i + " * " + tempMulti + " = " + result);
  40.  
  41. } // end for loop
  42.  
  43. return result;
  44.  
  45. } // end geometricIterative() method
  46.  
  47. static double harmonicIterative(int baseCase){
  48.  
  49. double result = 1;
  50. for(int i = 1; i < baseCase; i++){
  51.  
  52. double tempDiv;
  53. tempDiv = i + 1;
  54. result =+ result / tempDiv;
  55. // remove comment // to visually check the results case by case
  56. System.out.println(i + " / " + (int)tempDiv + " = " + result);
  57.  
  58. } // end for loop
  59.  
  60. return result;
  61.  
  62. } // end harmonicIterative() method
  63.  
  64. // Variables needed for the Geometric / Harmonic Recursive
  65. // method
  66.  
  67. // Cycle Keeps track of how many times recursive calls there are.
  68. static int cycles = 1;
  69. // I or D _Result are used for storage of the recursive results
  70. static int I_Result = 1;
  71. static double D_Result = 1;
  72. static int I_Temp_Result = 0;
  73. static double D_Temp_Result = 0;
  74.  
  75. static int geometricRecursive(int baseCase){
  76.  
  77. if(baseCase > cycles){
  78.  
  79. int tempMulti;
  80. tempMulti = cycles + 1;
  81. I_Result =+ I_Result * tempMulti;
  82. // remove comment // to visually check the results case by case
  83. System.out.println(cycles + " * " + tempMulti + " = " + I_Result);
  84. cycles++;
  85. geometricRecursive(baseCase);
  86.  
  87. } // end if
  88.  
  89. else if (baseCase == cycles){
  90.  
  91. I_Temp_Result = I_Result;
  92. return I_Temp_Result;
  93.  
  94. } // end else if
  95.  
  96. cycles = 1;
  97. I_Result = 1;
  98.  
  99. return I_Temp_Result;
  100.  
  101. } // end geometricRecursive() method
  102.  
  103. static double harmonicRecursive(int baseCase){
  104.  
  105. if(baseCase > cycles){
  106.  
  107. double tempDiv;
  108. tempDiv = cycles + 1;
  109. D_Result =+ D_Result / tempDiv;
  110. // remove comment // to visually check the results case by case
  111. System.out.println(cycles + " / " + (int)tempDiv + " = " + D_Result);
  112. cycles++;
  113. harmonicRecursive(baseCase);
  114.  
  115. } // end if
  116.  
  117. // figure out how to return the proper value for both of the recursive methods
  118.  
  119. else if (baseCase == cycles){
  120.  
  121. D_Temp_Result = D_Result;
  122. return D_Temp_Result;
  123.  
  124. } // end else if
  125.  
  126. cycles = 1;
  127. D_Result = 1;
  128.  
  129. return D_Temp_Result;
  130.  
  131. } // end geometricRecursive() method
  132.  
  133. } // end Progression class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement