Advertisement
CLooker

specific/general sum fns

Nov 16th, 2018
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // a bunch of specific sum fn's
  2.  
  3. const sumRange = (min, max) => {
  4.   const fastSumRange = (num, total = 0) =>
  5.     num > max
  6.       ? total
  7.       : fastSumRange(num + 1, total + num);
  8.  
  9.   return fastSumRange(min);
  10. }
  11.  
  12. const altSumRange = (min, max) => {
  13.   let total = 0;
  14.   for (let num = min; num <= max; num++) total += num;
  15.   return total;
  16. }
  17.  
  18. // ------------------------------------------------
  19.  
  20. const cube = num => num * num * num;
  21.  
  22. const sumCubes = (min, max) => {
  23.   const fastSumCubes = (num, total = 0) =>
  24.     num > max
  25.       ? total
  26.       : fastSumCubes(num + 1, total + cube(num));
  27.  
  28.   return fastSumCubes(min);
  29. }
  30.  
  31. const altSumCubes = (min, max) => {
  32.   let total = 0;
  33.   for (let num = min; num <= max; num++) total += cube(num);
  34.   return total;
  35. }
  36.  
  37. // ------------------------------------------------
  38. const updatePiTotal = num => 1 / (num * (num + 2));
  39.  
  40. const piSum = (min, max) => {
  41.   const fastPiSum = (num, total = 0) =>
  42.     num > max || !(min > 0)
  43.       ? total
  44.       : fastPiSum(num + 4, total + updatePiTotal(num));
  45.  
  46.   return fastPiSum(min);
  47. }
  48.  
  49.  
  50. const altPiSum = (min, max) => {
  51.   let total = 0;
  52.   for (let num = min; num > 0 && num <= max; num += 4)
  53.     total += updatePiTotal(num);    
  54.   return total;
  55. }
  56.  
  57. // ------------------------------------------------
  58. // General Sum Fn
  59.  
  60. const generalSum = (min, max, updateMinFn, updateNumFn) => {
  61.   const fastGeneralSum = (num, total = 0) =>
  62.     num > max
  63.       ? total
  64.       : fastGeneralSum(updateMinFn(num), total + updateNumFn(num));
  65.  
  66.   return fastGeneralSum(min);
  67. }
  68.  
  69. const altGenSum = (min, max, updateMinFn, updateNumFn) => {
  70.   let total = 0;
  71.   for (let num = min; num <= max; num = updateMinFn(num))
  72.     total += updateNumFn(num);
  73.   return total;
  74. }
  75.  
  76. // helpers for later
  77. const add = a => b => a + b;
  78. const inc = add(1);
  79. const identity = thing => thing;
  80.  
  81. // specific sum fn's created using generalSum
  82. const genSumRange = (min, max) =>
  83.   generalSum(min, max, inc, identity);
  84.  
  85. const altGenSumRange = (min, max) =>
  86.   altGenSum(min, max, inc, identity);
  87.  
  88. const genSumCubes = (min, max) =>
  89.   generalSum(min, max, inc, cube);
  90.  
  91. const altGenSumCubes = (min, max) =>
  92.   generalSum(min, max, inc, cube);
  93.  
  94. const genPiSum = (min, max) =>
  95.   generalSum(
  96.     min,
  97.     max,
  98.     add(4),
  99.     updatePiTotal
  100.   );
  101.  
  102. const altGenPiSum = (min, max) =>
  103.   altGenSum(
  104.     min,
  105.     max,
  106.     add(4),
  107.     updatePiTotal
  108.   );
  109.  
  110. // curried
  111. const genSumCurr = (updateMinFn, updateNumFn) => ([ min, max ]) => {
  112.   const fastGeneralSum = (num, total = 0) =>
  113.     num > max
  114.       ? total
  115.       : fastGeneralSum(updateMinFn(num), total + updateNumFn(num));
  116.  
  117.   return fastGeneralSum(min);
  118. }
  119.  
  120. const genSumRangeCurr = genSumCurr(inc, identity);
  121. const genSumCubesCurr = genSumCurr(inc, cube);
  122. const genPiSumCurr = genSumCurr(
  123.   add(4),
  124.   updatePiTotal
  125. );
  126.  
  127. // fn programming which expects data last like curried fns
  128. const compose =
  129.   (...fns) => initialData =>
  130.     fns.reduceRight((updatedData, fn) =>
  131.       fn(updatedData)
  132.     , initialData);
  133.  
  134. const range = (min, max) =>
  135.   Array
  136.     .from(Array(++max - min))
  137.     .map((_, i) => i + min);
  138.  
  139. const reduce = startingValue => fn => coll =>
  140.   coll.reduce((updatedVal, val) =>
  141.     Array.isArray(startingValue)
  142.       ? updatedVal.concat(fn(val))
  143.       : updatedVal += fn(val)    
  144.   , startingValue);
  145.  
  146. const sum = reduce(0);
  147. const map = reduce([]);
  148. const filter = fn => coll =>
  149.   coll.filter(fn);
  150.  
  151. const genSumRangeFnProg = sum(identity);
  152. const genSumCubesFnProg = sum(cube);
  153.  
  154. const isBelow = max => num => num <= max;
  155.  
  156. const getPiNums = coll => coll.reduce((piNums, rangeNum, i) => {
  157.   const prevPiNum = piNums[i - 1];
  158.   return !prevPiNum
  159.     ? piNums.concat(rangeNum)
  160.     : piNums.concat(prevPiNum + 4)
  161. }, []);
  162.  
  163. const genPiSumFnProg = (min, max) => compose(
  164.   sum(updatePiTotal),
  165.   filter(isBelow(max)),
  166.   getPiNums
  167. )(range(min, max));
  168.  
  169. const min = 1;
  170. const max = 10;
  171. const fnData = range(min,max);
  172.  
  173. // console.log(
  174. //   '-----------\n',
  175. //   sumRange(min, max),
  176. //   altSumRange(min, max),
  177. //   genSumRange(min, max),
  178. //   altGenSumRange(min, max),
  179. //   genSumRangeCurr([min, max]),
  180. //   genSumRangeFnProg(fnData),
  181. //   '\n-----------\n',
  182. //   sumCubes(min, max),
  183. //   altSumCubes(min, max),
  184. //   genSumCubes(min, max),
  185. //   altGenSumCubes(min, max),
  186. //   genSumCubesCurr([min, max]),
  187. //   genSumCubesFnProg(fnData),
  188. //   '\n-----------\n',
  189. //   piSum(min, max),
  190. //   altPiSum(min, max),
  191. //   genPiSum(min, max),
  192. //   altGenPiSum(min, max),
  193. //   genPiSumCurr([min, max]),
  194. //   genPiSumFnProg(min, max)
  195. // );
  196.  
  197. // definite integral
  198. const integral = (fn, x, limit, dx) =>
  199.    dx *
  200.    altGenSum(
  201.     (x + (dx / 2)),
  202.     limit,
  203.     term => term + dx,
  204.     fn
  205.   );
  206.  
  207. // alternate definite integral
  208. const simpsonIntegral = (fn, x, limit, totalIntervals) => {
  209.   if (totalIntervals % 2 !== 0) {
  210.     console.error('totalIntervals must be an even integer');
  211.     return 0;
  212.   }
  213.  
  214.   const dx = (limit - x) / totalIntervals;
  215.   const h = dx / 3;
  216.  
  217.   const getConstant = (function* () {
  218.     while (true)
  219.       for (let constant of [4,2])
  220.         yield constant;
  221.   })();
  222.  
  223.   const addDx = num => num + dx;
  224.  
  225.   const getNextSumTerm = term =>
  226.     term === x || // first term
  227.     term < limit && addDx(term) >= limit // last valid term
  228.       ? fn(term) // do not multiply by constant
  229.       : term >= limit
  230.         ? 0 // we don't want area when term equals limit
  231.         : fn(term) * getConstant.next().value; // multiply by constant
  232.  
  233.   return h * altGenSum(x, limit, addDx, getNextSumTerm);
  234. };  
  235.  
  236. /*console.log(
  237.   integral(cube, 0, 1, 0.0001),
  238.   simpsonIntegral(cube, 0, 1, 10000)
  239. )*/
  240.  
  241. const product = (min, max) => {
  242.   const fastProduct = (currMin, total = 0) =>
  243.     currMin === 0 || currMin > max
  244.       ? total
  245.       : fastProduct(currMin + 1, currMin * (total === 0 ? 1 : total));
  246.  
  247.   return fastProduct(min);
  248. }
  249.  
  250. const altProduct = (min, max) => {
  251.   if (min === 0 || max === 0) return 0;
  252.   let total = 1;
  253.   for (let num = min; num <= max; num++)
  254.     total *= num
  255.   return total;
  256. }
  257.  
  258. const factorial = n =>
  259.   product(1, n);
  260.  
  261. const add = (a, b) => a + b;
  262. const multiply = (a, b) => a * b;
  263.  
  264. const accumulate = (min, max, fn, accumulator) => {
  265.   const fastAccumulate = (currMin, currAccumulator) =>
  266.     min > max
  267.       ? currAccumulator
  268.       : fastAccumulate(currMin + 1, fn(accumulator, currMin));
  269.  
  270.   return fastAccumulate(min, accumulator);
  271. }
  272.  
  273. const altAccumulate = (fn, accumulator) => (min, max) => {
  274.   let total = accumulator;
  275.   for (num = min; num <= max; num++)
  276.     total = fn(num, total);
  277.   return total;
  278. }
  279.  
  280. const sum = altAccumulate(add, 0);
  281. const prod = altAccumulate(multiply, 1);
  282.  
  283. const filterAcc = (fn, predFn, accumulator) => (min, max) => {  
  284.   let currAccumulator = accumulator;
  285.   for (let num = min; num <= max; num++)
  286.     if (predFn(num)) (currAccumulator = fn(currAccumulator, num));
  287.   return currAccumulator;
  288. }
  289.  
  290. const isPrime = num => {
  291.   if (num < 2) return false;
  292.   let smallestDivisor = 2;
  293.   while (true) {
  294.     if ((smallestDivisor * smallestDivisor) > num) {
  295.       smallestDivisor = num;
  296.       break;
  297.     }
  298.     if (num % smallestDivisor++ === 0) break;
  299.   }  
  300.   return num === smallestDivisor;
  301. }
  302.  
  303. const sumPrimes = filterAcc(add, isPrime, 0);
  304. const eulersTotientProd = num => {
  305.   const isX = int => gcd(int, num) === 1;
  306.   return filterAcc(multiply, isX, 1)(1,num);  
  307. }
  308.  
  309. eulersTotientProd(10);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement