Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Trouble

Status
Not open for further replies.

Phack

Programmer
Jun 4, 2003
1
US
Hello, im in the process of teaching myself PERL from a book. I have a background in VB, so it is going fairly easy for me, so far.... As one of the excercises in the book, there is a program i had to write to find prime numbers, but i cannot figure out why it wont run properly? I would greatly appreciate your help on this. Im trying to do the best i can with just this book i have, but i seem to constantly hit a roadblock.:( heres the code, please examine, yeah, its pathetic)


#!/usr/bin/perl -w

$maxprimes=20;
$value=1;
$count= 0;
While($count < $maxprimes) {
$value++;
$composite=0;
OUTER: for ($i=2; $i<$value; $i++) {
for($j=$i+; $j<$value; $j++) {
if (($j*$i)==$value) {
$composite = 1;
last OUTER;
}
}
}
if (! $composite) {
$count++;
print &quot;$value is prime\n&quot;;
}
}
 
&quot;it is going fairly easy for me, so far...&quot;. &quot;i seem to constantly hit a roadblock&quot;. Sounds paradoxal to me.
 
#!/usr/bin/perl -w

$maxprimes=20;
$value=1;
$count= 0;
While($count < $maxprimes) {
$value++;
$composite=0;
OUTER: for ($i=2; $i<$value; $i++) {
for($j=$i+; $j<$value; $j++) {
if (($j*$i)==$value) {
$composite = 1;
last OUTER;
}
}
}
if (! $composite) {
$count++;
print &quot;$value is prime\n&quot;;
}
}

while - lower case w
$i+1 rather than $i+, definitely not $i++ cos then you'll affect $i in the outer loop

Seems to work, except it's saying 49 is prime

HTH
Paul

It's important in life to always strike a happy medium, so if you see someone with a crystal ball, and a smile on their face ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top