프롤로그-170 바이트
[]/[].
T/R:-0*_*T*R.
C*X*[X|T]*R:-(C+1)*X*T*R.
C*X*T*Y:-10*C+X+Y+R,T/R.
N+R+A:-N=:=0,A=R;D is N mod 10,N//10+R+[D|A].
F-C-R:-C/N,(N=F,R=C;F-N-R).
[1]-[1,1]. S-T:-S-[1]-T.
이 기능은 기능을 정의합니다 (-)/2
. 당신은 그것을 호출 할 수 있습니다
?- [1,1,1,3,2,1,3,2,1,1]-T.
T = [1, 3, 1, 1, 2, 2, 2, 1] .
?- [1]-T.
T = [1, 1] .
이 시퀀스 에는 길이 가 홀수 인 하나의 길이 인 것처럼 보입니다 [1]
.
wr_len :- wr_len(1, [1]).
wr_len(N, Cur) :-
length(Cur, Len),
TrailingZeroes is lsb(Len),
(TrailingZeroes > 0 -> Par = 'even'; Par = 'odd'),
writef('%t\t%t\t%t\t%t\n', [N, Len, Par, TrailingZeroes]),
get_next(Cur, Next),
succ(N, O),
!, wr_len(O, Next).
% index, length, parity of length, num of trailing 0 in bin presentation of length
?- wr_len.
1 1 odd 0
2 2 even 1
3 2 even 1
4 4 even 2
5 6 even 1
6 6 even 1
7 8 even 3
8 10 even 1
9 14 even 1
10 20 even 2
11 26 even 1
12 34 even 1
13 46 even 1
14 62 even 1
15 78 even 1
16 102 even 1
17 134 even 1
18 176 even 4
19 226 even 1
20 302 even 1
21 408 even 3
22 528 even 4
23 678 even 1
24 904 even 3
25 1182 even 1
26 1540 even 2
27 2012 even 2
28 2606 even 1
29 3410 even 1
30 4462 even 1
31 5808 even 4
32 7586 even 1
33 9898 even 1
34 12884 even 2
35 16774 even 1
36 21890 even 1
37 28528 even 4
38 37158 even 1
39 48410 even 1
40 63138 even 1
41 82350 even 1
42 107312 even 4
43 139984 even 4
44 182376 even 3
45 237746 even 1
46 310036 even 2
47 403966 even 1
48 526646 even 1
49 686646 even 1
50 894810 even 1
51 1166642 even 1
52 1520986 even 1
53 1982710 even 1
54 2584304 even 4
55 3369156 even 2
56 4391702 even 1
57 5724486 even 1
58 7462860 even 2
59 9727930 even 1
ERROR: Out of global stack
% I added a few "strategic" cuts (`!`) to get so far.
읽을 수있는 :
get_next([], []).
get_next(Current, Next) :-
get_next_sub(0, _, Current, Next).
get_next_sub(Length, Digit, [Digit|Tail], Result) :-
get_next_sub(Length+1, Digit, Tail, Result).
get_next_sub(Length, Digit, Further, Result) :-
number_to_list(10*Length+Digit, Result, ResultTail),
get_next(Further, ResultTail).
number_to_list(Number, Result, Accumulator) :-
0 is Number -> Result = Accumulator;
Digit is Number mod 10,
number_to_list(Number // 10, Result, [Digit|Accumulator]).
get_previous(Stop, Current, Result) :-
get_next(Current, Next),
( Next = Stop
-> Result = Current
; get_previous(Stop, Next, Result)
).
get_prev_or_next(Input, Result) :-
length(Input, Length),
( 1 is Length mod 2
-> get_next(Input, Result)
; get_previous(Input, [1], Result)
).
513111
로 분할을51
,32
그리고11
?