blob: de4cfe1eb6bdfae9ecc1f966e35fe63a3731dae8 (
plain)
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
|
#! /usr/bin/env gforth
\ Challenge 095
\
\ TASK #1 › Palindrome Number
\ Submitted by: Mohammad S Anwar
\ You are given a number $N.
\
\ Write a script to figure out if the given number is Palindrome. Print 1 if true otherwise 0.
\
\ Example 1:
\ Input: 1221
\ Output: 1
\ Example 2:
\ Input: -101
\ Output: 0, since -101 and 101- are not the same.
\ Example 3:
\ Input: 90
\ Output: 0
: is_palindrome { n -- f }
n 0< IF 0 EXIT THEN \ if <0 -> return 0
1 BEGIN n OVER 10 * > WHILE
10 *
REPEAT ( p10 ) \ highest power of 10 less than n
BEGIN n 10 >= WHILE
n OVER / ( n / p10 )
n 10 MOD ( n % 10 )
<> IF DROP 0 EXIT THEN \ if different -> return 0
n OVER MOD 10 / TO n \ remove hight and low digits
100 / \ divide p10 by 100
REPEAT
DROP 1 \ less than 10 -> return 1
;
next-arg evaluate \ convert argument to integer
is_palindrome . CR
BYE
|