Palindromes

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Palindromes

Post 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 ;)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

Good, even "palindrome" meaning is more ususally referred to letters than to numbers.

And BTW what is the request?
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dr. Dri
Enthusiast
Enthusiast
Posts: 243
Joined: Sat Aug 23, 2003 6:45 pm

Post 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:
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

native function?? :? :lol:
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Konne
Enthusiast
Enthusiast
Posts: 434
Joined: Thu May 12, 2005 9:15 pm

Post by Konne »

Psychophanta wrote:native function?? :? :lol:
:wink:
Apart from that Mrs Lincoln, how was the show?
Post Reply