LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl for loop (https://www.linuxquestions.org/questions/programming-9/perl-for-loop-832652/)

suthagarka 09-16-2010 03:24 PM

Perl for loop
 
Hi,

I am quiet new to perl. I want to achieve the following result.

Find all the Pythagorean triples (a2 +b2=c2) where a,b,c are less than 1000. (a squared + b squared = c squared)

To achieve this goal. I created the following code.

#!/usr/bin/perl -w


for ($a = 1 ; $a < 1001 ; $a++)
{
for ($bi = 1 ; $b < 1001 ; $b++)
{
for ($b = 1 ; $b < 1001 ; $b++)
{
if ((($a * $a) + ($b * $b)) == ($c * $c))
{
print " value of (a) = $a \n";
print "value of (b) = $b \n";
print "value of (c) = $c \n";
}
}
}
}

When I execute this code, I get the following message.
…............
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
Use of uninitialized value in multiplication (*) at ./problem9 line 10.
…........................


Can anyone please help me with this?

Thank you in advance.

K.Suthagar

suthagarka 09-16-2010 03:35 PM

I also tried.



#!/usr/bin/perl -w

my $a,$b,$c;

for ($a = 1 ; $a < 1001 ; $a++)
{
for ($bi = 1 ; $b < 1001 ; $b++)
{
for ($b = 1 ; $b < 1001 ; $b++)
{
if ((($a * $a) + ($b * $b)) == ($c * $c))
{
print " value of (a) = $a \n";
print "value of (b) = $b \n";
print "value of (c) = $c \n";

}
}
}
}

druuna 09-16-2010 03:37 PM

Hi,

You have a typo: for ($bi = 1 ; $b < 1001 ; $b++)

And there is no $c. Shouldn't the second for ($b = 1 ; $b < 1001 ; $b++) be for ($c = 1 ; $c < 1001 ; $c++)

Hope this helps.

Code:

#!/usr/bin/perl -w

for ( $a = 1 ; $a < 11 ; $a++ ) {
  for ( $b = 1 ; $b < 11 ; $b++ ) {
      for ( $c = 1 ; $c < 11 ; $c++ ) {
        if ( ( ( $a * $a ) + ( $b * $b ) ) == ( $c * $c ) ) {
            print "value of (a) = $a \n" ;
            print "value of (b) = $b \n" ;
            print "value of (c) = $c \n" ;
        }
      }
  }
}


suthagarka 09-16-2010 03:44 PM

Hi druuna,

Thank you for the help. I feel like a total idiot now. Sorry for wasting your time.


Thank You
K.Suthagar

druuna 09-16-2010 03:46 PM

Hi,

Don't worry about it, stuff like that happens ;)

BTW: You're welcome :)

kurumi 09-16-2010 10:20 PM

Code:

#!/usr/bin/env ruby -w

a=(1..100).to_a
a.collect!{|x| x*x}
c=b=a
a.each_index do |x|
  (0..99).to_a.each do |y|
  print "a:#{Math.sqrt(a[x])}, b:#{Math.sqrt(b[y])}, c:#{a[x]+b[y]}\n" if c.include?(a[x] + b[y])
  end
end


chrism01 09-17-2010 01:24 AM

Highly recommend you add

use strict;

as the first line of ALL your Perl programs. You'll thank me later, I promise :)
Do continue to use the -w switch as well, they catch different kinds of errors.
ie
Code:

#!/usr/bin/perl -w
use strict;

Try to minimise using global vars once you get into subroutines.
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials


All times are GMT -5. The time now is 01:02 PM.