-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEuler_Problem-024.b93
49 lines (34 loc) · 1.84 KB
/
Euler_Problem-024.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
v0123456789
>"ddd"**1v
vp129p11-<
>v>v v < v < > v >\1-\vv p14+1g14<
>21g:1+|>|>21g0\>:1-:#^_v>*\:#^_$:| >31p 141p >31g41g*11g`!|
$ >$1 ^ > p68*v>$1^ |-"x" g0:\<\1<g14 <
@ > #^0#<#v0#,+ #<$ v> >1+\:| 1
^p12-1g12p11-*-1g14g13g11 <^\"x"\-"0"g0:>#- #1 #$ #< ^
AB` A>B
[1,1] Current Number (1m -> 0)
[2,1] Current Possibilities (9 -> 0)
[3,1] Fac(Current Possibilities)
[4,1] Current Digit Index (1 -> ..)
// Factorial function (stack -> stack)
v < v < > v
0\>:1-:#^_$>*\:#^_:| >
>$1^
// 1 million
"ddd"**
// Get X-th number and cross out (num on stack)
>\1-\v
1\>\:0g "x"-#^_ >1+v>
|:\ <
>$1-:0g"0"-\"x"\0p ^
// Solution: 2783915460
---------------------------------------
This is a really nice problem - and I kinda like the solution.
If we think about the ordered numbers we can see that the first few start with `0`, the next with `1` and so on ...
We can also see that there are `8!` possible numbers that start with `0` (or any other digit).
So if `1 * 8! <= (1000000 - 0)` the result starts with `0`. Otherwise if `2 * 8! <= (1000000 - 0)`, etc.
Then after we got our first digit (`2`) we can similar calculate the second with `1 * 7! <= (1000000 - 2 * 8!)`, `2 * 7! <= (1000000 - 2 * 8!)` ...
The last thing we have to be aware of is that this method yields us to the "n-th digit of the remaining digits". So if we got the 6th digit for our second calculation its in fact `7`, because we already used `2`.
The program now does exactly these calculations, you can see in the top row the already used digits (they are crossed out).
All in all this program pretty fast - they could really do another version of this problem with bigger numbers.