Advertisement
_code_feedback_

Task 2: Number Series

Apr 2nd, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. /*
  2.     File: main.cpp
  3.  
  4.     Task: Limited prime factor series
  5.  
  6.     Author: *REDACTED*
  7.  
  8.     Created: *REDACTED*
  9.  
  10.     Desc: File containing the main function implementing a solution to task 2 of the assessment
  11.     dealing with generating a limited prime factor series.
  12. */
  13.  
  14.  
  15. #include <iostream>
  16. #include <algorithm>
  17. #include <conio.h>
  18. #include <vector>
  19. #include <assert.h>
  20.  
  21. #define DEBUG 0;
  22.  
  23. using namespace std;
  24.  
  25.  
  26. // Main function generating the desired value at a given position in the series
  27. /*
  28.     Algorithm:
  29.     * model the problem as a need to generate numbers of the form { 2^i * 3^j * 5^k }, for all possible i, j, k, in increasing order
  30.     * start with i, j, k being 0, then increment one at each step, ensuring the smallest result is chosen
  31.     * populate a growing series with the result of each step, and reference previous values in order to keep track of incrementing powers
  32.     * return the last value populated when the required series position is reached
  33.  
  34.     Notes:
  35.     - Initially implemented passing in the 'resultsVector' by reference, allowing it to be used outside the scope of this method
  36.     - Eventually decided to keep the actual generated list local, since no specification was provided for actually having access to the series after its populated
  37. */
  38. int ProvideValueAtPosition(int position)
  39. {
  40.     // The resultsVector will be populated with the series as its increasing
  41.     vector<int> resultsVector = { 1 };
  42.  
  43.     // Track the position we are looking for, start at 1 since we are adding '1' to the vector by definition
  44.     int counter = 1;
  45.  
  46.     // Track the power indicies for the required primes i.e. 'i' tracks powers of 2, 'j' of 3, etc
  47.     int i, j, k;
  48.     i = j = k = 0;
  49.  
  50.     // Build the series up to the requested position
  51.     while (counter < position)
  52.     {
  53.         // Find the minimum next value for the series, depending on the preceeding values and powers of the primes
  54.         int nextValue = min({ 2 * resultsVector[i], 3 * resultsVector[j], 5 * resultsVector[k] });
  55.         resultsVector.push_back(nextValue);
  56.  
  57.         // Increment the power indicies depending on which prime was the smallest
  58.         if (nextValue == 2 * resultsVector[i])
  59.         {
  60.             i++;
  61.         }
  62.  
  63.         if (nextValue == 3 * resultsVector[j])
  64.         {
  65.             j++;
  66.         }
  67.  
  68.         if (nextValue == 5 * resultsVector[k])
  69.         {
  70.             k++;
  71.         }
  72.  
  73. #if DEBUG
  74.         cout << endl;
  75.         cout << "counter: " << counter << endl;
  76.         cout << "nextValue: " << nextValue << endl;
  77.         cout << "{i, j, k}: " << "{ " << i << ", " << j << ", " << k << " }" << endl;
  78. #endif
  79.         counter++;
  80.     }
  81.  
  82.     return resultsVector.back();
  83. }
  84.  
  85. int main()
  86. {
  87.     cout << "Task 2: Limited prime factor series \n" << endl;
  88.  
  89.     // The required position of the series we are looking for
  90.     int requiredPosition = 1500;
  91.  
  92.     int result = ProvideValueAtPosition(requiredPosition);
  93.  
  94.     cout << "Value at position: " << requiredPosition << " is: " << result << endl;
  95.  
  96.     // Assert whether we got the correct value for position 1500
  97.     if( requiredPosition == 1500 )
  98.         assert(result == 859963392);
  99.  
  100.     // Dirty getch to see terminal output before termination
  101.     _getch();
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement