-
Notifications
You must be signed in to change notification settings - Fork 0
/
palin.wsf
50 lines (46 loc) · 1.08 KB
/
palin.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
# Sphere Online Judge
# Problem 5: PALIN - The Next Palindrome
# https://www.spoj.com/problems/PALIN/
"@bool" import
"@int" import
"@math" import
0 ^ readi retrieve
.loop:
1- ^ jn .end
0 ^ readi retrieve
1+ call find_pal printi '\n' printc
jmp .loop
.end:
drop end
# find_pal returns the smallest base-10 palindrome greater than or equal
# to n.
# (n -- pal)
find_pal:
^ call digit_count
10 ^1 2/ **
swap 2% jz .find_pal_even
2dup / # upper
^
^2 10* ^4 ^1 % + # lower with leading 1
10 call int.reverse_digits 10/
< + # upper++ if reverse(lower) > upper
10 call int.make_palindrome_odd
2slide ret
.find_pal_even:
2dup / # upper
^
^2 ^4 ^1 % + # lower with leading 1
10 call int.reverse_digits 10/
< + # upper++ if reverse(lower) > upper
10 call int.make_palindrome_even
2slide ret
# digit_count returns the number of base-10 digits in n.
# (n -- count)
digit_count:
0
.digit_count_loop:
swap ^ jz .digit_count_ret
10/ swap 1+
jmp .digit_count_loop
.digit_count_ret:
drop ret