division

Computing greatest common denominator in python

允我心安 提交于 2019-11-30 22:19:17
If you have a list of integers in python, say L = [4,8,12,24] , how can you compute their greatest common denominator/divisor (4 in this case)? dzhelil One way to do it is: import fractions def gcd(L): return reduce(fractions.gcd, L) print gcd([4,8,12,24]) 来源: https://stackoverflow.com/questions/3640955/computing-greatest-common-denominator-in-python

Division in VB.NET

孤街浪徒 提交于 2019-11-30 17:31:40
What's the difference between / and \ for division in VB.NET? My code gives very different answers depending on which I use. I've seen both before, but I never knew the difference. There are two ways to divide numbers. The fast way and the slow way. A lot of compilers try to trick you into doing it the fast way. C# is one of them, try this: using System; class Program { static void Main(string[] args) { Console.WriteLine(1 / 2); Console.ReadLine(); } } Output: 0 Are you happy with that outcome? It is technically correct, documented behavior when the left side and the right side of the

Faster integer division when denominator is known?

♀尐吖头ヾ 提交于 2019-11-30 17:22:25
I am working on GPU device which has very high division integer latency, several hundred cycles. I am looking to optimize divisions. All divisions by denominator which is in a set { 1,3,6,10 }, however numerator is a runtime positive value, roughly 32000 or less. due to memory constraints, lookup table may not be a good option. Can you think of alternatives? I have thought of computing float point inverses, and using those to multiply numerator. Thanks PS. thank you people. bit shift hack is a really cool. to recover from roundoff, I use following C segment: // q = m/n q += (n*(j +1)-1) < m; a

How can I perform divison on a datetime.timedelta in python?

老子叫甜甜 提交于 2019-11-30 16:26:31
问题 I'd like to be able to do the following: num_intervals = (cur_date - previous_date) / interval_length or print (datetime.now() - (datetime.now() - timedelta(days=5))) / timedelta(hours=12) # won't run, would like it to print '10' but the division operation is unsupported on timedeltas. Is there a way that I can implement divison for timedeltas? Edit: Looks like this was added to Python 3.2 (thanks rincewind!): http://bugs.python.org/issue2706 回答1: Sure, just convert to a number of seconds

Signed 64 by 32 integer division

一个人想着一个人 提交于 2019-11-30 16:03:45
Assuming you have a machine instruction udive that does a special case 64 by 32 unsigned division by taking a (32bit dividend << 32) / 32bit divisor, we can do a full 64 by 32 division using the following: // assume: a / b guaranteed not to overflow a = 64bit dividend, a.h & a.l are hi & lo 32bits respectively b = 32bit divisor q1 = udive(a.h, b) // (a.h << 32) / b r1 = -(q1 * b) // remainder of the above, shortcut since a.h & 0xffffffff == 0 q2 = a.l / b // a.l / b using regular unsigned division r2 = a.l - (q2 * b) // remainder of the above q = q1 + q2 r = r1 + r2 // r < r2, r overflowed and

What algorithm should I use for high-performance large integer division?

谁说胖子不能爱 提交于 2019-11-30 13:00:26
I am encoding large integers into an array of size_t . I already have the other operations working (add, subtract, multiply); as well as division by a single digit. But I would like match the time complexity of my multiplication algorithms if possible (currently Toom-Cook). I gather there are linear time algorithms for taking various notions of multiplicative inverse of my dividend. This means I could theoretically achieve division in the same time complexity as my multiplication, because the linear-time operation is "insignificant" by comparison anyway. My question is, how do I actually do

Converting KB to MB, GB, TB dynamically

梦想与她 提交于 2019-11-30 12:13:27
public String size(int size){ String hrSize = ""; int k = size; double m = size/1024; double g = size/1048576; double t = size/1073741824; DecimalFormat dec = new DecimalFormat("0.00"); if (k>0) { hrSize = dec.format(k).concat("KB"); } if (m>0) { hrSize = dec.format(m).concat("MB"); } if (g>0) { hrSize = dec.format(g).concat("GB"); } if (t>0) { hrSize = dec.format(t).concat("TB"); } return hrSize; } This is a method that should return size in GB,MB, KB or TB. Input value is in KB. for example result for 1245 should be like 1.21MB but what I get is 1.00MB. You are performing integer division .

how to do two complement multiplication and division of integers?

放肆的年华 提交于 2019-11-30 12:07:04
问题 I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg: -1 with -7 should give 7. A 4-bit, 2's complement of -1 is : 1111 A 4-bit, 2's complement of -7 is : 1001 some step-wise way of calculating the multiplication will be helpful. No article I came across talks about division. How to approach this? 回答1:

How can I Initialize a div_t Object?

白昼怎懂夜的黑 提交于 2019-11-30 09:21:12
问题 So the order of the members returned from the div functions seems to be implementation defined. Is quot the 1 st member or is rem ? Let's say that I'm doing something like this: generate(begin(digits), end(digits), [i = div_t{ quot, 0 }]() mutable { i = div(i.quot, 10); return i.rem; }) Of course the problem here is that I don't know if I initialized i.quot or i.rem in my lambda capture. Is intializing i with div(quot, 1) the only cross platform way to do this? 回答1: EDIT: I think the VS

What algorithm should I use for high-performance large integer division?

人走茶凉 提交于 2019-11-30 07:30:44
问题 I am encoding large integers into an array of size_t . I already have the other operations working (add, subtract, multiply); as well as division by a single digit. But I would like match the time complexity of my multiplication algorithms if possible (currently Toom-Cook). I gather there are linear time algorithms for taking various notions of multiplicative inverse of my dividend. This means I could theoretically achieve division in the same time complexity as my multiplication, because the