#!/usr/bin/perl # A sample script for MacPerl, which makes a new text file full of prime # numbers. The algorithm is inspired by an AppleScript script found in the # book of SHODA Tusyano, "Ouyou HyperCard" (Tokyo, ASCII, 1995). # Written by Kiwada Kats-hiro, 1997-1998. require "StandardFile.pl"; #$| = 1; $fname = &StandardFile'PutFile("Make New File as:","primes"); # Make a new file for output. open(OUT, ">$fname") || die "Cannot save $fname: $!"; select(OUT); $a = time; $num = 0; for (2 .. 1000) { # Set the area to search. $answer = $_; @prim = (); @set = (1,2,2,4); $x = $answer; $y = 0; $z = 2; while (($z)**2 <= $x) { $d = $x % $z; if ($d == 0) { $x = $x / $z; push(@prim,$z); } else { $z = $z + $set[$y]; if ($y == 3) { $y -= 1; } else { $y += 1; } } next if $x == 1; next if (scalar(@prim)) > 1; } push(@prim,$x) if ($x >= $z); if (scalar(@prim) == 1) { # 10 nums per line. print "$answer, "; $num += 1; if ($num % 10 == 0) { print "\n"; } } } $b = time; $c = $b - $a; print " $num numbers found. Time spent: $c sec. \n"; &MacPerl'SetFileInfo("YoED", "TEXT", $fname); # Set "creator" & "type" of the output file. close(OUT);