Page 1 of 1

Palindromes

Posted: Tue Dec 12, 2006 7:37 pm
by Dr. Dri
It's not another string function, this functions returns true if an integer is a palindrome.

Code: Select all

Procedure.l Palindrome(n.l)
  Protected r.l, p.l, d.l, t.l
  
  If n < 0
    r = #False ;the "-" makes it can't be a palindrome
  ElseIf n < 10
    r = #True  ;from 0 to 9 we do have a palindrome
  Else
    d = n % 10 ;get the last digit of n
    p = n ;it will containt the first digit of n
    t = 1
    
    While p / 10 > 0 ;loop if p still got digits
      p / 10 ;withdraws digits from the right
      t * 10 ;precomputes the power of 10
    Wend
    
    ;n may be a palindrome
    If p = d
      n = (n - p*t) / 10 ;so you withdraws the 2 digits
      r = Palindrome(n)  ;and you check the new n
    Else
      r = #False
    EndIf
    
  EndIf
  
  ProcedureReturn r
EndProcedure

Debug Palindrome(923456789)
Debug Palindrome(923454329)
Dri ;)

Posted: Tue Dec 12, 2006 9:49 pm
by Psychophanta
Good, even "palindrome" meaning is more ususally referred to letters than to numbers.

And BTW what is the request?

Posted: Wed Dec 13, 2006 6:05 pm
by Dr. Dri
Psychophanta wrote:And BTW what is the request?
i can see two possibilities :
-i want it as a native function
-i aimed to post it as a "tip"

Dri :lol:

Posted: Wed Dec 13, 2006 6:28 pm
by Psychophanta
native function?? :? :lol:

Posted: Wed Dec 13, 2006 9:47 pm
by Konne
Psychophanta wrote:native function?? :? :lol:
:wink: