-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprog.cpp
101 lines (78 loc) · 1.84 KB
/
prog.cpp
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
/*
Daniel "Trizen" Suteu
Date: 24 September 2019
https://github.com/trizen
Least a(n) such that the period of continued fraction for sqrt(a(n)) has at least n successive 1's.
https://oeis.org/A060215
Known terms are: 2, 3, 7, 7, 13, 58, 58, 135, 461, 819, 2081, 13624, 13834, 35955, 95773, 244647, 639389, 1798800, 4374866, 11448871, 30002701, 78439683, 205337953, 541653136, 1407271538
*/
#include <cmath>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
int isqrt(int n) {
return static_cast<int>(std::sqrt((double) n));
}
vector<int> cfrac_sqrt(int n) {
int x = isqrt(n);
int y = x;
int z = 1;
int r = 2 * x;
vector<int> cfrac = {x};
if (x * x == n) {
return cfrac;
}
do {
y = r * z - y;
z = (n - y * y) / z;
r = (x + y) / z;
cfrac.push_back(r);
}
while (z != 1);
return cfrac;
}
string join_vector(vector<int> v) {
stringstream ss;
for (size_t i = 0; i < v.size(); ++i) {
if (i != 0) {
ss << " ";
}
ss << v[i];
}
string s = ss.str();
return s;
}
bool isok(int n, int k, string ones) {
vector<int> v = cfrac_sqrt(k);
if (v.size() <= n) {
return false;
}
string s = join_vector(v);
if (s.find(ones) != std::string::npos) {
return true;
}
return false;
}
int a(int n, int from) {
stringstream ss;
for (int t = 1; t <= n; ++t) {
ss << " 1";
}
ss << " ";
string ones = ss.str();
for (int k = from; ; ++k) {
if (isok(n, k, ones)) {
return k;
}
}
}
int main(int argc, char **argv) {
int from = 1;
for (int n = 1; n <= 30; ++n) {
int k = a(n, from);
cout << "a(" << n << ") = " << k << endl;
from = k;
}
}