-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoeis.rb
121 lines (106 loc) · 2.25 KB
/
oeis.rb
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
require "prime"
module OEIS
def self.sequences
constants(false).select{|c| c.to_s.match(/^A\d{6}$/)}.sort.map{|c| const_get(c)}
end
class Sequence
include Enumerable
def each
@preamble.each{|e| yield e} if @preamble
while @step
yield @step.call
end
end
def self.first n
new.take n
end
private
def primes
Prime.lazy
end
def zero_to_infinity
(0..Float::INFINITY).each.lazy
end
end
class InfiniteSequence < Sequence
def to_a
raise "Inifinite sequences cannot be converted to arrays."
end
end
class A000007 < InfiniteSequence
def initialize
@preamble = [1]
@step = lambda{ 0 }
end
end
M0002 = A000007
class A002473 < InfiniteSequence
def initialize
@counter = 0
@step = lambda do
nil while ((Prime.prime_division(@counter += 1).map(&:first).max||0) > 7)
@counter
end
end
end
M0477 = N0177 = A002473
class A003418 < InfiniteSequence
def initialize
@counter = 0
@factors = Hash.new 0
@step = lambda do
Prime.prime_division(@counter += 1).each{|k,v| @factors[k] = [@factors[k],v].max}
Prime.int_from_prime_division @factors.to_a
end
end
end
M2638 = N1049 = A003418
class A004068 < InfiniteSequence
def initialize
@preamble = zero_to_infinity.map{|i| (5.0*(i**3.0)/6.0+i/6.0).to_i}
end
end
class A004680 < InfiniteSequence
def initialize
@preamble = primes.map{|i| i.to_s(6).to_i}
end
end
class A004681 < InfiniteSequence
def initialize
@preamble = primes.map{|i| i.to_s(7).to_i}
end
end
class A006928 < InfiniteSequence
def initialize
@state = [1,2]
@preamble = @state
@step = lambda do
return @state.push(@state.last).last if !(@reissue = !@reissue)
@reissue = (@state.shift == 2)
@state.push(-(@state.last-3)).last
end
end
end
M0070 = A006928
class A007318 < InfiniteSequence
def initialize
@row = [1]
@state = [1]
@step = lambda do
@state = (@row = [0,@row,0].flatten.each_cons(2).map{|a,b|a+b}.to_a).clone if @state == []
@state.shift
end
end
end
M0082 = A007318
class A085823 < Sequence
def initialize
@preamble = [2,3,5,7,23,37,53,73,373]
end
end
class A266313 < InfiniteSequence
def initialize
@preamble = [0,1,2,3,4,3,2,1].cycle
end
end
end