-
Notifications
You must be signed in to change notification settings - Fork 0
/
palindrome_2_3.wsf
59 lines (50 loc) · 1.3 KB
/
palindrome_2_3.wsf
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
# Rosetta Code
# Find palindromic numbers in both binary and ternary bases
# https://rosettacode.org/wiki/Find_palindromic_numbers_in_both_binary_and_ternary_bases
"@int" import
0 ^ readi retrieve # n
# Handle 0 separately
0 call print_pal
# Iterate chunked by magnitude so that palindromes are visited in
# order.
1 ^ # i found
.loop_magnitude: # IVs: i found
^1 2* swap # hi = i*2
^2 # j = i
.loop_odd: # IVs: found j
^ 2 call int.make_palindrome_odd
^ ^ 3 call int.reverse_digits j= .is_pal_odd
drop
.loop_odd_continue:
1+ # j++
^ ^3 j< .loop_odd # j < hi
drop
^2 # j = i
.loop_even: # IVs: found j
^ 2 call int.make_palindrome_even
^ ^ 3 call int.reverse_digits j= .is_pal_even
drop
.loop_even_continue:
1+ # j++
^ ^3 j< .loop_even # j < hi
drop
swap drop swap 2* swap # i *= 2
jmp .loop_magnitude
.is_pal_odd:
call print_pal
swap 1+ swap # j++
^1 ^5 j< .loop_odd_continue # found < n
jmp .end
.is_pal_even:
call print_pal
swap 1+ swap # j++
^1 ^5 j< .loop_even_continue # found < n
.end:
5drop end
# print_pal prints a palindrome in decimal, binary, and ternary.
# (n -- )
print_pal:
^ printi ' ' printc
^ 3 call int.print_base ' ' printc
2 call int.print_base '\n' printc
ret