/
sloane_functions.py
9190 lines (7026 loc) · 223 KB
/
sloane_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# sage.doctest: needs sage.libs.gap sage.libs.flint sage.libs.pari sage.modules
r"""
Functions that compute some of the sequences in Sloane's tables
EXAMPLES:
Type ``sloane.[tab]`` to see a list of the sequences that are defined.
::
sage: a = sloane.A000005; a
The integer sequence tau(n), which is the number of divisors of n.
sage: a(1)
1
sage: a(6)
4
sage: a(100)
9
Type ``d._eval??`` to see how the function that
computes an individual term of the sequence is implemented.
The input must be a positive integer::
sage: a(0)
Traceback (most recent call last):
...
ValueError: input n (=0) must be a positive integer
sage: a(1/3)
Traceback (most recent call last):
...
TypeError: input must be an int or Integer
You can also change how a sequence prints::
sage: a = sloane.A000005; a
The integer sequence tau(n), which is the number of divisors of n.
sage: a.rename('(..., tau(n), ...)')
sage: a
(..., tau(n), ...)
sage: a.reset_name()
sage: a
The integer sequence tau(n), which is the number of divisors of n.
TESTS::
sage: a = sloane.A000001
sage: a == loads(dumps(a))
True
We agree with the online database::
sage: for t in sloane.__dir__(): # long time; optional -- internet; known bug
....: online_list = list(oeis(t).first_terms())
....: L = max(2, len(online_list) // 2)
....: sage_list = sloane.__getattribute__(t).list(L)
....: if online_list[:L] != sage_list:
....: print('{} seems wrong'.format(t))
.. SEEALSO::
- If you want to get more informations relative to a sequence (references,
links, examples, programs, ...), you can use the On-Line Encyclopedia of
Integer Sequences provided by the :mod:`OEIS <sage.databases.oeis>`
module.
- If you plan to do a lot of automatic searches for subsequences, you
should consider installing :mod:`SloaneEncyclopedia
<sage.databases.sloane>`, a local partial copy of the OEIS.
AUTHORS:
- William Stein: framework
- Jaap Spies: most sequences
- Nick Alexander: updated framework
"""
########################################################################
#
# To add your own new sequence here, do the following:
#
# 1. Add a new class to Section II below, which you should
# do by copying an existing class and modifying it.
# Make sure to at least define _eval and _repr_.
# NOTE: (a) define the _eval method only, which you may
# assume has as input a *positive* Sage integer (offset > 0).
# Each sequence in the OEIS has an offset >= 0, indicating the
# value of the first index. The default offset = 1.
# (b) define the list method if there is a faster
# way to compute the terms of the sequence than
# just calling _eval (which is the default definition
# of list, note: the offset is counted for, it lists n numbers).
# (c) *AVOID* using gp.method if possible! Use pari(obj).method()
# (d) In many cases the function that computes a given integer
# sequence belongs elsewhere in Sage. Put it there and make
# your class in this file just call it.
# (e) _eval should always return a Sage integer.
#
# 2. Add an instance of your class in Section III below.
#
# 3. Type "sage -br" to rebuild Sage, then fire up the notebook and
# try out your new sequence. Click the text button to get a version
# of your session that you then include as a docstring.
# You can check your results with the entries of the OEIS::
# sage: seq = oeis(45) ; seq
# A000045: Fibonacci numbers: F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.
# sage: seq.first_terms()[:12]
# (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89)
#
# 4. Send a patch using
# sage: hg_sage.ci()
# sage: hg_sage.send('patchname')
# (Email it to sage-dev@groups.google.com or post it online.)
#
########################################################################
########################################################################
# I. Define the generic Sloane sequence class.
########################################################################
# just used for handy .load, .save, etc.
import sys
import inspect
from sage.structure.sage_object import SageObject
from sage.arith.srange import srange
from sage.rings.integer_ring import ZZ
from sage.functions.all import prime_pi
from sage.rings.integer import Integer as Integer_class
# You may have to import more here when defining new sequences
import sage.arith.all as arith
from sage.matrix.matrix_space import MatrixSpace
from sage.rings.rational_field import QQ
from sage.combinat import combinat
from sage.misc.misc_c import prod
class SloaneSequence(SageObject):
r"""
Base class for a Sloane integer sequence.
"""
def __init__(self, offset=1):
r"""
A sequence starting at offset (=1 by default).
EXAMPLES::
sage: from sage.combinat.sloane_functions import SloaneSequence
sage: SloaneSequence().offset
1
sage: SloaneSequence(4).offset
4
"""
self.offset = ZZ(offset)
def _repr_(self):
"""
EXAMPLES::
sage: from sage.combinat.sloane_functions import SloaneSequence
sage: SloaneSequence(4)._repr_()
Traceback (most recent call last):
...
NotImplementedError
"""
raise NotImplementedError
def __eq__(self, other):
"""
EXAMPLES::
sage: sloane.A000007 == sloane.A000045
False
sage: sloane.A000007 == sloane.A000007
True
"""
if not isinstance(other, SloaneSequence):
return False
return repr(self) == repr(other)
def __ne__(self, other):
"""
EXAMPLES::
sage: sloane.A000007 != sloane.A000045
True
sage: sloane.A000007 != sloane.A000007
False
"""
return not self.__eq__(other)
def _sage_src_(self):
"""
Return the source code for the class of ``self``.
EXAMPLES::
sage: sloane.A000045._sage_src_()
'class A000045(...'
"""
from sage.misc.sageinspect import sage_getsource
return sage_getsource(self.__class__)
def __call__(self, n):
"""
EXAMPLES::
sage: sloane.A000007(2)
0
sage: sloane.A000007('a')
Traceback (most recent call last):
...
TypeError: input must be an int or Integer
sage: sloane.A000007(-1)
Traceback (most recent call last):
...
ValueError: input n (=-1) must be an integer >= 0
sage: sloane.A000001(0)
Traceback (most recent call last):
...
ValueError: input n (=0) must be a positive integer
"""
if not isinstance(n, (int, Integer_class)):
raise TypeError("input must be an int or Integer")
m = ZZ(n)
if m < self.offset:
if self.offset == 1:
raise ValueError("input n (=%s) must be a positive integer" % (n))
else:
raise ValueError("input n (=%s) must be an integer >= %s" % (n, self.offset))
return self._eval(m)
def _eval(self, n):
"""
EXAMPLES::
sage: from sage.combinat.sloane_functions import SloaneSequence
sage: SloaneSequence(0)._eval(4)
Traceback (most recent call last):
...
NotImplementedError
"""
# this is what you implement in the derived class
# the input n is assumed to be a *Sage* integer >= offset
raise NotImplementedError
def list(self, n):
r"""
Return ``n`` terms of the sequence::
sequence[offset], sequence[offset+1], ..., sequence[offset+n-1].
EXAMPLES::
sage: sloane.A000012.list(4)
[1, 1, 1, 1]
"""
return [self._eval(i) for i in srange(self.offset, n+self.offset)]
# The Python default tries repeated __getitem__ calls, which will succeed,
# but is probably not what is wanted.
# This prevents list(sequence) from wandering off.
def __iter__(self):
"""
EXAMPLES::
sage: iter(sloane.A000012)
Traceback (most recent call last):
...
NotImplementedError
"""
raise NotImplementedError
def __getitem__(self, n):
r"""
Return the ``n``-th item of ``self``.
We interpret slices as best we can, but our sequences are infinite
so we want to prevent some mis-incantations.
Therefore, we arbitrarily cap slices to be at most LENGTH=100000
elements long. Since many Sloane sequences are costly to compute,
this is probably not an unreasonable decision, but just in case,
list does not cap length.
EXAMPLES::
sage: sloane.A000012[3]
1
sage: sloane.A000012[:4]
[1, 1, 1, 1]
sage: sloane.A000012[:10]
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
sage: sloane.A000012[4:10]
[1, 1, 1, 1, 1, 1]
sage: sloane.A000012[0:1000000000]
Traceback (most recent call last):
...
IndexError: slice (=slice(0, 1000000000, None)) too long
sage: sloane.A000001[:8]
[1, 1, 1, 2, 1, 2, 1]
sage: sloane.A000001[0:8:2]
[1, 2, 2]
sage: sloane.A000001[1:8:2]
[1, 1, 1, 1]
"""
if not isinstance(n, slice):
return self(n)
LENGTH = 100000
(start, stop, step) = n.indices(2 * LENGTH)
if abs(stop - start) > LENGTH:
raise IndexError("slice (=%s) too long" % n)
return [self(i) for i in range(start, stop, step) if i >= self.offset]
########################################################################
# II. Actual implementations of Sloane sequences.
########################################################################
# This one should be here!
class A000001(SloaneSequence):
def __init__(self):
r"""
Number of groups of order `n`.
INPUT:
- ``n`` -- positive integer
OUTPUT: integer
EXAMPLES::
sage: a = sloane.A000001;a
Number of groups of order n.
sage: a(0)
Traceback (most recent call last):
...
ValueError: input n (=0) must be a positive integer
sage: a(1)
1
sage: a(2)
1
sage: a(9)
2
sage: a.list(16)
[1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14]
sage: a(60)
13
AUTHORS:
- Jaap Spies (2007-02-04)
"""
self._small = [1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51, 1, 2, 1, 14, 1, 2, 2, 14, 1, 6, 1, 4, 2, 2, 1, 52, 2, 5]
SloaneSequence.__init__(self, offset=1)
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A000001._repr_()
'Number of groups of order n.'
"""
return "Number of groups of order n."
def _eval(self, n):
"""
EXAMPLES::
sage: sloane.A000001._eval(4)
2
sage: sloane.A000001._eval(51)
1
sage: sloane.A000001._eval(5000)
Traceback (most recent call last):
...
GAPError: Error, the library of groups of size 5000 is not available
"""
if n <= 50:
return self._small[n - 1]
from sage.libs.gap.libgap import libgap
return ZZ(libgap.NumberSmallGroups(n))
class A000027(SloaneSequence):
def __init__(self):
r"""
The natural numbers. Also called the whole numbers, the counting
numbers or the positive integers.
The following examples are tests of SloaneSequence more than
A000027.
EXAMPLES::
sage: s = sloane.A000027; s
The natural numbers.
sage: s(10)
10
Index n is interpreted as _eval(n)::
sage: s[10]
10
Slices are interpreted with absolute offsets, so the following
returns the terms of the sequence up to but not including the third
term::
sage: s[:3]
[1, 2]
sage: s[3:6]
[3, 4, 5]
sage: s.list(5)
[1, 2, 3, 4, 5]
"""
SloaneSequence.__init__(self, offset=1)
# is this a good idea to have a link for all sequences? Jaap
link = "http://oeis.org/classic/A000027"
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A000027._repr_()
'The natural numbers.'
"""
return "The natural numbers."
def _eval(self, n):
"""
EXAMPLES::
sage: sloane.A000027._eval(5)
5
"""
return n
class A000004(SloaneSequence):
def __init__(self):
r"""
The zero sequence.
INPUT:
- ``n`` - non negative integer
EXAMPLES::
sage: a = sloane.A000004; a
The zero sequence.
sage: a(1)
0
sage: a(2007)
0
sage: a.list(12)
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
AUTHORS:
- Jaap Spies (2006-12-10)
"""
SloaneSequence.__init__(self, offset=0)
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A000004._repr_()
'The zero sequence.'
"""
return "The zero sequence."
def _eval(self, n):
"""
EXAMPLES::
sage: sloane.A000004._eval(5)
0
"""
return ZZ.zero()
class A000005(SloaneSequence):
def __init__(self):
r"""
The sequence `tau(n)`, which is the number of divisors of `n`.
This sequence is also denoted `d(n)` (also called
`\tau(n)` or `\sigma_0(n)`), the number of
divisors of `n`.
INPUT:
- ``n`` - positive integer
EXAMPLES::
sage: d = sloane.A000005; d
The integer sequence tau(n), which is the number of divisors of n.
sage: d(1)
1
sage: d(6)
4
sage: d(51)
4
sage: d(100)
9
sage: d(0)
Traceback (most recent call last):
...
ValueError: input n (=0) must be a positive integer
sage: d.list(10)
[1, 2, 2, 3, 2, 4, 2, 4, 3, 4]
AUTHORS:
- Jaap Spies (2006-12-10)
- William Stein (2007-01-08)
"""
SloaneSequence.__init__(self, offset=1)
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A000005._repr_()
'The integer sequence tau(n), which is the number of divisors of n.'
"""
return "The integer sequence tau(n), which is the number of divisors of n."
def _eval(self, n):
"""
EXAMPLES::
sage: sloane.A000005._eval(5)
2
"""
return arith.number_of_divisors(n)
class A000008(SloaneSequence):
def __init__(self):
r"""
Number of ways of making change for n cents using coins
of 1, 2, 5, 10 cents.
INPUT:
- ``n`` -- non negative integer
OUTPUT:
- ``integer`` -- function value
EXAMPLES::
sage: a = sloane.A000008;a
Number of ways of making change for n cents using coins of 1, 2, 5, 10 cents.
sage: a(0)
1
sage: a(1)
1
sage: a(13)
16
sage: a.list(14)
[1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 16]
AUTHOR:
- J. Gaski (2009-05-29)
"""
SloaneSequence.__init__(self, offset=0)
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A000008._repr_()
'Number of ways of making change for n cents using coins of 1, 2, 5, 10 cents.'
"""
return "Number of ways of making change for n cents using coins of 1, 2, 5, 10 cents."
def _eval(self, n):
"""
EXAMPLES::
sage: [sloane.A000008._eval(n) for n in range(14)]
[1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 11, 12, 15, 16]
"""
from sage.combinat.partition import Partitions
return Partitions(n, parts_in=[1, 2, 5, 10]).cardinality()
class A000009(SloaneSequence):
def __init__(self):
r"""
Number of partitions of `n` into odd parts.
INPUT:
- ``n`` -- non negative integer
OUTPUT:
- ``integer`` -- function value
EXAMPLES::
sage: a = sloane.A000009;a
Number of partitions of n into odd parts.
sage: a(0)
1
sage: a(1)
1
sage: a(13)
18
sage: a.list(14)
[1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 15, 18]
AUTHOR:
- Jaap Spies (2007-01-30)
"""
SloaneSequence.__init__(self, offset=0)
self._b = []
self._precompute(2)
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A000009._repr_()
'Number of partitions of n into odd parts.'
"""
return "Number of partitions of n into odd parts."
def cf(self):
"""
EXAMPLES::
sage: it = sloane.A000009.cf()
sage: [next(it) for i in range(14)]
[1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 15, 18]
"""
_, x = QQ['x'].objgen()
k = 0
yield ZZ.one()
p = 1
while True:
k += 1
p *= (1+x**k)
yield ZZ(p.coefficients(sparse=False)[k])
def _precompute(self, how_many=50):
"""
EXAMPLES::
sage: initial = len(sloane.A000009._b)
sage: sloane.A000009._precompute(10)
sage: len(sloane.A000009._b) - initial == 10
True
"""
try:
f = self._f
except AttributeError:
self._f = self.cf()
f = self._f
self._b += [next(f) for i in range(how_many)]
def _eval(self, n):
"""
EXAMPLES::
sage: [sloane.A000009._eval(i) for i in range(14)]
[1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 15, 18]
"""
if len(self._b) <= n:
self._precompute(n - len(self._b) + 1)
return self._b[n]
def list(self, n):
"""
EXAMPLES::
sage: sloane.A000009.list(14)
[1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10, 12, 15, 18]
"""
self._eval(n) # force computation
return self._b[:n]
class A000796(SloaneSequence):
def __init__(self):
r"""
Decimal expansion of `\pi`.
INPUT:
- ``n`` -- positive integer
OUTPUT:
- ``integer`` -- function value
EXAMPLES::
sage: a = sloane.A000796;a
Decimal expansion of Pi.
sage: a(0)
Traceback (most recent call last):
...
ValueError: input n (=0) must be a positive integer
sage: a(1)
3
sage: a(13)
9
sage: a.list(14)
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7]
sage: a(100)
7
AUTHOR:
- Jaap Spies (2007-01-30)
"""
SloaneSequence.__init__(self, offset=1)
self._b = []
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A000796._repr_()
'Decimal expansion of Pi.'
"""
return "Decimal expansion of Pi."
def pi(self):
"""
Based on an algorithm of Lambert Meertens The ABC-programming
language!!!
EXAMPLES::
sage: it = sloane.A000796.pi()
sage: [next(it) for i in range(10)]
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
"""
k, a, b, a1, b1 = ZZ(2), ZZ(4), ZZ.one(), ZZ(12), ZZ(4)
while True:
p, q, k = k*k, 2*k+1, k+1
a, b, a1, b1 = a1, b1, p*a+q*a1, p*b+q*b1
d, d1 = a//b, a1//b1
while d == d1:
yield d
a, a1 = 10*(a % b), 10*(a1 % b1)
d, d1 = a//b, a1//b1
def _precompute(self, how_many=1000):
"""
EXAMPLES::
sage: initial = len(sloane.A000796._b)
sage: sloane.A000796._precompute(10)
sage: len(sloane.A000796._b) - initial
10
"""
try:
f = self._f
except AttributeError:
self._f = self.pi()
f = self._f
self._b += [next(f) for i in range(how_many)]
def _eval(self, n):
"""
EXAMPLES::
sage: [sloane.A000796._eval(n) for n in range(1,11)]
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
"""
while len(self._b) <= n:
self._precompute()
return self._b[n-1]
def list(self, n):
"""
EXAMPLES::
sage: sloane.A000796.list(10)
[3, 1, 4, 1, 5, 9, 2, 6, 5, 3]
"""
self._eval(n) # force computation
return self._b[:n]
class A003418(SloaneSequence):
def __init__(self):
r"""
Least common multiple (or lcm) of `\{1, 2, \ldots, n\}`.
INPUT:
- ``n`` -- non negative integer
OUTPUT:
- ``integer`` -- function value
EXAMPLES::
sage: a = sloane.A003418;a
Least common multiple (or lcm) of {1, 2, ..., n}.
sage: a(0)
1
sage: a(1)
1
sage: a(13)
360360
sage: a.list(14)
[1, 1, 2, 6, 12, 60, 60, 420, 840, 2520, 2520, 27720, 27720, 360360]
sage: a(20.0)
Traceback (most recent call last):
...
TypeError: input must be an int or Integer
AUTHOR:
- Jaap Spies (2007-01-31)
"""
SloaneSequence.__init__(self, offset=0)
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A003418._repr_()
'Least common multiple (or lcm) of {1, 2, ..., n}.'
"""
return "Least common multiple (or lcm) of {1, 2, ..., n}."
def _eval(self, n):
"""
EXAMPLES::
sage: [sloane.A003418._eval(n) for n in range(1,11)]
[1, 2, 6, 12, 60, 60, 420, 840, 2520, 2520]
"""
return arith.lcm([i for i in range(1, n+1)])
class A007318(SloaneSequence):
def __init__(self):
r"""
Pascal's triangle read by rows:
`C(n,k) = \binom{n}{k} = \frac {n!} {(k!(n-k)!)}`,
`0 \le k \le n`.
INPUT:
- ``n`` -- non negative integer
OUTPUT:
- ``integer`` -- function value
EXAMPLES::
sage: a = sloane.A007318
sage: a(0)
1
sage: a(1)
1
sage: a(13)
4
sage: a.list(15)
[1, 1, 1, 1, 2, 1, 1, 3, 3, 1, 1, 4, 6, 4, 1]
sage: a(100)
715
AUTHORS:
- Jaap Spies (2007-01-31)
"""
SloaneSequence.__init__(self, offset=0)
keyword = ["nonn", "tabl", "nice", "easy", "core", "triangle"]
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A007318._repr_()
"Pascal's triangle read by rows: C(n,k) = binomial(n,k) = n!/(k!*(n-k)!), 0<=k<=n."
"""
return "Pascal's triangle read by rows: C(n,k) = binomial(n,k) = n!/(k!*(n-k)!), 0<=k<=n."
def _eval(self, n):
"""
EXAMPLES::
sage: [sloane.A007318._eval(n) for n in range(10)]
[1, 1, 1, 1, 2, 1, 1, 3, 3, 1]
"""
m = 0
while m*(m+1)//2 <= n:
m += 1
m -= 1
k = n - m*(m+1)//2
return arith.binomial(m, k)
class A008275(SloaneSequence):
def __init__(self):
r"""
Triangle of Stirling numbers of first kind, `s(n,k)`,
`n \ge 1`, `1 \le k \le n`.
The unsigned numbers are also called Stirling cycle numbers:
`|s(n,k)|` = number of permutations of `n` objects
with exactly `k` cycles.
INPUT:
- ``n`` -- non negative integer
OUTPUT:
- ``integer`` -- function value
EXAMPLES::
sage: a = sloane.A008275;a
Triangle of Stirling numbers of first kind, s(n,k), n >= 1, 1<=k<=n.
sage: a(0)
Traceback (most recent call last):
...
ValueError: input n (=0) must be a positive integer
sage: a(1)
1
sage: a(2)
-1
sage: a(3)
1
sage: a(11)
24
sage: a.list(12)
[1, -1, 1, 2, -3, 1, -6, 11, -6, 1, 24, -50]
AUTHORS:
- Jaap Spies (2007-02-02)
"""
SloaneSequence.__init__(self, offset=1)
keyword = ["sign", "tabl", "nice", "core", "triangle"]
def _repr_(self):
"""
EXAMPLES::
sage: sloane.A008275._repr_()
'Triangle of Stirling numbers of first kind, s(n,k), n >= 1, 1<=k<=n.'
"""
return "Triangle of Stirling numbers of first kind, s(n,k), n >= 1, 1<=k<=n."
def s(self, n, k):
"""
EXAMPLES::
sage: sloane.A008275.s(4,2)
11
sage: sloane.A008275.s(5,2)
-50
sage: sloane.A008275.s(5,3)
35
"""
return (-1)**(n-k) * combinat.stirling_number1(n, k)
def _eval(self, n):
"""
EXAMPLES::
sage: [sloane.A008275._eval(n) for n in range(1, 11)]
[1, -1, 1, 2, -3, 1, -6, 11, -6, 1]
"""