Changeset 2402:5aeb36277f36


Ignore:
Timestamp:
01/13/07 17:21:38 (6 years ago)
Author:
Jaap Spies <jaapspies@…>
Branch:
default
Parents:
2401:8f9d1298c7d1 (diff), 2384:4ead03d6f252 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

iwhat??

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sage/databases/sloane_functions.py

    r2401 r2402  
    156156class A000010(SloaneSequence): 
    157157    r""" 
    158     A000010 is Euler's totient function. 
     158    The integer sequence A000010 is Euler's totient function. 
    159159 
    160160    Number of positive integers $i < n$ that are relative prime to $n$. 
     
    205205class A000045(SloaneSequence): 
    206206    r""" 
    207     returns Fibonacci number with index $n \le 1001$, offset 0,4 
    208  
    209     S. Plouffe, Project Gutenberg, The First 1001 Fibonacci Numbers 
    210     http://ibiblio.org/pub/docs/books/gutenberg/etext01/fbncc10.txt 
    211  
     207    Sequence of Fibonacci numbers, offset 0,4. 
     208 
     209    REFERENCES: S. Plouffe, Project Gutenberg, 
     210    The First 1001 Fibonacci Numbers, 
     211    \url{http://ibiblio.org/pub/docs/books/gutenberg/etext01/fbncc10.txt} 
    212212    We have one more. Our first Fibonacci number is 0. 
    213  
    214  
    215  
    216213 
    217214    INPUT: 
     
    221218        integer -- function value 
    222219 
    223  
    224   
    225220    EXAMPLES: 
    226221        sage: a = sloane.A000045; a 
    227222        Fibonacci number with index n >= 0 
    228  
    229223        sage: a(1) 
    230224        1 
     
    241235        -- Jaap Spies (2007-01-13) 
    242236    """ 
     237    def __init__(self): 
     238        self._b = [] 
     239         
    243240    def _repr_(self): 
    244241        return "Fibonacci number with index n >= 0" 
     
    247244        m = Integer(n) 
    248245        if m < 0: 
    249             raise ValueError, "input n (=%s) must be a non negative integer"%n 
     246            raise ValueError, "input n (=%s) must be a non-negative integer"%n 
    250247        return self._eval(m) 
    251248 
    252  
    253     def fib(): 
     249    def _precompute(self, how_many=500): 
     250        try: 
     251            f = self._f 
     252        except AttributeError: 
     253            self._f = self.fib() 
     254            f = self._f 
     255        self._b += [f.next() for i in range(how_many)] 
     256 
     257    def fib(self): 
    254258        """ 
    255             generates an "infinity" of Fibonacci numbers, 
    256             starting with 0 
     259        Returns a generator over all Fibanacci numbers, starting with 0. 
    257260        """ 
    258         x, y = 0, 1 
     261        x, y = Integer(0), Integer(1) 
    259262        yield x 
    260         while 1: 
     263        while True: 
    261264            x, y = y, x+y 
    262265            yield x 
     
    264267    offset = 0 
    265268 
    266     f = fib() 
    267     b = [f.next() for i in range(0,1002)] 
    268  
    269     def _eval(self, n): 
    270         if n < 1002: 
    271             return self.b[n] 
    272  
    273     def list(self, n): 
    274         if n < 1002: 
    275             return self.b[:n] 
    276  
     269    def _eval(self, n): 
     270        if len(self._b) < n: 
     271            self._precompute(n - len(self._b) + 1) 
     272        return self._b[n] 
     273 
     274    def list(self, n): 
     275        self._eval(n)   # force computation 
     276        return self._b[:n] 
     277     
    277278 
    278279class A000203(SloaneSequence): 
    279280    r""" 
    280     This function returns $sigma(n)$ 
    281  
    282     $\sigma(n)$ is the sum of the divisors of $n$. Also called $\sigma_1(n)$. 
     281    The sequence $\sigma(n)$, where $\sigma(n)$ is the sum of the 
     282    divisors of $n$.   Also called $\sigma_1(n)$. 
    283283  
    284284    INPUT: 
     
    306306        TypeError: Unable to coerce rational (=1/3) to an Integer. 
    307307 
    308         AUTHOR: 
    309             - Jaap Spies (2007-01-13) 
     308    AUTHOR: 
     309        -- Jaap Spies (2007-01-13) 
    310310    """ 
    311311 
     
    318318        return sum(arith.divisors(n)) 
    319319  
    320  
    321320    def list(self, n): 
    322321        return [self(i) for i in range(1,n+1)] 
    323322 
    324323 
    325 def is_number_of_the_third_kind(n): 
    326     r"""" 
    327     This function returns True iff $n$ is a number of the third kind. 
    328  
    329     A number of the third kind can be written as a sum of at least 
    330     three consecutive positive integers. 
    331     Odd primes can only be written as a sum of two consecutive integers. 
    332     Powers of 2 do not have a representation as a sum of $k$ consecutive 
    333     integers (other than the trivial $n = n$ for $k = 1$). 
    334  
    335     See: http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf 
    336  
    337     INPUT: 
    338         n -- positive integer 
    339  
    340     OUTPUT: 
    341         True -- if n is not prime and not a power of 2 
    342         False -- 
    343  
    344     EXAMPLES: 
    345         sage: is_number_of_the_third_kind(6) 
    346         True 
    347  
    348         sage: is_number_of_the_third_kind(100) 
    349         True 
    350  
    351         sage: is_number_of_the_third_kind(16) 
    352         False 
    353  
    354         sage: is_number_of_the_third_kind(97) 
    355         False 
    356  
    357     AUTHOR: 
    358         - Jaap Spies (2006-12-09) 
    359     """ 
    360  
    361     if (not arith.is_prime(n)) and (not is_power_of_two(n)): 
    362         return True 
    363     else: 
    364         return False 
    365  
    366324 
    367325def is_power_of_two(n): 
    368     r"""" 
    369     This function returns True iff $n$ is a power of 2 
     326    r""" 
     327    This function returns True if and only if $n$ is a power of 2 
    370328 
    371329    INPUT: 
     
    377335 
    378336    EXAMPLES: 
     337        sage: from sage.databases.sloane_functions import is_power_of_two 
     338         
    379339        sage: is_power_of_two(1024) 
    380340        True 
     
    393353 
    394354    AUTHOR: 
    395         - Jaap Spies (2006-12-09) 
    396  
    397     """ 
    398 # modification of is2pow(n) from the Programming Guide 
     355        -- Jaap Spies (2006-12-09) 
     356 
     357    """ 
     358    # modification of is2pow(n) from the Programming Guide 
    399359    while n > 0 and n%2 == 0:  
    400360        n = n >> 1 
     
    403363class A111774(SloaneSequence): 
    404364    r""" 
    405     Numbers that can be written as a sum of at least three consecutive positive integers. 
    406  
    407   
    408     Numbers of the third kind can be written as a sum of at least 
    409     three consecutive positive integers. 
     365    Sequence of numbers of the third kind, i.e., numbers that can be 
     366    written as a sum of at least three consecutive positive integers. 
     367 
    410368    Odd primes can only be written as a sum of two consecutive integers. 
    411369    Powers of 2 do not have a representation as a sum of $k$ consecutive 
     
    439397        TypeError: Unable to coerce rational (=1/3) to an Integer. 
    440398 
     399    AUTHOR: 
     400        -- Jaap Spies (2007-01-13) 
     401    """ 
     402    def _repr_(self): 
     403        return "Numbers that can be written as a sum of at least three consecutive positive integers." 
     404 
     405    offset = 1 
     406 
     407    def _precompute(self, how_many=150): 
     408        try: 
     409            self._b 
     410            n = self._n 
     411        except AttributeError: 
     412            self._b = [] 
     413            n = 1 
     414            self._n = n 
     415        self._b += [i for i in range(n, n+how_many) if self.is_number_of_the_third_kind(i)] 
     416 
     417    def _eval(self, n): 
     418        try: 
     419            return self._b[n-1] 
     420        except (AttributeError, IndexError): 
     421            self._precompute() 
     422            # try again 
     423            return self._eval(n) 
     424 
     425    def list(self, n): 
     426        try: 
     427            if len(self._b) < n: 
     428                raise IndexError 
     429            else: 
     430                return self._b[:n] 
     431        except (AttributeError, IndexError): 
     432            self._precompute() 
     433            # try again 
     434            return self.list(n) 
     435 
     436    def is_number_of_the_third_kind(self, n): 
     437        r""" 
     438        This function returns True if and only if $n$ is a number of the third kind. 
     439 
     440        A number is of the third kind if it can be written as a sum of at 
     441        least three consecutive positive integers.  Odd primes can only be 
     442        written as a sum of two consecutive integers.  Powers of 2 do not 
     443        have a representation as a sum of $k$ consecutive integers (other 
     444        than the trivial $n = n$ for $k = 1$). 
     445 
     446        See: \url{http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf} 
     447 
     448        INPUT: 
     449            n -- positive integer 
     450 
     451        OUTPUT: 
     452            True -- if n is not prime and not a power of 2 
     453            False -- 
     454 
     455        EXAMPLES: 
     456            sage: a = sloane.A111774 
     457            sage: a.is_number_of_the_third_kind(6) 
     458            True 
     459            sage: a.is_number_of_the_third_kind(100) 
     460            True 
     461            sage: a.is_number_of_the_third_kind(16) 
     462            False 
     463            sage: a.is_number_of_the_third_kind(97) 
     464            False 
     465 
    441466        AUTHOR: 
    442             - Jaap Spies (2007-01-13) 
    443     """ 
    444     def _repr_(self): 
    445         return "Numbers that can be written as a sum of at least three consecutive positive integers." 
    446  
    447     offset = 1 
    448  
    449     b = [i for i in range(1, 150) if is_number_of_the_third_kind(i)] 
    450  
    451     def _eval(self, n): 
    452         return self.b[n-1] 
    453   
    454  
    455     def list(self, n): 
    456        return self.b[:n] 
     467            -- Jaap Spies (2006-12-09) 
     468        """ 
     469        if (not arith.is_prime(n)) and (not is_power_of_two(n)): 
     470            return True 
     471        else: 
     472            return False 
     473 
    457474 
    458475class A111775(SloaneSequence): 
     
    468485    there is a unique corresponding $k$, $k=1$ and $k=2$ must be excluded. 
    469486 
    470     See: http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf 
     487    See: \url{http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf} 
    471488 
    472489    INPUT: 
     
    479496        sage: a = sloane.A111775; a 
    480497        Number of ways n can be written as a sum of at least three consecutive integers. 
     498         
    481499        sage: a(1) 
    482500        0 
    483501        sage: a(0) 
    484502        0 
     503 
     504    We have a(15)=2 because 15 = 4+5+6 and 15 = 1+2+3+4+5. The number of odd divisors of 15 is 4. 
     505        sage: a(15) 
     506        2 
     507         
    485508        sage: a(100) 
    486509        2 
     
    496519        TypeError: Unable to coerce rational (=1/3) to an Integer. 
    497520 
    498         AUTHOR: 
    499             - Jaap Spies (2006-12-09) 
    500     """ 
    501  
     521    AUTHOR: 
     522        -- Jaap Spies (2006-12-09) 
     523    """ 
    502524    def _repr_(self): 
    503525        return "Number of ways n can be written as a sum of at least three consecutive integers." 
     
    521543            return k-2 
    522544 
    523   
    524  
    525545    def list(self, n): 
    526546       return [self(i) for i in range(0,n)] 
     
    529549class A111776(SloaneSequence): 
    530550    r""" 
    531     $a(n)$ is the largest $k$ such that $n$ can be written as sum of $k$ consecutive integers. 
     551    The $n$th term of the sequence $a(n)$ is the largest $k$ such that 
     552    $n$ can be written as sum of $k$ consecutive integers. 
    532553 
    533554    $n$ is the sum of at most $a(n)$ consecutive positive integers. 
     
    537558    $k = min(d,2n/d)$. $a(n)$ is the largest among those $k$ 
    538559. 
    539     See: http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf 
     560    See: \url{http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf} 
    540561 
    541562    INPUT: 
     
    545566        integer -- function value 
    546567 
    547  
    548     EXAMPLES: 
    549  
    550         AUTHOR: 
    551             - Jaap Spies (2007-01-13) 
    552     """ 
    553  
    554  
     568    AUTHOR: 
     569        -- Jaap Spies (2007-01-13) 
     570    """ 
    555571    def _repr_(self): 
    556572        return "a(n) is the largest k such that n can be written as sum of k consecutive integers." 
     
    563579            raise ValueError, "input n (=%s) must be a non negative integer"%n 
    564580        return self._eval(m) 
    565  
    566581 
    567582    def _eval(self, n): 
     
    575590        return m 
    576591 
    577  
    578592    def list(self, n): 
    579593       return [self(i) for i in range(0,n)] 
     
    581595 
    582596 
    583  
    584  
    585  
    586  
    587  
    588597############################################################# 
    589 # III. Create the sloane object, off which all the sequence 
    590 #      objects hang. 
     598# III. Create the Sloane object, off which all the sequence 
     599#      objects are members. 
    591600############################################################# 
    592601 
Note: See TracChangeset for help on using the changeset viewer.