Palindrome Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mwesolow
    New Member
    • Apr 2010
    • 3

    Palindrome Question

    Hello,

    I'm having trouble getting my code to work. It seems that my function isPalindrome works correctly when I just test it out on some particular products of 3-digit integers, but when I use it in my full program, it fails to do its job.

    Here's my code:

    Code:
    for ($j = "100"; $j <= "110"; $j += 1){
     for($i = "100"; $i <= $j; $i += 1){
      if(isPalindrome($i*$j)){
       print STDOUT ($i*$j)." Is a product of two three-digit integers and a palindrome.";
      }
     }
    }
    
    sub isPalindrome {
     my($Qstring) = @_;
     for($i = 0; $i <= length($Qstring)-1; $i++){
      if(substr($Qstring,$i,1) == substr($Qstring,length($Qstring)-1-$i,1)){}
      else{return 0;}
     }
     return 1;
    }
    Thanks for any help!

    P.S. Yes, this is for one of the early Project Euler problems..
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    If you had used use strict; and use warnings;, you would have caught the problem. The problem is with the use of global variable inside the subroutine. The code below works (make use of local variables):

    Code:
    for (my $j = "100"; $j <= "110"; $j += 1){ 
     for(my $i = "100"; $i <= $j; $i += 1){ 
      if(isPalindrome($i*$j)){ 
       print STDOUT ($i*$j)." Is a product of two three-digit integers and a palindrome.\n"; 
      } 
     } 
    } 
    
      
    sub isPalindrome { 
     my($Qstring) = @_ ; 
     for(my $i = 0; $i <= length($Qstring)-1; $i++){ 
      if(substr($Qstring,$i,1) == substr($Qstring,length($Qstring)-1-$i,1)){} 
      else{return 0;} 
     } 
     return 1
    }

    Comment

    • mwesolow
      New Member
      • Apr 2010
      • 3

      #3
      Thanks! That worked perfectly. Now I have to figure out why the largest one it gives is 580085 = 583 * 995, when the solution is apparently 906609

      Comment

      • mwesolow
        New Member
        • Apr 2010
        • 3

        #4
        nevermind, got it :D

        Code:
        for ($j = "100"; $j <= "999"; $j += 1){
         for($i = "100"; $i <= $j; $i += 1){
          if(isPalindrome($i*$j) and $i*$j > $l*$p){
            $l=$i;
            $p=$j;
          }
         }
        }
        print STDOUT ($l*$p)." Is a product of two three-digit integers and a palindrome.\n";
        print STDOUT "($l,$p)";
        
        sub isPalindrome {
         my($Qstring) = @_;
         for($k = 0; $k <= length($Qstring)-1; $k++){
          if(substr($Qstring,$k,1) == substr($Qstring,length($Qstring)-1-$k,1)){}
          else{return 0;}
         }
         return 1;
        }
        
        Thanks again!

        Comment

        • toolic
          Recognized Expert New Member
          • Sep 2009
          • 70

          #5
          Code:
          sub isPalindrome { 
              my ($Qstring) = @_;
              return ($Qstring eq reverse($Qstring))
          }

          Comment

          • Chendil
            New Member
            • Mar 2010
            • 6

            #6
            Originally posted by toolic
            Code:
            sub isPalindrome { 
                my ($Qstring) = @_;
                return ($Qstring eq reverse($Qstring))
            }
            This solution is simple. Why reinvent the wheel.

            Use the reverse function in the perl and compare the value with the original string.

            Comment

            • Jyoti Ballabh
              Banned
              New Member
              • Jul 2010
              • 115

              #7
              or could be done in the following manner - convert the whole sequence into an alphanumeric code and then verify every digit in ASCII reference. At the end , set up a counter for the Competitive match score. it should settle it.

              Comment

              Working...