gcd

"Approximate" greatest common divisor

Suppose you have a list of floating point numbers that are approximately multiples of a common quantity, for example 2.468, 3.700, 6.1699 which are approximately all multiples of 1.234. How would you characterize this "approximate gcd", and how would you proceed to compute or estimate it? Strictly related to my answer to this questio...

What is the fastest way to check if two given numbers are coprime?

One way is to calculate their gcd and check if it is 1. Is there some faster way? ...

How can I optimize my C / x86 code?

int lcm_old(int a, int b) { int n; for(n=1;;n++) if(n%a == 0 && n%b == 0) return n; } int lcm(int a,int b) { int n = 0; __asm { lstart: inc n; mov eax, n; mov edx, 0; idiv a; mov eax, 0; cmp eax, edx; jne lstart; mov eax, n; mo...

GCD function in matlab

hi, i am looking for a way to implement the "gcd" function used in matlab in another language but i really cant understand the way it functions. it says in http://www.mathworks.com/access/helpdesk/help/techdoc/ref/gcd.html that: "[G,C,D] = gcd(A,B) returns both the greatest common divisor array G, and the arrays C and D, which satisfy ...

What algorithm does Python employ in fractions.gcd()?

I'm using the fractions module in Python v3.1 to compute the greatest common divisor. I would like to know what algorithm is used. I'm guessing the Euclidean method, but would like to be sure. The docs (http://docs.python.org/py3k/library/fractions.html?highlight=fractions.gcd#fractions.gcd) don't help. Can anybody clue me in? ...

GCD iOS 4 questions

Hi all, I've looked at some of the presentations form WWDC 2010 and also read most of the documents on blocks and concurrency and have a couple of questions regarding using blocks with serial queues in Grand Central Dispatch. I have an iOS 4 project that has a scrollview and a dictionary of image information - urls to the images and so ...

GCD blocks not updating NSCollectionView

I have a Cocoa app that listens for notification and posts updates to an NSMutableArray monitored by a NSCollectionView. The notifications arrive in large volumes so I was thinking to use a different queue to process them and update the array accordingly. Right now I am using addObserverForName:object:queue:usingBlock to register for no...

Why does Apple recommend using a runloop over GCD for fetching multiple images?

Is it a good idea to load images (1 block each) through Grand Central Dispatch in iOS 4.0? (for use in a UITableView) Why is a runloop preferred by Apple, as illustrated in the WWDC video sessions 207 and 208? ...

Best practice to send a lot of data in background on iOS4 device?

I have an app that needs to send date (using POST) to a server. This function has to be on one of the NavigationController sub-controllers and user should be able to navigate away from this controller and/of close the app (only iPhone4/iOS4 will be supported). Should I use threads/NSOperations or/and send data using existing asynchronous...

What is the difference between GCD Dispatch Sources and select() ?

I've been writing some code that replaces some existing: while(runEventLoop){ if(select(openSockets, readFDS, writeFDS, errFDS, timeout) > 0){ // check file descriptors for activity and dispatch events based on same } } socket reading code. I'd like to change this to use a GCD queue, so that I can pop events on to the queue...

Java: get greatest common divisor

I have seen that such a function exists for BigInteger, i.e. BigInteger#gcd. Are there other functions in Java which also works for other types (int, long or Integer)? It seems this would make sense as java.lang.Math.gcd (with all kinds of overloads) but it is not there. Is it somewhere else? (Don't confuse this question with "how do ...