437,933 Members | 1,675 Online Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

# Palindrome Question

 P: 3 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: Expand|Select|Wrap|Line Numbers 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.. Apr 27 '10 #1

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):

Expand|Select|Wrap|Line Numbers
1. for (my \$j = "100"; \$j <= "110"; \$j += 1){
2.  for(my \$i = "100"; \$i <= \$j; \$i += 1){
3.   if(isPalindrome(\$i*\$j)){
4.    print STDOUT (\$i*\$j)." Is a product of two three-digit integers and a palindrome.\n";
5.   }
6.  }
7.
8.
9. sub isPalindrome {
10.  my(\$Qstring) = @_ ;
11.  for(my \$i = 0; \$i <= length(\$Qstring)-1; \$i++){
12.   if(substr(\$Qstring,\$i,1) == substr(\$Qstring,length(\$Qstring)-1-\$i,1)){}
13.   else{return 0;}
14.  }
15.  return 1
16. }
17.

6 Replies

 Expert 100+ P: 410 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): Expand|Select|Wrap|Line Numbers 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 }   Apr 27 '10 #2

 P: 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 Apr 27 '10 #3

 P: 3 nevermind, got it :D Expand|Select|Wrap|Line Numbers 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!   Apr 27 '10 #4

 Expert P: 70 Expand|Select|Wrap|Line Numbers sub isPalindrome {      my (\$Qstring) = @_;     return (\$Qstring eq reverse(\$Qstring)) }   Apr 28 '10 #5

 P: 6 @toolic This solution is simple. Why reinvent the wheel. Use the reverse function in the perl and compare the value with the original string. Jun 8 '10 #6

 100+ P: 115 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. Jul 25 '10 #7 