By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,675 Online
Bytes IT Community
+ Ask a Question
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
  1. for ($j = "100"; $j <= "110"; $j += 1){
  2.  for($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.";
  5.   }
  6.  }
  7. }
  8.  
  9. sub isPalindrome {
  10.  my($Qstring) = @_;
  11.  for($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.  
Thanks for any help!

P.S. Yes, this is for one of the early Project Euler problems..
Apr 27 '10 #1

✓ answered by nithinpes

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.  

Share this Question
Share on Google+
6 Replies


nithinpes
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
  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.  
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
  1. for ($j = "100"; $j <= "999"; $j += 1){
  2.  for($i = "100"; $i <= $j; $i += 1){
  3.   if(isPalindrome($i*$j) and $i*$j > $l*$p){
  4.     $l=$i;
  5.     $p=$j;
  6.   }
  7.  }
  8. }
  9. print STDOUT ($l*$p)." Is a product of two three-digit integers and a palindrome.\n";
  10. print STDOUT "($l,$p)";
  11.  
  12. sub isPalindrome {
  13.  my($Qstring) = @_;
  14.  for($k = 0; $k <= length($Qstring)-1; $k++){
  15.   if(substr($Qstring,$k,1) == substr($Qstring,length($Qstring)-1-$k,1)){}
  16.   else{return 0;}
  17.  }
  18.  return 1;
  19. }
  20.  
  21. Thanks again!
  22.  
Apr 27 '10 #4

Expert
P: 70
Expand|Select|Wrap|Line Numbers
  1. sub isPalindrome { 
  2.     my ($Qstring) = @_;
  3.     return ($Qstring eq reverse($Qstring))
  4. }
  5.  
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

Post your reply

Sign in to post your reply or Sign up for a free account.