-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathp40.cpp
More file actions
59 lines (54 loc) · 1.23 KB
/
Copy pathp40.cpp
File metadata and controls
59 lines (54 loc) · 1.23 KB
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
#include <iostream>
#include <algorithm>
#include "euler/digits.hpp"
#include "euler.h"
BEGIN_PROBLEM(40, solve_problem_40)
PROBLEM_TITLE("Finding the nth digit of a fractional number")
PROBLEM_ANSWER("210")
PROBLEM_DIFFICULTY(1)
PROBLEM_FUN_LEVEL(1)
PROBLEM_TIME_COMPLEXITY("ln(N)")
PROBLEM_SPACE_COMPLEXITY("1")
PROBLEM_KEYWORDS("digit")
END_PROBLEM()
static int get_nth_digit(int m, int p)
{
auto digits = euler::digits(m);
auto it = digits.begin();
std::advance(it, (p - 1));
return *it;
}
static void solve_problem_40()
{
const int N = 1000000;
const int b = 10; // base
int result = 1;
int n = 1;
int k = 1; // k-digit group
int s = 0; // total number of digits till the end of k-digit group
int bexp = 1; // b^(k-1)
for (; ; )
{
int s_next = s + (b-1)*k*bexp;
for (; n <= s_next && n <= N; n *= b)
{
int i = (n - s - 1) / k;
int m = bexp + i;
int p = n - s - i*k;
if (verbose())
{
std::cout << p << "-th digit in " << m << " is "
<< get_nth_digit(m, p) << std::endl;
}
result *= get_nth_digit(m, p);
}
if (n > N)
{
break;
}
s = s_next;
bexp *= b;
k++;
}
std::cout << result << std::endl;
}