I'm not sure why you are getting different results from sagenb.com and
from the "shell", but I would guess that you must be using two different
versions of Sage. Either way...

The problem is that when you divide two integers, the result is always a
rational number, whether or not that rational number happens to be an
integer. Then, when you ask for the divisors of a rational number (Not
that I'm even sure why this should make sense), you get back a list of
rational numbers. For rational numbers, Sage decides to always return a
GCD of 1:

sage: a.gcd
<built-in method gcd of sage.rings.rational.Rational object at 0xb7c49cdc>
sage: a.gcd?
Type:           builtin_function_or_method
Base Class:     <type 'builtin_function_or_method'>
String Form:    <built-in method gcd of sage.rings.rational.Rational object at 
0xb7c49cdc>
Namespace:      Interactive
Docstring:
    
            Return a gcd of the rational numbers self and other.
    
            If self = other = 0, this is by convention 0.  In all other
            cases it can (mathematically) be any nonzero rational number,
            but for simplicity we choose to always return 1.
            
            EXAMPLES::
    
                sage: gcd(1/3, 2/1)
                1
                sage: gcd(1/1, 0/1)
                1
                sage: gcd(0/1, 0/1)
                0

In particular, we have:

sage: gcd(3/1, 3)
1
sage: gcd(3, 3)
3

You can get the behavior that you want by forcing quot to be an Integer,
as in:

quot = Integer(D/dhat)

I feel like Sage is doing something wrong here, but I'm not sure what
the right thing to do is. A strange example:

sage: factor(15/2)
2^-1 * 3 * 5
sage: a = divisors(15/2); a
[1, 3, 5, 15]
sage: type(a[0])
<type 'sage.rings.rational.Rational'>


On Mon, 2009-03-02 at 18:12 -0600, Martin Mereb wrote:
> I had this function in a worksheet at sagenb.com
> 
> def Z(D,t,dhat):
>     quot=D/dhat
>     sum=0
>     for l in divisors(quot):
>         a=l*dhat
>         print a, t, GCD(a,t)
>         sum=sum+GCD(a,t)
>     return sum
> 
> 
> and Z(3,3,1) returns
> 1 3 1
> 3 3 1
> 2
> 
> which means GCD(3,3)=1
> 
> I know that in order to solve the problem I had to put quot=ZZ(D/dhat)
> instead of quot=D/dhat
> (I took me a while, though).
> 
> I didn't have that problem in the Shell
> sage: def Z(D,t,dhat):
> ....:         quot=D/dhat
> ....:     sum=0
> ....:     for l in divisors(quot):
> ....:             a=l*dhat
> ....:         print a, t, GCD(a,t)
> ....:         sum=sum+GCD(a,t)
> ....:     return sum
> ....:
> sage: Z(3,3,1)
> 1 3 1
> 3 3 3
> 4
> 
> so I see a difference here.
> cheers
> 
> 
> tincho
> 
> > 
> 
> 


--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to