calculate gcd, factorial and divisors of numbers

631 views
Skip to first unread message

Addicted

unread,
Apr 10, 2011, 10:09:33 AM4/10/11
to spyder
Hi there! I´m new using Spyder and I´m trying to calculate the
greatest common divisor (gcd), the factorial and the divisor of
numbers..I´ve tried on this way :

from math import divisors
n=(divisors(712504))
print n

but then ImportError: cannot import name divisors appears
I searched on Internet but without a result.I need a help!

stefan

unread,
Apr 10, 2011, 1:06:18 PM4/10/11
to spyder
This is not strictly a spyder question but rather general python but
here goes:

the gcd can be imported from module fractions
>>> import fractions
>>> fractions.gcd(120,70)
10

factorial is provided by the math module
>>> import math
>>> math.factorial(10)
3628800

Finding all divisors is a bit more involved (NP-hard problem) and
there appears to be no readily provided function in a standard
module
(I might be wrong). Consider the highest-ranked answer in
http://stackoverflow.com/questions/171765/what-is-the-best-way-to-get-all-the-divisors-of-a-number
and and the ensuing discussion. It should be straighforward to take
what has been posted and use their function as is.

Hope this helps, Stefan
You are getting the ImportError because module math does not provide
divisors:

>>> import math
>>> dir(math)
['__doc__', '__file__', '__name__', '__package__', 'acos', 'acosh',
'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos',
'cosh', 'degrees', 'e', 'exp', 'fabs', 'factorial', 'floor', 'fmod',
'frexp', 'fsum', 'hypot', 'isinf', 'isnan', 'ldexp', 'log', 'log10',
'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan',
'tanh', 'trunc']

Alexandra Sekalska

unread,
Apr 10, 2011, 2:36:36 PM4/10/11
to spyd...@googlegroups.com
Thank you for the helpful and rapid answer.I´ve already solved the problems because of your explanations.Thanks again! 

--
You received this message because you are subscribed to the Google Groups "spyder" group.
To post to this group, send email to spyd...@googlegroups.com.
To unsubscribe from this group, send email to spyderlib+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spyderlib?hl=en.


Ariel Balter

unread,
Apr 10, 2011, 11:53:23 PM4/10/11
to spyd...@googlegroups.com
We should all remember that this is a forum for snarky comments about Spyder.  If you don't have any trivial complaints, ridiculous feature  requests, or other nonsensical comments about Spyder,  then this is not the place.   If you need help with your Finite Math homework, please post to the PyDev forum instead.
-- 
Ariel Balter
arielbalter.com
509-713-0087

"Don't believe everything you think."

David

unread,
Apr 11, 2011, 1:12:06 AM4/11/11
to spyd...@googlegroups.com
On Monday, April 11, 2011 1:53:23 PM UTC+10, abalter wrote:
We should all remember that this is a forum for snarky comments about Spyder.  If you don't have any trivial complaints, ridiculous feature  requests, or other nonsensical comments about Spyder,  then this is not the place.   If you need help with your Finite Math homework, please post to the PyDev forum instead.

While this comment is perfectly correct, it does highlight one of the problems with using python for scientific computing in comparison to matlab.  If a newbie has a matlab problem, then they can go to a matlab forum and expect to get a reasonable answer.  But if that problem occurs in python, then a beginner might need to ask their question in a python, spyder, numpy, scipy, pyqt etc. forum, and if you aren't familiar with the full tool stack and the relationship between all the parts, it could be a bit confusing (the same goes for reading through documentation).  So I would advocate being forgiving of slightly off-topic questions.

Stefan Reinsberg

unread,
Apr 11, 2011, 2:19:09 AM4/11/11
to spyd...@googlegroups.com
This is admittedly getting off-topic but my rationale is that I am
keen to get as many people using spyder and associates as possible. I
am in the process off switching my research group from IDL and Matlab
to scientific python and I want this to be a long-term viable
solution. This in no small part relies on there being a dedicated but
also large community which I am hoping to support by being friendly.
So while I appreciate the humour in Ariel's comment I agree with
David.

Ariel Balter

unread,
Apr 11, 2011, 7:23:51 AM4/11/11
to spyd...@googlegroups.com
I'm sure you realized that I was 92.5% in jest.

That is actually a very good point.  You can often get great answers on programming forums such as stackoverflow.  On the other hand, Matlab Central is one stop shopping.  Great threads on questions.  And, most importantly, programming solutions and useful bits -- with ratings and comment threads.

I think the place to host this is at with scipy.  If you propose it to them, I would back it.
--
You received this message because you are subscribed to the Google Groups "spyder" group.
To post to this group, send email to spyd...@googlegroups.com.
To unsubscribe from this group, send email to spyderlib+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spyderlib?hl=en.

Carlos Córdoba

unread,
Apr 11, 2011, 9:26:00 AM4/11/11
to spyd...@googlegroups.com
Without a smiley face anywhere it was kind of hard to realize.... (at least for me) :-)

Being this a low traffic list, I don't see a problem with people asking this questions but Ariel is right, there are better places to ask. Scipy lists could be ones and stackoverflow is another.

Just my two cents,
Carlos

El 11/04/11 06:23, Ariel Balter escribió:

Ariel Balter

unread,
Apr 11, 2011, 9:29:15 AM4/11/11
to spyd...@googlegroups.com
An emoticon would have been a good idea.  I'm prone to wry humor.  I sincerely meant no harm.

On the other hand, I think a great idea has emerged-- python central.  It would be great to have a searchable database of recipes -- particularly for scientific computing.

Pierre Raybaut

unread,
Apr 11, 2011, 3:47:09 PM4/11/11
to spyder
SciPy is already hosting this:
http://www.scipy.org/Cookbook

But there is no comparison with MATLAB Central.

-Pierre

On 11 avr, 15:29, Ariel Balter <ariel.bal...@gmail.com> wrote:
> An emoticon would have been a good idea.  I'm prone to wry humor.  I
> sincerely meant no harm.
>
> On the other hand, I think a great idea has emerged-- python central.  
> It would be great to have a searchable database of recipes --
> particularly for scientific computing.
>
> On 4/11/2011 6:26 AM, Carlos C�rdoba wrote:
>
>
>
>
>
>
>
>
>
> > Without a smiley face anywhere it was kind of hard to realize.... (at
> > least for me) :-)
>
> > Being this a low traffic list, I don't see a problem with people
> > asking this questions but Ariel is right, there are better places to
> > ask. Scipy lists could be ones and stackoverflow is another.
>
> > Just my two cents,
> > Carlos
>
> > El 11/04/11 06:23, Ariel Balter escribi�:

josef...@gmail.com

unread,
Apr 11, 2011, 3:54:48 PM4/11/11
to spyd...@googlegroups.com
On Mon, Apr 11, 2011 at 3:47 PM, Pierre Raybaut
<pierre....@gmail.com> wrote:
> SciPy is already hosting this:
> http://www.scipy.org/Cookbook
>
> But there is no comparison with MATLAB Central.

There was a long discussion on scipy-user last October on "Central
File Exchange for SciPy"

However, nobody has delivered an implementation for this.

Josef

Ariel Balter

unread,
Apr 11, 2011, 4:17:19 PM4/11/11
to spyd...@googlegroups.com
@Josef andPierre -- Thanks for finding those out. Something to get back
to down the road.

On 4/11/2011 12:54 PM, josef...@gmail.com wrote:
> On Mon, Apr 11, 2011 at 3:47 PM, Pierre Raybaut
> <pierre....@gmail.com> wrote:
>> SciPy is already hosting this:
>> http://www.scipy.org/Cookbook
>>
>> But there is no comparison with MATLAB Central.
> There was a long discussion on scipy-user last October on "Central
> File Exchange for SciPy"
>
> However, nobody has delivered an implementation for this.
>
> Josef
>
>
>> -Pierre
>>
>> On 11 avr, 15:29, Ariel Balter<ariel.bal...@gmail.com> wrote:
>>> An emoticon would have been a good idea. I'm prone to wry humor. I
>>> sincerely meant no harm.
>>>
>>> On the other hand, I think a great idea has emerged-- python central.
>>> It would be great to have a searchable database of recipes --
>>> particularly for scientific computing.
>>>

>>> On 4/11/2011 6:26 AM, Carlos C�rdoba wrote:
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>> Without a smiley face anywhere it was kind of hard to realize.... (at
>>>> least for me) :-)
>>>> Being this a low traffic list, I don't see a problem with people
>>>> asking this questions but Ariel is right, there are better places to
>>>> ask. Scipy lists could be ones and stackoverflow is another.
>>>> Just my two cents,
>>>> Carlos

>>>> El 11/04/11 06:23, Ariel Balter escribi�:

pmav99

unread,
Apr 12, 2011, 3:19:13 AM4/12/11
to spyder
There was also this one:
http://advice.mechanicalkern.com/

which transformed into this one:
http://ask.scipy.org/en/

but it is not very active...
Reply all
Reply to author
Forward
0 new messages