Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.36 KB | None | 0 0
  1. /* @author Connor Guerin
  2. * @date February 23, 2017
  3. * @purpose Question 5 of homework #5
  4. */
  5.  
  6. #include <iostream>
  7. #include <cstdlib>
  8. #include <string>
  9. #include <vector>
  10. #include <cmath>
  11.  
  12. using namespace std;
  13.  
  14. long evenLucasSum(int n);
  15.  
  16. long long * firstDDigitLucas(int d);
  17.  
  18. int main() {
  19. cout << "--- Question 5a ---" << endl;
  20.  
  21. //Runs evenLucasSum() (Question 5a)
  22. cout << "Value for N:" << endl;
  23. int n;
  24. cin >> n;
  25. cout << "Sum of even numbers in Lucas Sequence of " << n << " + 1 numbers: ";
  26. cout << evenLucasSum(n) << endl;
  27.  
  28. cout << "--- Question 5b ---" << endl;
  29.  
  30. //Runs firstDDigitLucas() (Question 5b)
  31. cout << "Value for D:" << endl;
  32. int d;
  33. cin >> d;
  34. long long * result;
  35. //result[0] is index, resut[1] is the value at that index
  36. result = firstDDigitLucas(d);
  37. cout << "First value with " << d << " digits occurs at index " << result[0] << " and has a value of " << result[1] << endl;
  38. }
  39.  
  40. //Question 5a
  41. long evenLucasSum(int n) {
  42. long lucasSequence[n + 1];
  43.  
  44. //Creates the Lucas Sequence
  45. lucasSequence[0] = 2;
  46. lucasSequence[1] = 1;
  47. for (int i = 2; i < n + 1; i ++) {
  48. lucasSequence[i] = lucasSequence[i - 1] + lucasSequence[i - 2];
  49. }
  50.  
  51. //Sums all even numbers in Sequence
  52. long sum = 0;
  53. for (int i = 0; i < n + 1; i ++) {
  54. //Checks if number is even
  55. if (lucasSequence[i] % 2 == 0) {
  56. sum += lucasSequence[i];
  57. }
  58. }
  59.  
  60. return sum;
  61. }
  62.  
  63. //Question 5b
  64. long long * firstDDigitLucas(int d) {
  65. //Creates a Lucas sequence to build on
  66. vector<long long> lucasSequence = {2, 1};
  67.  
  68. //Creates an array to store return values
  69. //result[0] is index, resut[1] is the value at that index
  70. long long result[2];
  71. long long * pointResult = result;
  72.  
  73. //Searches the sequence for the first number with d digits
  74. int index = 0;
  75. int digitCompare = pow(10, d - 1);
  76. bool indexFound = false;
  77. while (indexFound == false) {
  78. //Checks if the value at index has d digits
  79. if ((lucasSequence[index] / digitCompare) >= 1) {
  80. result[0] = index;
  81. result[1] = lucasSequence[index];
  82. indexFound = true;
  83. }
  84. else {
  85. //Adds to the Lucas Sequence
  86. if (index == lucasSequence.size() - 1) {
  87. lucasSequence.push_back(lucasSequence.at(index) + lucasSequence.at(index - 1));
  88. //Used for debugging
  89. //cout << "Added: " << lucasSequence[index] << endl;
  90. }
  91. index ++;
  92. }
  93. }
  94.  
  95. return pointResult;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement