Advertisement
beelzebielsk

sequence-question-possibilities.js

Aug 15th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Function 1:
  2.  * This is based on the observation that the 3 different example
  3.  * series were all contained in the larger series:
  4.  * 6  : 6, 3, 10, 5, 8, 4, 2, 1
  5.  * 10 :       10, 5, 8, 4, 2, 1
  6.  * 4  :                 4, 2, 1
  7.  * So, if I just produced all the numbers in the series, then this
  8.  * should work fine. This approach is more general than just handling
  9.  * 6, 10, and 4. It handles all of the numbers given in series.
  10.  * However... there's not enough information in the question to
  11.  * suggest that this was a CORRECT generalization.
  12.  */
  13. function seriesOf(num) {
  14.     function next(num) {
  15.         if (num == 6) return 3;
  16.         if (num == 3) return 10;
  17.         if (num == 10) return 5;
  18.         if (num == 16) return 8;
  19.         if (num == 8) return 4;
  20.         if (num == 4) return 2;
  21.         if (num == 2) return 1;
  22.     }
  23.     if (num == 1) return [1];
  24.     else {
  25.         let series = [num];
  26.         while () {
  27.             let n = next(num);
  28.             series.push(n);
  29.             if (n == 1) break;
  30.         }
  31.         return series;
  32.     }
  33. }
  34.  
  35. /* Function 2:
  36.  * Source: <https://en.wikipedia.org/wiki/Collatz_conjecture>
  37.  */
  38.  
  39. function seriesOf(num) {
  40.     function collatz(num) {
  41.         return (num % 2 == 0) ? num / 2 : 3*num + 1;
  42.     }
  43.     let series = [num];
  44.     while(collatz(num) != 1) series.push(collatz(num));
  45.     return series;
  46. }
  47.  
  48. /* Function 3:
  49.  * This was the initial pattern that I saw. After sleeping on it, I
  50.  * remembered that I saw a similar sequence before as part of a
  51.  * problem in a class of mine. The sequence is from the Collatz'
  52.  * Conjecture problem, and it turns out that the Collatz' sequence if
  53.  * probably what this problem was hunting for. However, the quesion
  54.  * doesn't actually say enough to guarantee that.
  55.  *
  56.  * Note that, while my function might look similar to collatz, it's
  57.  * different, because for some numbers, the produced results would be
  58.  * different.
  59.  */
  60. function seriesOf(num) {
  61.     let series = [num];
  62.     let current = num;
  63.     // position in sequence of element at the end of series.
  64.     let posInSequence = 0;
  65.     function isPowerOf2(num) {
  66.         // A number is a power of two if it's base 2 logarithm is an
  67.         // integer.
  68.         let actual = Math.log2(num);
  69.         let nearestIntegerExponent = Math.round(actual);
  70.         return actual == nearestIntegerExponent;
  71.     }
  72.     function isOdd(n) { return (n % 2) == 1; };
  73.     function isEven(n) { return !isOdd(n); };
  74.     while (true) {
  75.  
  76.         if (isPowerOf2(current)) series.push(current / 2);
  77.         if (isEven(posInSequence)) series.push(current / 2);
  78.         if (isOdd(posInSequence)) series.push(3 * current + 1);
  79.         posInSequence++;
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement