binomial-coefficients

Efficient algorithm to get the combinations of all items in object

筅森魡賤 提交于 2019-12-04 05:11:24
Given an array or object with n keys, I need to find all combinations with length x . Given X is variable. binomial_coefficient(n,x) . Currently I'm using this: function combine(items) { var result = []; var f = function(prefix, items) { for (var i = 0; i < items.length; i++) { result.push(prefix + items[i]); f(prefix + items[i], items.slice(i + 1)); } } f('', items); return result; } var combinations = combine(["a", "b", "c", "d"]); The output is: ["a", "ab", "abc", "abcd", "abd", "ac", "acd", "ad", "b", "bc", "bcd", "bd", "c", "cd", "d"] So if I want the binomial coefficient x=3 from n=4 I

Algorithm to find Sum of the first r binomial coefficients for fixed n modulo m

妖精的绣舞 提交于 2019-12-03 18:13:23
I am trying to find the sum of the first r binomial coefficients for a fixed n. (nC1 + nC2 + nC3 + ... + nCr) % M where r < = n. Is there an efficient algorithm to solve this problem ? Note that the "first" binomial coefficient for fixed n is nC0 . Let f(n) = nC0 + nC1 + ... + nC(r-1) . Using the "Pascal's triangle" identity, nCk = (n-1)C(k-1) + (n-1)Ck we have nC0 + nC1 + nC2 + ... + nC(r-1) = (n-1)C(-1) + (n-1)C0 + (n-1)C0 + (n-1)C1 + (n-1)C1 + (n-1)C2 + ... + (n-1)C(r-2) + (n-1)C(r-1) = 2[(n-1)C0 + (n-1)C1 + (n-1)C2 + ... + (n-1)C(r-2)] + (n-1)C(r-1) = 2[(n-1)C0 + ... + (n-1)C(r-1)] - (n-1

Strange precision issues in R when computing cumulative binomial probability

我怕爱的太早我们不能终老 提交于 2019-12-01 22:08:23
问题 I've been running into some weird problems when using this code: positions<-c(58256) occurrencies<-c(30) frequency<-c(11/5531777) length<-c(4) prob<-c(0) for(i in 0:(occurrencies-1)) { pow<-frequency^i pow1<-(1-frequency)^(positions-i) bin<-choose(positions, i) prob<<-prob+(bin*pow*pow1) } Each iteration of this for loop should calculate the binomial probability that, i number of occurrences of the event occur given the frequency. Each iteration also sums up the result. This should result in

Strange precision issues in R when computing cumulative binomial probability

蓝咒 提交于 2019-12-01 20:22:01
I've been running into some weird problems when using this code: positions<-c(58256) occurrencies<-c(30) frequency<-c(11/5531777) length<-c(4) prob<-c(0) for(i in 0:(occurrencies-1)) { pow<-frequency^i pow1<-(1-frequency)^(positions-i) bin<-choose(positions, i) prob<<-prob+(bin*pow*pow1) } Each iteration of this for loop should calculate the binomial probability that, i number of occurrences of the event occur given the frequency. Each iteration also sums up the result. This should result in the prob variable never exceeding 1, but after 7 or so for loop iterations, everything goes to hell and

Calculate Nth multiset combination (with repetition) based only on index

断了今生、忘了曾经 提交于 2019-12-01 10:59:09
How can i calculate the Nth combo based only on it's index. There should be (n+k-1)!/(k!(n-1)!) combinations with repetitions. with n=2, k=5 you get: 0|{0,0,0,0,0} 1|{0,0,0,0,1} 2|{0,0,0,1,1} 3|{0,0,1,1,1} 4|{0,1,1,1,1} 5|{1,1,1,1,1} So black_magic_function(3) should produce {0,0,1,1,1}. This will be going into a GPU shader, so i want each work-group/thread to be able to figure out their subset of permutations without having to store the sequence globally. with n=3, k=5 you get: i=0, {0,0,0,0,0} i=1, {0,0,0,0,1} i=2, {0,0,0,0,2} i=3, {0,0,0,1,1} i=4, {0,0,0,1,2} i=5, {0,0,0,2,2} i=6, {0,0,1,1

How to efficiently calculate a row in pascal's triangle?

隐身守侯 提交于 2019-11-28 03:56:44
I'm interested in finding the nth row of pascal triangle (not a specific element but the whole row itself). What would be the most efficient way to do it? I thought about the conventional way to construct the triangle by summing up the corresponding elements in the row above which would take: 1 + 2 + .. + n = O(n^2) Another way could be using the combination formula of a specific element: c(n, k) = n! / (k!(n-k)!) for each element in the row which I guess would take more time the the former method depending on the way to calculate the combination. Any ideas? >>> def pascal(n): ... line = [1] .

Binomial coefficient

a 夏天 提交于 2019-11-27 07:55:43
问题 'Simple' question, what is the fastest way to calculate the binomial coefficient? - Some threaded algorithm? I'm looking for hints :) - not implementations :) 回答1: According to the equation below (from wikipedia) the fastest way would be to split the range i=1,k to the number of threads, give each thread one range segment, and each thread updates the final result in a lock. "Academic way" would be to split the range into tasks, each task being to calculate (n - k + i)/i, and then no matter

Which is better way to calculate nCr

爱⌒轻易说出口 提交于 2019-11-27 06:49:30
Approach 1: C(n,r) = n!/(n-r)!r! Approach 2: In the book Combinatorial Algorithms by wilf , i have found this: C(n,r) can be written as C(n-1,r) + C(n-1,r-1) . e.g. C(7,4) = C(6,4) + C(6,3) = C(5,4) + C(5,3) + C(5,3) + C(5,2) . . . . . . . . After solving = C(4,4) + C(4,1) + 3*C(3,3) + 3*C(3,1) + 6*C(2,1) + 6*C(2,2) As you can see, the final solution doesn't need any multiplication. In every form C(n,r), either n==r or r==1. Here is the sample code i have implemented: int foo(int n,int r) { if(n==r) return 1; if(r==1) return n; return foo(n-1,r) + foo(n-1,r-1); } See output here. In the

How to efficiently calculate a row in pascal's triangle?

喜你入骨 提交于 2019-11-27 05:14:57
问题 I'm interested in finding the nth row of pascal triangle (not a specific element but the whole row itself). What would be the most efficient way to do it? I thought about the conventional way to construct the triangle by summing up the corresponding elements in the row above which would take: 1 + 2 + .. + n = O(n^2) Another way could be using the combination formula of a specific element: c(n, k) = n! / (k!(n-k)!) for each element in the row which I guess would take more time the the former

Binomial coefficient modulo 142857

流过昼夜 提交于 2019-11-26 23:10:15
问题 How to calculate binomial coefficient modulo 142857 for large n and r . Is there anything special about the 142857? If the question is modulo p where p is prime then we can use Lucas theorem but what should be done for 142857. 回答1: The algorithm is: factorise the base into prime powers; 142857 = 3^3×11×13×37 compute the result modulo each prime power combine the results using the Chinese Remainder Theorem. To compute (n above k) mod p^q : Source: http://www.dms.umontreal.ca/~andrew/PDF