-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEuler_Problem-040.b93
59 lines (41 loc) · 1.69 KB
/
Euler_Problem-040.b93
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
v
>01:55+*:55+*:55+*:55+*:55+*:55+*130p v
v <
>110p120p>:10g920g**`!#v_10g920g**-10g1+10p20g55+*20p^>v <
>30g.@ >1-:10g/20g+\ 10g%10g\-1-:|>\55+/\1-:#^_v
$ $ > :^
^_^#: <# p03*g03%+55$<
// getDigit (value, digit_idx -> digit) // digit of a single number at position x
>v <
:|>\55+/\1-:#^_$55+%
> :^
// digitAt (pos -> digit) // digit of fraction at position x
v <
110p120p>:10g920g**`!#v_10g920g**-10g1+10p20g55+*20p^>v <
>1-: 10g/20g+ \ 10g%10g\-1- :|>\55+/\1-:#^_$55+%
> :^
[1,0] digitcount
[2,0] digitvalue
[3,0] result
---------------------------------------
This one is really great - I came up with an O(log n) algorithm (crazy fast) for determining the n-th digit.
First I tested it in LinqPad, so here the C# code for the algorithm:
```csharp
public int digitAt(int pos) {
int digitcount = 1;
int digitvalue = 1;
// Get DigitCount of current number
while(pos > digitvalue * 9 * digitcount) {
pos -= digitvalue * 9 * digitcount;
digitcount++;
digitvalue *= 10;
}
// current number and digit-position in number
int value = digitvalue + (pos - 1)/digitcount;
int digit = digitcount - (pos - 1)%digitcount - 1;
return getInternalDigit(value, digit);
}
public int getInternalDigit(int value, int digit) {
return (value / (int)Math.Pow(10, digit)) % 10;
}
```