discrete-mathematics

Find non-common elements in lists

ぐ巨炮叔叔 提交于 2019-11-26 20:14:11
问题 I'm trying to write a piece of code that can automatically factor an expression. For example, if I have two lists [1,2,3,4] and [2,3,5], the code should be able to find the common elements in the two lists, [2,3], and combine the rest of the elements together in a new list, being [1,4,5]. From this post: How to find list intersection? I see that the common elements can be found by set([1,2,3,4]&set([2,3,5]). Is there an easy way to retrieve non-common elements from each list, in my example

How do you calculate log base 2 in Java for integers?

荒凉一梦 提交于 2019-11-26 18:44:29
问题 I use the following function to calculate log base 2 for integers: public static int log2(int n){ if(n <= 0) throw new IllegalArgumentException(); return 31 - Integer.numberOfLeadingZeros(n); } Does it have optimal performance? Does someone know ready J2SE API function for that purpose? UPD1 Surprisingly for me, float point arithmetics appears to be faster than integer arithmetics. UPD2 Due to comments I will conduct more detailed investigation. UPD3 My integer arithmetic function is 10 times