LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2010, 03:24 PM   #1
suthagarka
LQ Newbie
 
Registered: Jul 2010
Location: London
Distribution: Cent OS, RH, Ubuntu
Posts: 7

Rep: Reputation: 0
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
 
Old 09-16-2010, 03:35 PM   #2
suthagarka
LQ Newbie
 
Registered: Jul 2010
Location: London
Distribution: Cent OS, RH, Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 0
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";

}
}
}
}
 
Old 09-16-2010, 03:37 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
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" ;
         }
      }
   }
}

Last edited by druuna; 09-16-2010 at 03:39 PM. Reason: Added sample code
 
Old 09-16-2010, 03:44 PM   #4
suthagarka
LQ Newbie
 
Registered: Jul 2010
Location: London
Distribution: Cent OS, RH, Ubuntu
Posts: 7

Original Poster
Rep: Reputation: 0
Hi druuna,

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


Thank You
K.Suthagar
 
Old 09-16-2010, 03:46 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Don't worry about it, stuff like that happens

BTW: You're welcome
 
Old 09-16-2010, 10:20 PM   #6
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
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
 
Old 09-17-2010, 01:24 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,362

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to break if loop in Perl? Barca Programming 9 08-03-2011 01:15 PM
perl loop jazman Programming 3 07-05-2006 10:31 AM
loop through string with perl lfur Programming 2 07-03-2004 08:05 AM
Need help with perl loop! morbid_ru Programming 1 02-24-2004 01:14 PM
Need help with perl loop! morbid_ru Programming 2 02-17-2004 05:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration