1 define divrem10 : (int int -> int) 2 {{ 3 desc: 4 Pop a number from the stack and push its remainder 5 followed by its quotient (when divided by 10) back on. 6 test: 7 in: 123 divrem10 8 out: 3 12 9 }} 10 { 11 dup [10 %] dip 10 / 12 } 13 14 define int_rev : (int int -> int) 15 {{ 16 desc: 17 Reverse the integer on top of the stack (base 10). 18 test: 19 in: 1234 int_rev 20 out: 4321 21 }} 22 { 23 0 swap [[10 *] dip divrem10 [+] dip] [0 is_neq] while pop 24 } 25 26 define is_div10 : (int -> int bool) 27 {{ 28 desc: 29 Put True on the stack if the top number is 30 divisible by 10. Put False otherwise. 31 test: 32 in: 1230 is_div10 33 out: 1230 true 34 }} 35 { 36 dup 10 % 0 eq 37 } 38 39 define is_palnum : (int -> int bool) 40 {{ 41 desc: 42 Put True on the stack if the top number 43 is the same reversed (via int_rev). Put 44 False otherwise. 45 test: 46 in: 323 is_palnum 47 out: 323 true 48 }} 49 { 50 dup int_rev is_eq 51 } 52 53 define is_divrev : (int -> int bool) 54 {{ 55 desc: 56 Put True on the stack if the top number 57 is evenly divisible by its reverse (via int_rev). 58 Put False otherwise. 59 test: 60 in: 9801 is_divrev 61 out: 9801 true 62 }} 63 { 64 dup dup int_rev % 0 eq 65 } 66 67 define is_interesting : (int -> int bool) 68 {{ 69 desc: 70 Put True on the stack if the number is "interesting". 71 A number is interesting if it is different when reversed 72 (via int_rev) and it doesn't end in zero. Put False 73 otherwise. 74 test: 75 in: 123 is_interesting 76 out: 123 true 77 }} 78 { 79 is_palnum not swap 80 is_div10 not swap 81 is_divrev swap 82 [and and] dip swap 83 } 84 85 define reversibles 86 {{ 87 desc: 88 Push all numbers less than 1 million onto the 89 stack if they are divisible by their reverse 90 (via int_rev). Numbers divisible by 10 or the 91 same as their reverse are not counted; they are 92 uninteresting. 93 }} 94 { 95 1 [is_interesting [dup] [noop] if inc] [1000000 is_lt] while pop 96 }