# Changeset 2402:5aeb36277f36

Ignore:
Timestamp:
01/13/07 17:21:38 (6 years ago)
Branch:
default
Parents:
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
 r2401 class A000010(SloaneSequence): r""" A000010 is Euler's totient function. The integer sequence A000010 is Euler's totient function. Number of positive integers $i < n$ that are relative prime to $n$. class A000045(SloaneSequence): r""" returns Fibonacci number with index $n \le 1001$, offset 0,4 S. Plouffe, Project Gutenberg, The First 1001 Fibonacci Numbers http://ibiblio.org/pub/docs/books/gutenberg/etext01/fbncc10.txt Sequence of Fibonacci numbers, offset 0,4. REFERENCES: S. Plouffe, Project Gutenberg, The First 1001 Fibonacci Numbers, \url{http://ibiblio.org/pub/docs/books/gutenberg/etext01/fbncc10.txt} We have one more. Our first Fibonacci number is 0. INPUT: integer -- function value EXAMPLES: sage: a = sloane.A000045; a Fibonacci number with index n >= 0 sage: a(1) 1 -- Jaap Spies (2007-01-13) """ def __init__(self): self._b = [] def _repr_(self): return "Fibonacci number with index n >= 0" m = Integer(n) if m < 0: raise ValueError, "input n (=%s) must be a non negative integer"%n raise ValueError, "input n (=%s) must be a non-negative integer"%n return self._eval(m) def fib(): def _precompute(self, how_many=500): try: f = self._f except AttributeError: self._f = self.fib() f = self._f self._b += [f.next() for i in range(how_many)] def fib(self): """ generates an "infinity" of Fibonacci numbers, starting with 0 Returns a generator over all Fibanacci numbers, starting with 0. """ x, y = 0, 1 x, y = Integer(0), Integer(1) yield x while 1: while True: x, y = y, x+y yield x offset = 0 f = fib() b = [f.next() for i in range(0,1002)] def _eval(self, n): if n < 1002: return self.b[n] def list(self, n): if n < 1002: return self.b[:n] def _eval(self, n): if len(self._b) < n: self._precompute(n - len(self._b) + 1) return self._b[n] def list(self, n): self._eval(n)   # force computation return self._b[:n] class A000203(SloaneSequence): r""" This function returns $sigma(n)$ $\sigma(n)$ is the sum of the divisors of $n$. Also called $\sigma_1(n)$. The sequence $\sigma(n)$, where $\sigma(n)$ is the sum of the divisors of $n$.   Also called $\sigma_1(n)$. INPUT: TypeError: Unable to coerce rational (=1/3) to an Integer. AUTHOR: - Jaap Spies (2007-01-13) AUTHOR: -- Jaap Spies (2007-01-13) """ return sum(arith.divisors(n)) def list(self, n): return [self(i) for i in range(1,n+1)] def is_number_of_the_third_kind(n): r"""" This function returns True iff $n$ is a number of the third kind. A number of the third kind can be written as a sum of at least three consecutive positive integers. Odd primes can only be written as a sum of two consecutive integers. Powers of 2 do not have a representation as a sum of $k$ consecutive integers (other than the trivial $n = n$ for $k = 1$). See: http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf INPUT: n -- positive integer OUTPUT: True -- if n is not prime and not a power of 2 False -- EXAMPLES: sage: is_number_of_the_third_kind(6) True sage: is_number_of_the_third_kind(100) True sage: is_number_of_the_third_kind(16) False sage: is_number_of_the_third_kind(97) False AUTHOR: - Jaap Spies (2006-12-09) """ if (not arith.is_prime(n)) and (not is_power_of_two(n)): return True else: return False def is_power_of_two(n): r"""" This function returns True iff $n$ is a power of 2 r""" This function returns True if and only if $n$ is a power of 2 INPUT: EXAMPLES: sage: from sage.databases.sloane_functions import is_power_of_two sage: is_power_of_two(1024) True AUTHOR: - Jaap Spies (2006-12-09) """ # modification of is2pow(n) from the Programming Guide -- Jaap Spies (2006-12-09) """ # modification of is2pow(n) from the Programming Guide while n > 0 and n%2 == 0: n = n >> 1 class A111774(SloaneSequence): r""" Numbers that can be written as a sum of at least three consecutive positive integers. Numbers of the third kind can be written as a sum of at least three consecutive positive integers. Sequence of numbers of the third kind, i.e., numbers that can be written as a sum of at least three consecutive positive integers. Odd primes can only be written as a sum of two consecutive integers. Powers of 2 do not have a representation as a sum of $k$ consecutive TypeError: Unable to coerce rational (=1/3) to an Integer. AUTHOR: -- Jaap Spies (2007-01-13) """ def _repr_(self): return "Numbers that can be written as a sum of at least three consecutive positive integers." offset = 1 def _precompute(self, how_many=150): try: self._b n = self._n except AttributeError: self._b = [] n = 1 self._n = n self._b += [i for i in range(n, n+how_many) if self.is_number_of_the_third_kind(i)] def _eval(self, n): try: return self._b[n-1] except (AttributeError, IndexError): self._precompute() # try again return self._eval(n) def list(self, n): try: if len(self._b) < n: raise IndexError else: return self._b[:n] except (AttributeError, IndexError): self._precompute() # try again return self.list(n) def is_number_of_the_third_kind(self, n): r""" This function returns True if and only if $n$ is a number of the third kind. A number is of the third kind if it can be written as a sum of at least three consecutive positive integers.  Odd primes can only be written as a sum of two consecutive integers.  Powers of 2 do not have a representation as a sum of $k$ consecutive integers (other than the trivial $n = n$ for $k = 1$). See: \url{http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf} INPUT: n -- positive integer OUTPUT: True -- if n is not prime and not a power of 2 False -- EXAMPLES: sage: a = sloane.A111774 sage: a.is_number_of_the_third_kind(6) True sage: a.is_number_of_the_third_kind(100) True sage: a.is_number_of_the_third_kind(16) False sage: a.is_number_of_the_third_kind(97) False AUTHOR: - Jaap Spies (2007-01-13) """ def _repr_(self): return "Numbers that can be written as a sum of at least three consecutive positive integers." offset = 1 b = [i for i in range(1, 150) if is_number_of_the_third_kind(i)] def _eval(self, n): return self.b[n-1] def list(self, n): return self.b[:n] -- Jaap Spies (2006-12-09) """ if (not arith.is_prime(n)) and (not is_power_of_two(n)): return True else: return False class A111775(SloaneSequence): there is a unique corresponding $k$, $k=1$ and $k=2$ must be excluded. See: http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf See: \url{http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf} INPUT: sage: a = sloane.A111775; a Number of ways n can be written as a sum of at least three consecutive integers. sage: a(1) 0 sage: a(0) 0 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. sage: a(15) 2 sage: a(100) 2 TypeError: Unable to coerce rational (=1/3) to an Integer. AUTHOR: - Jaap Spies (2006-12-09) """ AUTHOR: -- Jaap Spies (2006-12-09) """ def _repr_(self): return "Number of ways n can be written as a sum of at least three consecutive integers." return k-2 def list(self, n): return [self(i) for i in range(0,n)] class A111776(SloaneSequence): r""" $a(n)$ is the largest $k$ such that $n$ can be written as sum of $k$ consecutive integers. The $n$th term of the sequence $a(n)$ is the largest $k$ such that $n$ can be written as sum of $k$ consecutive integers. $n$ is the sum of at most $a(n)$ consecutive positive integers. $k = min(d,2n/d)$. $a(n)$ is the largest among those $k$ . See: http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf See: \url{http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf} INPUT: integer -- function value EXAMPLES: AUTHOR: - Jaap Spies (2007-01-13) """ AUTHOR: -- Jaap Spies (2007-01-13) """ def _repr_(self): return "a(n) is the largest k such that n can be written as sum of k consecutive integers." raise ValueError, "input n (=%s) must be a non negative integer"%n return self._eval(m) def _eval(self, n): return m def list(self, n): return [self(i) for i in range(0,n)] ############################################################# # III. Create the sloane object, off which all the sequence #      objects hang. # III. Create the Sloane object, off which all the sequence #      objects are members. #############################################################