Changeset 3168:7209bfa43e10


Ignore:
Timestamp:
02/21/07 11:44:54 (6 years ago)
Author:
'Martin Albrecht <malb@…
Branch:
default
Message:

new routine for iterating over rational numbers by Nils Bruin

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/rings/rational_field.py

    r2802 r3168  
    176176 
    177177    def __iter__(self): 
    178         yield self(0) 
    179         yield self(1) 
    180         yield self(-1) 
    181         from integer_ring import IntegerRing 
    182         for n in IntegerRing(): 
    183             m = abs(n) 
    184             for d in abs(n).coprime_integers(m): 
    185                 yield n/d 
    186                 yield d/n 
     178        r""" 
     179        Creates an iterator that generates the rational numbers without 
     180        repetition. It uses the sequence defined by $a_0=0$ and 
     181        $a_{n+1}=\frac{1}{2\lfloor a_n\rfloor+1-a_n}$ and generates the 
     182        sequence $$a_0,a_1,-a_1,a_2,-a_2,\ldots$$ 
     183 
     184        EXAMPLES: 
     185 
     186        This example creates a list consisting of the first 10 terms 
     187        generated by this function. 
     188 
     189            sage: import itertools 
     190            sage: [a for a in itertools.islice(Rationals(),10)] 
     191            [0, 1, -1, 1/2, -1/2, 2, -2, 1/3, -1/3, 3/2] 
     192 
     193        NOTES: 
     194            A proof of the correctness of this formula is attributed to 
     195            Sam Vandervelde and Don Zagier [A002487], but a better 
     196            reference for the origin of this formula would be welcome. 
     197 
     198            REFERENCES: 
     199                 [A002487] Sloane's OLEIS, 
     200                 http://www.research.att.com/~njas/sequences/A002487 
     201 
     202        AUTHORS: 
     203            - Nils Bruin (2007-02-20) 
     204        """ 
     205 
     206        from sage.rings.arith import floor 
     207 
     208        n=self(0) 
     209        yield n 
     210        while True: 
     211          n=1/(2*floor(n)+1-n) 
     212          yield n 
     213          yield -n 
    187214 
    188215    def complex_embedding(self, prec=53): 
Note: See TracChangeset for help on using the changeset viewer.