SHARE
TWEET

Untitled

a guest Feb 22nd, 2019 82 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class CompositionNode {
  2.     constructor() {
  3.         /**
  4.          * @type {CompositionNode[]}
  5.          */
  6.         this.children = [];
  7.         this.sum = 0n;
  8.     }
  9.  
  10.     /**
  11.      *
  12.      * @param {CompositionNode} child
  13.      */
  14.     addChild(child) {
  15.         this.children.push(child);
  16.     }
  17. }
  18.  
  19. /**
  20.  * @param {number} n
  21.  * @param {number[]} values
  22.  * @returns {CompositionNode}
  23.  */
  24. function buildCompositionGraph(n, values) {
  25.     const valueNodes = new Array(n + 1);
  26.     for (let i = 0; i <= n; i++) {
  27.         valueNodes[i] = new CompositionNode();
  28.     }
  29.     for (let i = 0; i < valueNodes.length; i++) {
  30.         for (let j = 0; values[j] + i <= n && j < values.length; j++) {
  31.             valueNodes[i].addChild(valueNodes[values[j] + i]);
  32.         }
  33.     }
  34.  
  35.     return valueNodes[0];
  36. }
  37.  
  38. /**
  39.  * @param {CompositionNode} graph
  40.  */
  41. function traverseGraph(graph) {
  42.     if (graph.children.length === 0) {
  43.         graph.sum = 1n;
  44.         return;
  45.     }
  46.  
  47.     let sum = 0n;
  48.     for (let i = 0; i < graph.children.length; i++) {
  49.         const child = graph.children[i];
  50.         if (child.sum) {
  51.             sum += child.sum;
  52.         } else {
  53.             traverseGraph(child);
  54.             sum += child.sum;
  55.         }
  56.     }
  57.     graph.sum = sum;
  58. }
  59.  
  60. /**
  61.  * @param {number} n
  62.  * @param {number[]} values
  63.  * @returns {BigInt}
  64.  */
  65. function countCompositions(n, values) {
  66.     const graph = buildCompositionGraph(n, values);
  67.     traverseGraph(graph);
  68.     return graph.sum;
  69. }
  70.  
  71. /**
  72.  * @param {number} n
  73.  * @param {number[]} values
  74.  * @returns {BigInt[]}
  75.  */
  76. function countCompositionsUpTo(n, values) {
  77.     const sums = new Array(n);
  78.     for (let i = 0; i < n; i++) {
  79.         sums[i] = countCompositions(i + 1, values);
  80.     }
  81.     return sums;
  82. }
  83.  
  84. const hebrew = [
  85.     1,
  86.     2,
  87.     3,
  88.     4,
  89.     5,
  90.     6,
  91.     7,
  92.     8,
  93.     9,
  94.     10,
  95.     20,
  96.     30,
  97.     40,
  98.     50,
  99.     60,
  100.     70,
  101.     80,
  102.     90,
  103.     100,
  104.     200,
  105.     300,
  106.     400
  107. ];
  108. const english = [...new Array(26)].map((_, i) => i + 1);
  109.  
  110.  
  111. const len = 150;
  112. const cellSize = 6;
  113. const radix = 2;
  114. const compositionsUpTo = new Array(len);
  115. const canvas = document.getElementById('cvs');
  116. canvas.width = len * cellSize;
  117. canvas.height = len * cellSize;
  118. const ctx = canvas.getContext('2d');
  119.  
  120. function countLoop(begin, end) {
  121.     let mod = end - begin;
  122.     let i = 0;
  123.  
  124.     const shades = {};
  125.     for (let i = 0; i < radix; i++) {
  126.         const digit = i.toString(radix);
  127.         const shadeByte = 255 - Math.ceil(255 * i / (radix - 1));
  128.         shades[digit] = '#' + shadeByte.toString(16).padStart(2, '0').repeat(3);
  129.     }
  130.  
  131.     const lastDigits = [...new Array(len)].map(() => new Array(len));
  132.  
  133.     window.requestAnimationFrame(function cb() {
  134.         i = i % mod;
  135.         compositionsUpTo[i] = compositionsUpTo[i] || countCompositionsUpTo(len, [...new Array(i + begin)].map((_, i) => i + 1)).map(val => val.toString(radix).padStart(len, '0'));
  136.         const comps = compositionsUpTo[i];
  137.  
  138.         for (let i = 0; i < radix; i++) {
  139.             const digit = i.toString(radix);
  140.             ctx.beginPath();
  141.             ctx.fillStyle = shades[digit];
  142.             for (let row = 0; row < comps.length; row++) {
  143.                 const str = comps[row];
  144.                 for (let col = 0; col < str.length; col++) {
  145.                     const currentDigit = comps[row][col];
  146.                     if (currentDigit === digit && lastDigits[row][col] !== currentDigit) {
  147.                         ctx.rect(col * cellSize, row * cellSize, cellSize, cellSize);
  148.                     }
  149.                 }
  150.             }
  151.             ctx.fill();
  152.         }
  153.  
  154.         i++;
  155.         window.requestAnimationFrame(cb);
  156.     });
  157. }
  158. countLoop(2, len + 2);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top