[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] What's wrong?



At 4:02 PM -0600 12/5/97, Dakota Duff wrote:

} Here's a short script that I wrote that I want to calculate prime numbers.
} When I run it though the running camel pointer appears, but he won't leave!
} The script never finishes running, so I have to force quit.  Thanks.

This kind of thing is what the debugger is for.

}
} Dakota
}
}
} #!/usr/local/bin/perl

#!/usr/local/bin/perl -d

}
} # PrimePick
} # by Dakota Duff
} # Looks for, and prints, prime numbers
} # Copyright December 1, 1997
}
} # This program may be distributed and edited freely,
} # but please leave the above comments attached.
}
} $Begin = 1;
} $stop = &MacPerl'Ask("Stop searching at:", "100");
} $End = int($stop);

This isn't really necessary if the input is integer.

} @intgrs = (1 .. "$End");

And there's no need for the quotes.  I don't know why you want this array
anyway.  This is a list of integers between 1 and $End.  What are you going
to use it for?

}
}
} while ($Begin < $End)  {
}   $div = 2;
}   $num = $Begin;
}   $status = 0;
}   while ($div < $End)  {

$div = 2 is always < $End.  This is an infinite loop.

}     $ans = $num/$div;
}     if ($ans == @intgrs)  {

Here you're asking if $ans is = the number of elements in @intgrs, which is
just $End.

}       $status = 1;
}     }
}     $num++;
}   }
}   if ($status == 0)  {
}     print $num;
}   }
}   $Begin++;
} }
}
} print "...DONE...";

print "...DONE...\n"; will probably look a little nicer.

}
}
}

You should learn about the modulus operator (%).  The definition of a prime
is a number that's divisible only by itself and 1.  Taking the % of numbers
between 1 and itself is one way of testing whether a number is prime
(probably not the fastest way).  You can also skip all even numbers, since
they are all, by definition, divisible by 2 and so not prime.  I'm sure
there must be prime finding algorithms (factoring is the technical term, if
I remember right) all over the place.  You should probably do a web search.


---
Paul J. Schinder
NASA Goddard Space Flight Center
Code 693, Greenbelt, MD 20771
schinder@pjstoaster.pg.md.us



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch