Advertisement
leomaster

Math fun

Jun 28th, 2018 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Greatest Common Divisor
  2. const gcd = (x, y) => x ? gcd(y % x, x) : y
  3. // Lowest Common Multiple
  4. const lcm = (x, y) => x * y / gcd(x, y)
  5. // Modulo
  6. const mod = (n, d) => (n % d + d) % d
  7.  
  8. inline int modinverse(int a, int m) {
  9.     int x;
  10.     for(a%=m,x=1;x<m;++x)
  11.         if (a*x%m==1)
  12.             return x;
  13. }
  14.  
  15. // Permutations
  16. const P=(m,n)=>!n||m--*P(m,--n)
  17.  
  18. // Combinations
  19. const C=(m,n)=>n<=m&&P(m,n)/P(n,n)
  20.  
  21. // Factorial
  22. const f = n => !n || n * f(--n)
  23. const ƒ=ǃ=>!ǃ||ǃ--*ƒ(ǃ)
  24. const F=n=>P(n,n)
  25. (function(ǃ) { return !ǃ||ǃ*arguments.callee(~-ǃ) })(5)
  26.  
  27.  
  28. // PowerSet
  29. const ps = A => A.reduceRight((a, i) => [...a,...a.map(j => [...j,i])], [[]]);
  30.  
  31.  
  32. map = ([h,...t],i) => !!h ?[...h,i,...map(t,i)] : []
  33.  
  34. rps = ([h,...t]) => !!h ? [...rps(t), ...rps(t).map(i => [...i,h])] : [[]]
  35.  
  36. 𝒫=()=>(λ=(ʔ,)=>ʔ--?[[...((ʙ=ǃ=>ǃ?ʙ(ǃ>>!!ǃ)+[ǃ&!!ǃ]:[])(ʔ)+'').padStart(,'0')].reduce((Σ,ε,δ)=>+ε?[...Σ,[δ]]:Σ,[]), ...λ(ʔ,)]:[])(!!λ<<(=Ꮪ.length),)
  37.  
  38. //make array of N elements and fill values 1..N
  39. ʃ=ǃ=>ǃ--?[...ʃ(ǃ),++ǃ]:[]
  40.  
  41. //Alphabet
  42. String.fromCharCode(...ʃ(0xFF))
  43.  
  44. //ꮅ (prime check)
  45. =(ʔ,ǃ=~-ʔ)=>~-ʔ&&!~-ǃ||!!(ʔ%ǃ)&&(ʔ,~-ǃ)
  46.  
  47. SieveOfEratosthenes = N => ʃ(N).reduce((a,i) =>(i) ? [...a, i] : a, [])
  48.  
  49. //Prime Factors
  50. pf = (n,f,i=f?~-n:2,A=[]) => f ? ~-n && !~-i || !!(n%i) && pf(n,1,~-i) : !~-n ? A : !(n%i) && pf(i,1) ? (A=[...A,i], pf(n/i,0,i,A)) : pf(n,0,-~i,A)
  51.  
  52. //Fibonacci
  53. fib = n => n && ~-n && fib(--n)+fib(--n) || n
  54. fib = n => ~-n<-n ? -n : fib(--n)+fib(--n)
  55.  
  56. //  Fib BF
  57. //  >++++++++++>+>+[
  58. //      [+++++[>++++++++<-]>.<++++++[>--------<-]+<<<]>.>>[
  59. //          [-]<[>+<-]>>[<<+>+>-]<[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-
  60. //              [>+<-[>+<-[>+<-[>[-]>+>+<<<-[>+<-]]]]]]]]]]]+>>>
  61. //      ]<<<
  62. //  ]
  63.  
  64. //Digitsum
  65. Σ=(ε,δ=10)=>ε&&Σ(ε/δ)+~~ε%δ
  66.  
  67. //Sum
  68. Σ = ([α,...n]) => !!α &&α +Σ(n)
  69.  
  70. Σ=([ǃ,...λ])=>!!ǃ&&ǃ+Σ(λ)
  71.  
  72. Σ=([ǃ,...ʔ],ː)=>ː?~~ʔ?Σ([ʔ^ǃ,(ʔ&ǃ)<<!!ǃ],ǃ):ǃ:!!ǃ&&Σ([ǃ,Σ(ʔ)],ǃ)
  73.  
  74. //Bin
  75. b=n=>n?b(n>>!!n)+[n&!!n]:[]
  76.  
  77. //strrev
  78. strrev = ([h,...t]) => h?[...strrev(t),h]:[]
  79.  
  80. // [0,1,2,...,100] -> [0,-1,1,-2,2,...-99,99] -> [0,1,2...,100]
  81. [...Array(100).keys()].map(n=>((n >> 1) ^ (-(n & 1)))).map(m=>(m << 1) ^ (m >> 31))
  82.  
  83. arousal     0 ... 100
  84. valence   -99 ... 99
  85.  
  86.  
  87.  
  88. 2**53 = 9007199254740992 = 9007199254740992 + 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement