Changeset 2402:5aeb36277f36
- Timestamp:
- 01/13/07 17:21:38 (6 years ago)
- 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. - File:
-
- 1 edited
-
sage/databases/sloane_functions.py (modified) (22 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/databases/sloane_functions.py
r2401 r2402 156 156 class A000010(SloaneSequence): 157 157 r""" 158 A000010 is Euler's totient function.158 The integer sequence A000010 is Euler's totient function. 159 159 160 160 Number of positive integers $i < n$ that are relative prime to $n$. … … 205 205 class A000045(SloaneSequence): 206 206 r""" 207 returns Fibonacci number with index $n \le 1001$, offset 0,4208 209 S. Plouffe, Project Gutenberg, The First 1001 Fibonacci Numbers210 http://ibiblio.org/pub/docs/books/gutenberg/etext01/fbncc10.txt211 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} 212 212 We have one more. Our first Fibonacci number is 0. 213 214 215 216 213 217 214 INPUT: … … 221 218 integer -- function value 222 219 223 224 225 220 EXAMPLES: 226 221 sage: a = sloane.A000045; a 227 222 Fibonacci number with index n >= 0 228 229 223 sage: a(1) 230 224 1 … … 241 235 -- Jaap Spies (2007-01-13) 242 236 """ 237 def __init__(self): 238 self._b = [] 239 243 240 def _repr_(self): 244 241 return "Fibonacci number with index n >= 0" … … 247 244 m = Integer(n) 248 245 if m < 0: 249 raise ValueError, "input n (=%s) must be a non negative integer"%n246 raise ValueError, "input n (=%s) must be a non-negative integer"%n 250 247 return self._eval(m) 251 248 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): 254 258 """ 255 generates an "infinity" of Fibonacci numbers, 256 starting with 0 259 Returns a generator over all Fibanacci numbers, starting with 0. 257 260 """ 258 x, y = 0, 1261 x, y = Integer(0), Integer(1) 259 262 yield x 260 while 1:263 while True: 261 264 x, y = y, x+y 262 265 yield x … … 264 267 offset = 0 265 268 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 277 278 278 279 class A000203(SloaneSequence): 279 280 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)$. 283 283 284 284 INPUT: … … 306 306 TypeError: Unable to coerce rational (=1/3) to an Integer. 307 307 308 AUTHOR:309 - Jaap Spies (2007-01-13)308 AUTHOR: 309 -- Jaap Spies (2007-01-13) 310 310 """ 311 311 … … 318 318 return sum(arith.divisors(n)) 319 319 320 321 320 def list(self, n): 322 321 return [self(i) for i in range(1,n+1)] 323 322 324 323 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 least330 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$ consecutive333 integers (other than the trivial $n = n$ for $k = 1$).334 335 See: http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf336 337 INPUT:338 n -- positive integer339 340 OUTPUT:341 True -- if n is not prime and not a power of 2342 False --343 344 EXAMPLES:345 sage: is_number_of_the_third_kind(6)346 True347 348 sage: is_number_of_the_third_kind(100)349 True350 351 sage: is_number_of_the_third_kind(16)352 False353 354 sage: is_number_of_the_third_kind(97)355 False356 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 True363 else:364 return False365 366 324 367 325 def is_power_of_two(n): 368 r""" "369 This function returns True if f $n$ is a power of 2326 r""" 327 This function returns True if and only if $n$ is a power of 2 370 328 371 329 INPUT: … … 377 335 378 336 EXAMPLES: 337 sage: from sage.databases.sloane_functions import is_power_of_two 338 379 339 sage: is_power_of_two(1024) 380 340 True … … 393 353 394 354 AUTHOR: 395 - Jaap Spies (2006-12-09)396 397 """ 398 # modification of is2pow(n) from the Programming Guide355 -- Jaap Spies (2006-12-09) 356 357 """ 358 # modification of is2pow(n) from the Programming Guide 399 359 while n > 0 and n%2 == 0: 400 360 n = n >> 1 … … 403 363 class A111774(SloaneSequence): 404 364 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 410 368 Odd primes can only be written as a sum of two consecutive integers. 411 369 Powers of 2 do not have a representation as a sum of $k$ consecutive … … 439 397 TypeError: Unable to coerce rational (=1/3) to an Integer. 440 398 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 441 466 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 457 474 458 475 class A111775(SloaneSequence): … … 468 485 there is a unique corresponding $k$, $k=1$ and $k=2$ must be excluded. 469 486 470 See: http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf487 See: \url{http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf} 471 488 472 489 INPUT: … … 479 496 sage: a = sloane.A111775; a 480 497 Number of ways n can be written as a sum of at least three consecutive integers. 498 481 499 sage: a(1) 482 500 0 483 501 sage: a(0) 484 502 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 485 508 sage: a(100) 486 509 2 … … 496 519 TypeError: Unable to coerce rational (=1/3) to an Integer. 497 520 498 AUTHOR: 499 - Jaap Spies (2006-12-09) 500 """ 501 521 AUTHOR: 522 -- Jaap Spies (2006-12-09) 523 """ 502 524 def _repr_(self): 503 525 return "Number of ways n can be written as a sum of at least three consecutive integers." … … 521 543 return k-2 522 544 523 524 525 545 def list(self, n): 526 546 return [self(i) for i in range(0,n)] … … 529 549 class A111776(SloaneSequence): 530 550 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. 532 553 533 554 $n$ is the sum of at most $a(n)$ consecutive positive integers. … … 537 558 $k = min(d,2n/d)$. $a(n)$ is the largest among those $k$ 538 559 . 539 See: http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf560 See: \url{http://www.jaapspies.nl/mathfiles/problem2005-2C.pdf} 540 561 541 562 INPUT: … … 545 566 integer -- function value 546 567 547 548 EXAMPLES: 549 550 AUTHOR: 551 - Jaap Spies (2007-01-13) 552 """ 553 554 568 AUTHOR: 569 -- Jaap Spies (2007-01-13) 570 """ 555 571 def _repr_(self): 556 572 return "a(n) is the largest k such that n can be written as sum of k consecutive integers." … … 563 579 raise ValueError, "input n (=%s) must be a non negative integer"%n 564 580 return self._eval(m) 565 566 581 567 582 def _eval(self, n): … … 575 590 return m 576 591 577 578 592 def list(self, n): 579 593 return [self(i) for i in range(0,n)] … … 581 595 582 596 583 584 585 586 587 588 597 ############################################################# 589 # III. Create the sloane object, off which all the sequence590 # objects hang.598 # III. Create the Sloane object, off which all the sequence 599 # objects are members. 591 600 ############################################################# 592 601
Note: See TracChangeset
for help on using the changeset viewer.
