Ticket #10025: trac10025-correct_sloane_sequences.patch

File trac10025-correct_sloane_sequences.patch, 6.8 KB (added by ylchapuy, 9 years ago)
  • sage/combinat/sloane_functions.py

    # HG changeset patch
    # User Yann Laigle-Chapuy <yannlaiglechapuy@gmail.com>
    # Date 1285667923 -7200
    # Node ID b6576fd05c394b4332a154a47c01fe6a1d80dce5
    # Parent  6df06ea9d20eee8ed8264ea8e30111424d3fe175
    #10025 correct differences with Sloane's OEIS
    
    diff -r 6df06ea9d20e -r b6576fd05c39 sage/combinat/sloane_functions.py
    a b  
    4343
    4444TESTS::
    4545
    46     sage: a = sloane.A000001;
     46    sage: a = sloane.A000001
    4747    sage: a == loads(dumps(a))
    4848    True
    4949
     50    We agree with the online database::
     51
     52    sage: for t in sloane.trait_names(): # long time optional, requires internet
     53    ...       print t
     54    ...       online_list = sloane_sequence(ZZ(t[1:].lstrip('0')))[2]
     55    ...       L = max(2, len(online_list) // 2)
     56    ...       sage_list = sloane.__getattribute__(t).list(L)
     57    ...       if online_list[:L] != sage_list:
     58    ...           print t, 'seems wrong'
     59
     60
    5061AUTHORS:
    5162
    5263- William Stein: framework
     
    14861497        INPUT:
    14871498       
    14881499       
    1489         -  ``n`` - positive integer
     1500        -  ``n`` - integer
    14901501       
    14911502       
    14921503        OUTPUT:
     
    15001511            sage: a = sloane.A000272;a
    15011512            Number of labeled rooted trees with n nodes: n^(n-2).
    15021513            sage: a(0)
    1503             Traceback (most recent call last):
    1504             ...
    1505             ValueError: input n (=0) must be a positive integer
     1514            1
    15061515            sage: a(1)
    15071516            1
    15081517            sage: a(2)
    15091518            1
    15101519            sage: a(10)
    15111520            100000000
    1512             sage: a.list(11)
    1513             [1, 1, 3, 16, 125, 1296, 16807, 262144, 4782969, 100000000, 2357947691]
    1514        
    1515         AUTHORS:
    1516 
    1517         - Jaap Spies (2007-01-26)
    1518         """
    1519         SloaneSequence.__init__(self, offset=1)
     1521            sage: a.list(12)
     1522            [1, 1, 1, 3, 16, 125, 1296, 16807, 262144, 4782969, 100000000, 2357947691]
     1523       
     1524        AUTHORS:
     1525
     1526        - Jaap Spies (2007-01-26)
     1527        """
     1528        SloaneSequence.__init__(self, offset=0)
    15201529
    15211530    def _repr_(self):
    15221531        """
     
    15341543            sage: [sloane.A000272._eval(n) for n in range(1,11)]
    15351544            [1, 1, 3, 16, 125, 1296, 16807, 262144, 4782969, 100000000]
    15361545        """
     1546        if n == 0:
     1547            return 1
    15371548        return Integer(ZZ(n)**(ZZ(n)-2))
    15381549
    15391550
     
    89899000            [5, 21, 76, 246, 738, 2108, 5794, 15458, 40296, 103129]
    89909001        """
    89919002        return perm_mh(n, 4)
    8992    
    8993    
     9003
     9004class A109814(SloaneSequence):
     9005    def __init__(self):
     9006        r"""
     9007        The `n`th term of the sequence `a(n)` is the
     9008        largest `k` such that `n` can be written as sum of
     9009        `k` consecutive integers.
     9010
     9011        `n` is the sum of at most `a(n)` consecutive
     9012        positive integers. Suppose `n` is to be written as sum of
     9013        `k` consecutive integers starting with `m`, then
     9014        `2n = k(2m + k - 1)`. Only one of the factors is odd. For
     9015        each odd divisor `d` of `n` there is a unique
     9016        corresponding `k = min(d,2n/d)`. `a(n)` is the
     9017        largest among those `k` . See:
     9018        http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf
     9019
     9020        INPUT:
     9021
     9022
     9023        -  ``n`` - non negative integer
     9024
     9025
     9026        OUTPUT:
     9027
     9028
     9029        -  ``integer`` - function value
     9030
     9031
     9032        EXAMPLES::
     9033
     9034            sage: a = sloane.A109814; a
     9035            a(n) is the largest k such that n can be written as sum of k consecutive positive integers.
     9036            sage: a(0)
     9037            Traceback (most recent call last):
     9038            ...
     9039            ValueError: input n (=0) must be a positive integer
     9040            sage: a(2)
     9041            1
     9042            sage: a.list(9)
     9043            [1, 1, 2, 1, 2, 3, 2, 1, 3]
     9044
     9045        AUTHORS:
     9046
     9047        - Jaap Spies (2007-01-13)
     9048        """
     9049        SloaneSequence.__init__(self, offset=1)
     9050
     9051    def _repr_(self):
     9052        """
     9053        EXAMPLES::
     9054
     9055            sage: sloane.A109814._repr_()
     9056            'a(n) is the largest k such that n can be written as sum of k consecutive positive integers.'
     9057        """
     9058        return "a(n) is the largest k such that n can be written as sum of k consecutive positive integers."
     9059
     9060    def _eval(self, n):
     9061        """
     9062        EXAMPLES::
     9063
     9064            sage: [sloane.A109814._eval(n) for n in range(1, 10)]
     9065            [1, 1, 2, 1, 2, 3, 2, 1, 3]
     9066        """
     9067        if n == 1:
     9068            return 1
     9069        m = 0
     9070        for d in [i for i in arith.divisors(n) if i%2]: # d is odd divisor
     9071            k = min(d, 2*n/d)
     9072            if k > m:
     9073                m = k
     9074        return ZZ(m)
     9075
    89949076class A111774(SloaneSequence):
    89959077    def __init__(self):
    89969078        r"""
     
    92469328        else:
    92479329            return k-2
    92489330
    9249 class A111776(SloaneSequence):
    9250     def __init__(self):
    9251         r"""
    9252         The `n`th term of the sequence `a(n)` is the
    9253         largest `k` such that `n` can be written as sum of
    9254         `k` consecutive integers.
    9255        
    9256         `n` is the sum of at most `a(n)` consecutive
    9257         positive integers. Suppose `n` is to be written as sum of
    9258         `k` consecutive integers starting with `m`, then
    9259         `2n = k(2m + k - 1)`. Only one of the factors is odd. For
    9260         each odd divisor `d` of `n` there is a unique
    9261         corresponding `k = min(d,2n/d)`. `a(n)` is the
    9262         largest among those `k` . See:
    9263         http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf
    9264        
    9265         INPUT:
    9266        
    9267        
    9268         -  ``n`` - non negative integer
    9269        
    9270        
    9271         OUTPUT:
    9272        
    9273        
    9274         -  ``integer`` - function value
    9275        
    9276        
    9277         EXAMPLES::
    9278        
    9279             sage: a = sloane.A111776; a
    9280             a(n) is the largest k such that n can be written as sum of k consecutive integers.
    9281             sage: a(0)
    9282             1
    9283             sage: a(2)
    9284             1
    9285             sage: a.list(9)
    9286             [1, 1, 1, 2, 1, 2, 3, 2, 1]
    9287        
    9288         AUTHORS:
    9289 
    9290         - Jaap Spies (2007-01-13)
    9291         """
    9292         SloaneSequence.__init__(self, offset=0)
    9293 
    9294     def _repr_(self):
    9295         """
    9296         EXAMPLES::
    9297 
    9298             sage: sloane.A111776._repr_()
    9299             'a(n) is the largest k such that n can be written as sum of k consecutive integers.'
    9300         """
    9301         return "a(n) is the largest k such that n can be written as sum of k consecutive integers."
    9302 
    9303     def _eval(self, n):
    9304         """
    9305         EXAMPLES::
    9306        
    9307             sage: [sloane.A111776._eval(n) for n in range(10)]
    9308             [1, 1, 1, 2, 1, 2, 3, 2, 1, 3]
    9309         """
    9310         if n == 1 or n == 0:
    9311             return 1
    9312         m = 0
    9313         for d in [i for i in arith.divisors(n) if i%2]: # d is odd divisor
    9314             k = min(d, 2*n/d)
    9315             if k > m:
    9316                 m = k
    9317         return ZZ(m)
    9318 
    93199331class A111787(SloaneSequence):
    93209332    def __init__(self):
    93219333        r"""