Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // a bunch of specific sum fn's
- const sumRange = (min, max) => {
- const fastSumRange = (num, total = 0) =>
- num > max
- ? total
- : fastSumRange(num + 1, total + num);
- return fastSumRange(min);
- }
- const altSumRange = (min, max) => {
- let total = 0;
- for (let num = min; num <= max; num++) total += num;
- return total;
- }
- // ------------------------------------------------
- const cube = num => num * num * num;
- const sumCubes = (min, max) => {
- const fastSumCubes = (num, total = 0) =>
- num > max
- ? total
- : fastSumCubes(num + 1, total + cube(num));
- return fastSumCubes(min);
- }
- const altSumCubes = (min, max) => {
- let total = 0;
- for (let num = min; num <= max; num++) total += cube(num);
- return total;
- }
- // ------------------------------------------------
- const updatePiTotal = num => 1 / (num * (num + 2));
- const piSum = (min, max) => {
- const fastPiSum = (num, total = 0) =>
- num > max || !(min > 0)
- ? total
- : fastPiSum(num + 4, total + updatePiTotal(num));
- return fastPiSum(min);
- }
- const altPiSum = (min, max) => {
- let total = 0;
- for (let num = min; num > 0 && num <= max; num += 4)
- total += updatePiTotal(num);
- return total;
- }
- // ------------------------------------------------
- // General Sum Fn
- const generalSum = (min, max, updateMinFn, updateNumFn) => {
- const fastGeneralSum = (num, total = 0) =>
- num > max
- ? total
- : fastGeneralSum(updateMinFn(num), total + updateNumFn(num));
- return fastGeneralSum(min);
- }
- const altGenSum = (min, max, updateMinFn, updateNumFn) => {
- let total = 0;
- for (let num = min; num <= max; num = updateMinFn(num))
- total += updateNumFn(num);
- return total;
- }
- // helpers for later
- const add = a => b => a + b;
- const inc = add(1);
- const identity = thing => thing;
- // specific sum fn's created using generalSum
- const genSumRange = (min, max) =>
- generalSum(min, max, inc, identity);
- const altGenSumRange = (min, max) =>
- altGenSum(min, max, inc, identity);
- const genSumCubes = (min, max) =>
- generalSum(min, max, inc, cube);
- const altGenSumCubes = (min, max) =>
- generalSum(min, max, inc, cube);
- const genPiSum = (min, max) =>
- generalSum(
- min,
- max,
- add(4),
- updatePiTotal
- );
- const altGenPiSum = (min, max) =>
- altGenSum(
- min,
- max,
- add(4),
- updatePiTotal
- );
- // curried
- const genSumCurr = (updateMinFn, updateNumFn) => ([ min, max ]) => {
- const fastGeneralSum = (num, total = 0) =>
- num > max
- ? total
- : fastGeneralSum(updateMinFn(num), total + updateNumFn(num));
- return fastGeneralSum(min);
- }
- const genSumRangeCurr = genSumCurr(inc, identity);
- const genSumCubesCurr = genSumCurr(inc, cube);
- const genPiSumCurr = genSumCurr(
- add(4),
- updatePiTotal
- );
- // fn programming which expects data last like curried fns
- const compose =
- (...fns) => initialData =>
- fns.reduceRight((updatedData, fn) =>
- fn(updatedData)
- , initialData);
- const range = (min, max) =>
- Array
- .from(Array(++max - min))
- .map((_, i) => i + min);
- const reduce = startingValue => fn => coll =>
- coll.reduce((updatedVal, val) =>
- Array.isArray(startingValue)
- ? updatedVal.concat(fn(val))
- : updatedVal += fn(val)
- , startingValue);
- const sum = reduce(0);
- const map = reduce([]);
- const filter = fn => coll =>
- coll.filter(fn);
- const genSumRangeFnProg = sum(identity);
- const genSumCubesFnProg = sum(cube);
- const isBelow = max => num => num <= max;
- const getPiNums = coll => coll.reduce((piNums, rangeNum, i) => {
- const prevPiNum = piNums[i - 1];
- return !prevPiNum
- ? piNums.concat(rangeNum)
- : piNums.concat(prevPiNum + 4)
- }, []);
- const genPiSumFnProg = (min, max) => compose(
- sum(updatePiTotal),
- filter(isBelow(max)),
- getPiNums
- )(range(min, max));
- const min = 1;
- const max = 10;
- const fnData = range(min,max);
- // console.log(
- // '-----------\n',
- // sumRange(min, max),
- // altSumRange(min, max),
- // genSumRange(min, max),
- // altGenSumRange(min, max),
- // genSumRangeCurr([min, max]),
- // genSumRangeFnProg(fnData),
- // '\n-----------\n',
- // sumCubes(min, max),
- // altSumCubes(min, max),
- // genSumCubes(min, max),
- // altGenSumCubes(min, max),
- // genSumCubesCurr([min, max]),
- // genSumCubesFnProg(fnData),
- // '\n-----------\n',
- // piSum(min, max),
- // altPiSum(min, max),
- // genPiSum(min, max),
- // altGenPiSum(min, max),
- // genPiSumCurr([min, max]),
- // genPiSumFnProg(min, max)
- // );
- // definite integral
- const integral = (fn, x, limit, dx) =>
- dx *
- altGenSum(
- (x + (dx / 2)),
- limit,
- term => term + dx,
- fn
- );
- // alternate definite integral
- const simpsonIntegral = (fn, x, limit, totalIntervals) => {
- if (totalIntervals % 2 !== 0) {
- console.error('totalIntervals must be an even integer');
- return 0;
- }
- const dx = (limit - x) / totalIntervals;
- const h = dx / 3;
- const getConstant = (function* () {
- while (true)
- for (let constant of [4,2])
- yield constant;
- })();
- const addDx = num => num + dx;
- const getNextSumTerm = term =>
- term === x || // first term
- term < limit && addDx(term) >= limit // last valid term
- ? fn(term) // do not multiply by constant
- : term >= limit
- ? 0 // we don't want area when term equals limit
- : fn(term) * getConstant.next().value; // multiply by constant
- return h * altGenSum(x, limit, addDx, getNextSumTerm);
- };
- /*console.log(
- integral(cube, 0, 1, 0.0001),
- simpsonIntegral(cube, 0, 1, 10000)
- )*/
- const product = (min, max) => {
- const fastProduct = (currMin, total = 0) =>
- currMin === 0 || currMin > max
- ? total
- : fastProduct(currMin + 1, currMin * (total === 0 ? 1 : total));
- return fastProduct(min);
- }
- const altProduct = (min, max) => {
- if (min === 0 || max === 0) return 0;
- let total = 1;
- for (let num = min; num <= max; num++)
- total *= num
- return total;
- }
- const factorial = n =>
- product(1, n);
- const add = (a, b) => a + b;
- const multiply = (a, b) => a * b;
- const accumulate = (min, max, fn, accumulator) => {
- const fastAccumulate = (currMin, currAccumulator) =>
- min > max
- ? currAccumulator
- : fastAccumulate(currMin + 1, fn(accumulator, currMin));
- return fastAccumulate(min, accumulator);
- }
- const altAccumulate = (fn, accumulator) => (min, max) => {
- let total = accumulator;
- for (num = min; num <= max; num++)
- total = fn(num, total);
- return total;
- }
- const sum = altAccumulate(add, 0);
- const prod = altAccumulate(multiply, 1);
- const filterAcc = (fn, predFn, accumulator) => (min, max) => {
- let currAccumulator = accumulator;
- for (let num = min; num <= max; num++)
- if (predFn(num)) (currAccumulator = fn(currAccumulator, num));
- return currAccumulator;
- }
- const isPrime = num => {
- if (num < 2) return false;
- let smallestDivisor = 2;
- while (true) {
- if ((smallestDivisor * smallestDivisor) > num) {
- smallestDivisor = num;
- break;
- }
- if (num % smallestDivisor++ === 0) break;
- }
- return num === smallestDivisor;
- }
- const sumPrimes = filterAcc(add, isPrime, 0);
- const eulersTotientProd = num => {
- const isX = int => gcd(int, num) === 1;
- return filterAcc(multiply, isX, 1)(1,num);
- }
- eulersTotientProd(10);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement