# Python: What is the rationale for gcd(x, y) in Fractions returning a negative value when y is negative?

+1 vote
295 views

For example gcd(3, -7) returns -1, which means that a co-prime test that would work in many other languages 'if gcd(x, y) == 1' will fail in Python for negative y.

And, of course, since -|x| is less than |x|, returning -|x| rather than |x| is not returning the greatest common divisor of x and y when y is negative.

posted Sep 23, 2014
Looking for an answer?  Promote on:

Similar Questions

I have a list of a list of integers. The lists are long so i cant really show an actual example of on of the lists, but I know that they contain only the integers 1,2,3,4.

``````so for example. s2 = [[1,2,2,3,2,1,4,4],[2,4,3,2,3,1]]
``````

I am calculating the product, sum, max, min.... of each list in s2 but I get negative or 0 for the product for a lot of the lists. (I am doing this in ipython)

``````for x in s2: Â  Â  print(len = , len(x), sum = , sum(x), prod = , prod(x), max = , max(x), min = , min(x))
...
(len = , 100, sum = , 247, prod = , 0, max = , 4, min = , 1) (len = , 100, sum = , 230, prod = , -4611686018427387904, max = , 4, min = , 1) (len = , 100, sum = , 261, prod = , 0, max = , 4, min = , 1)
.....
(prod =, 0, max =, 4, min =, 1) (prod =, 1729382256910270464, max =, 4, min =, 1) (prod =, 0, max =, 4, min =, 1)
....
``````

Whats going on?

+1 vote

For example:

``````a=[-15,-30,-10,1,3,5]
``````

I want to find a negative and a positive minimum.

``````example: negative
print(min(a)) = -30

positive
print(min(a)) = 1
``````
+1 vote

I have C-Python API like below. It works fine, but the problem is while invoking this method from python script say

``````#cat script.py

offset=0
size=4
write_object(offset,size)
``````

This calls write_this_c() C api and returns quickly to next printf statement. But the return call (Py_RETURN_NONE) takes something like 4-6 seconds.

``````static
PyMethodDef xyz_methods[] = {
{"write_object", write_object, METH_VARARGS,"write some stuff "},
{NULL, NULL, 0, NULL}
};
....

static PyObject *
write_object(PyObject *self, PyObject *args)
{
int offset, size;
if (!PyArg_ParseTuple(args,"ii", &offset,

printf("before call");
write_this_c(offset, size);
printf("after call");
Py_RETURN_NONE; ##delay happens here
}
``````

How to avoid this delay time?

+1 vote

I want to write my own Screenshot Taking program in Python for Mac OS X.

Example : Whenever Command + Shift + 3 is pressed ==> whatever is there on the screen, should be grabbed / captured, and should be stored on my local with my own desired path, and name should automatically given as SnapshotX.. as in Snapshot1, Snapshot2, etc...

The same goes with Command + Shift + 4 ==> the only difference is that, it allows you to select a particular area to be captured / grabbed.

Command + Shift + 5 ==> This one is a little bit tricky which I am looking for. This shortcut doesn't exist. I want to create one of my own, where it would grab the entire webpage's screenshot which I am currently working on, and store the name as the webpage's link.